Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 2

#include <iostream>

using namespace std;

class MyOperator
{
private:
int x;
int y;
public:
MyOperator()
{
cout << "\n MyOperator() default called." << endl;
}
MyOperator(int x, int y)
{
this->x = x;
this->y = y;
cout << "\n MyOperator() parameter called." << endl;
}
void print()
{
cout << "\n X = " << x << endl;
cout << "\n Y = " << y << endl;
}
friend MyOperator operator++(MyOperator &box);
friend MyOperator operator--(MyOperator &box);
MyOperator operator=(MyOperator box)
{
x = box.x;
y = box.y;
cout << "\n Operator= overload called." << endl;
return *this;
}
};

MyOperator operator++(MyOperator &box)


{
box.x++;
box.y++;
cout << "\n Operator++ overload called." << endl;
return box;
}

MyOperator operator--(MyOperator &box)


{
box.x--;
box.y--;
cout << "\n Operator-- overload called." << endl;
return box;
}

int main()
{
MyOperator obj1(100, 200);
MyOperator obj2(400, 500);
obj1.print();
obj2.print();
++obj1;
obj1.print();
obj2 = ++obj1;
obj2.print();
--obj2;
obj2.print();
return 0;
}

You might also like