Write a program overload pre-increment operator.
#include<iostream> using namespace std; class A { private: int x,y; public: A(); A(int,int); void print(); //void operator ++(); friend void operator ++(A&); }; #if 0 //using member function void A :: operator ++() { ++x,++y; } #else //using friend function void operator ++(A& ob) { ++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,62); obj1.print(); obj2.print(); ++obj1;//obj1.operator++(); ++obj2; obj1.print(); obj2.print(); return 0; }
Output
parameterized constructor parameterized constructor x = 12 y = 57 x = 34 y = 62 x = 13 y = 58 x = 35 y = 63