Navigation



9.6 — Overloading operators using member functions

In the lesson on overloading the arithmetic operators, you learned that when the operator does not modify it’s operands, it’s best to implement the overloaded operator as a friend function of the class. For operators that do modify their operands, we typically overload the operator using a member function of the class.

Overloading operators . . . → Read More: 9.6 — Overloading operators using member functions

9.5 — Overloading unary operators +, -, and !

Overloading unary operators

Unlike the operators you’ve seen so far, the positive (+), negative (-) and logical not (!) operators all are unary operators, which means they only operate on one operand. Because none of these operators change their operands, we will be implementing them as friend functions. All three operands are implemented in . . . → Read More: 9.5 — Overloading unary operators +, -, and !

9.4 — Overloading the comparison operators

Overloading the comparison operators is simple once you’ve learned how to overload the arithmetic operators.

Because the comparison operators are all binary operators that do not modify their operands, we will make our overloaded comparison operators friend functions.

Here’s an example Point class from the previous lesson with an overloaded operator== and operator!=.

class . . . → Read More: 9.4 — Overloading the comparison operators

9.3 — Overloading the I/O operators

Overloading >:

class Point { private: double m_dX, m_dY, m_dZ; public: Point(double dX=0.0, double dY=0.0, double dZ=0.0) { m_dX = dX; m_dY = dY; m_dZ = dZ; } friend ostream& operator<< (ostream &out, Point &cPoint); friend istream& operator>> (istream &in, Point &cPoint); double GetX() { return m_dX; } double GetY() { return m_dY; } . . . → Read More: 9.3 — Overloading the I/O operators