Candidates for Overloading
We may overload the following operators (amongst others):
- post-fix/pre-fix (++ --)
- assignment (= += -= *= /= %=)
- unary arithmetic (+ -)
- binary arithmetic (+ - * / %)
- relational (== < > <= >= !=)
- logical (&& || !)
We may NOT overload the following operators
- the scope resolution operator (::)
- the member selection operator (.)
- the member selection through pointer to member operator (.*)
- the ternary operator (?:)
- the sizeof operator (sizeof)
- the typeid operator (type())
Type operator++(int) or Type operator--(int)
The int type in the parameter list
distinguishes the post-fix operator from its pre-fix
counterpart.
Student Student::operator++() {
for (int i = 0; grade[i] != '\0'; i++)
if (grade[i] == 'F') grade[i] = 'D';
else if (grade[i] != 'A') grade[i]--;
return *this;
}
Student Student::operator++(int) {
Student s = *this; // save the original
++(*this); // call the pre-fix operator
return s; // return the original
}
No comments:
Post a Comment