c++ overload operator in class code example

Example 1: operator overloading in c++ <<

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

Example 2: c++ overload < operator

struct Record
{
    std::string name;
    unsigned int floor;
    double weight;
    friend bool operator<(const Record& l, const Record& r)
    {
        return std::tie(l.name, l.floor, l.weight)
             < std::tie(r.name, r.floor, r.weight); // keep the same order
    }
};

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:

Misc Example