overloading istream operator c++ code example

Example 1: operator overloading in c++ <<

ostream &operator<<(ostream &output, const MyClass &myObject)
{ 
  output << "P : " << myObject.property;
  return output;            
}

Example 2: c++ operator overloading

// money.h -- define the prototype
class Money
{
   public:
      Money & operator += (const Money &rhs);
}

// money.cpp -- define the implementation
Money& Money :: operator += (const Money &rhs)
{
   // Yadda Yadda
  
   return *this;
}

Example 3: operator ++ overloading c++

class Point
{
public:
	Point& operator++() { ... }		// prefix
	Point operator++(int) { ... }	// postfix
  	friend Point& operator++(Point &p);			// friend prefix
  	friend Point operator++(Point &p, int);		// friend postfix
  	// in Microsoft Docs written "friend Point& operator++(Point &p, int);"
};

Tags:

Cpp Example