when to use operator overloading in c++ code example
Example 1: c++ operator overloading
class Money
{
public:
Money & operator += (const Money &rhs);
}
Money& Money :: operator += (const Money &rhs)
{
return *this;
}
Example 2: Operator overloading in C++ Programming
#include <iostream>
using namespace std;
class Test
{
private:
int count;
public:
Test(): count(5){}
void operator ++()
{
count = count+1;
}
void Display() { cout<<"Count: "<<count; }
};
int main()
{
Test t;
++t;
t.Display();
return 0;
}
Example 3: operator ++ overloading c++
class Point
{
public:
Point& operator++() { ... }
Point operator++(int) { ... }
friend Point& operator++(Point &p);
friend Point operator++(Point &p, int);
};