c++ override operator explain 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: overload the >> operator in c++

istream &operator>>( istream  &input, Class_Name &c )

Tags:

Cpp Example