Pages

July 26, 2010

Revew OOP244 - Operators overload

Candidates for Overloading

We may overload the following operators (amongst others):

  1. post-fix/pre-fix (++ --)
  2. assignment (= += -= *= /= %=)
  3. unary arithmetic (+ -)
  4. binary arithmetic (+ - * / %)
  5. relational (== < > <= >= !=)
  6. 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())
Post-Fix Operators
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