Write a program overload post-increment operator.
#include<iostream> using namespace std; class A { private: int x,y; public: A(); A(int,int); void print(); //void operator ++(int); friend void operator ++(A&,int); }; #if 0 //using member function void A :: operator ++(int) { x++,y++; } #else //using friend function void operator ++(A& ob,int) { (ob.x)++; (ob.y)++; } #endif A :: A() { cout << "default constructor" << endl; } A :: A(int z,int t):x(z),y(t) { cout << "parameterized constructor" << endl; } void A :: print() { cout << "x = " << x << " y = " << y << endl; } int main() { A obj1(12,57),obj2(34,93); obj1.print(); obj2.print(); obj1++;//obj1.operator(int); obj2++; obj1.print(); obj2.print(); return 0; }
Output
parameterized constructor parameterized constructor x = 12 y = 57 x = 34 y = 93 x = 13 y = 58 x = 35 y = 94