operator overloading c++ object 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
class Money
{
public:
Money & operator += (const Money &rhs);
}
Money& Money :: operator += (const Money &rhs)
{
return *this;
}
Example 3: 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);
}
};
Example 4: operator = overloading c++
inline bool operator==(const X& lhs, const X& rhs){ }
inline bool operator!=(const X& lhs, const X& rhs){ return !(lhs == rhs); }