Overloading The Comparison Operators

# Introduction

The comparison operators are all binary operators that do not modify their operands so we can make our overloaded comparison operators friend functions.

# Examples

**Example 1**

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; } double GetZ() { return m_dZ; } }; ostream& operator<< (ostream &out, Point &cPoint) { // Since operator<< is a friend of the Point class, we can access // Point's members directly. out << "(" << cPoint.m_dX << ", " << cPoint.m_dY << ", " << cPoint.m_dZ << ")"; return out; } istream& operator>> (istream &in, Point &cPoint) { in >> cPoint.m_dX; in >> cPoint.m_dY; in >> cPoint.m_dZ; return in; }

**Example 2**

class Cents { private: int m_nCents; public: Cents(int nCents) { m_nCents = nCents; } friend bool operator> (Cents &cC1, Cents &cC2); friend bool operator<= (Cents &cC1, Cents &cC2); friend bool operator< (Cents &cC1, Cents &cC2); friend bool operator>= (Cents &cC1, Cents &cC2); }; bool operator> (Cents &cC1, Cents &cC2) { return cC1.m_nCents > cC2.m_nCents; } bool operator<= (Cents &cC1, Cents &cC2) { return cC1.m_nCents <= cC2.m_nCents; } bool operator< (Cents &cC1, Cents &cC2) { return cC1.m_nCents < cC2.m_nCents; } bool operator>= (Cents &cC1, Cents &cC2) { return cC1.m_nCents >= cC2.m_nCents; }