#include #include using namespace std; class IntPair { public: IntPair(int firstValue, int secondValue); IntPair operator++( ); //Prefix version IntPair operator++(int); //Postfix version IntPair& operator+= (const IntPair& x); // overload of += operator void setFirst(int newValue); void setSecond(int newValue); int getFirst( ) const; int getSecond( ) const; private: int first; int second; }; int main( ) { IntPair a(1,2); cout << "Postfix a++: Start value of object a: "; cout << a.getFirst( ) << " " << a.getSecond( ) << endl; IntPair b = a++; cout << "Value returned: "; cout << b.getFirst( ) << " " << b.getSecond( ) << endl; cout << "Changed object: "; cout << a.getFirst( ) << " " << a.getSecond( ) << endl; a = IntPair(1, 2); cout << "Prefix ++a: Start value of object a: "; cout << a.getFirst( ) << " " << a.getSecond( ) << endl; IntPair c = ++a; cout << "Value returned: "; cout << c.getFirst( ) << " " << c.getSecond( ) << endl; cout << "Changed object: "; cout << a.getFirst( ) << " " << a.getSecond( ) << endl; IntPair x(10,20); IntPair y(3, 5); cout << "Start value of object x: "; cout << x.getFirst( ) << " " << x.getSecond( ) << endl; cout << "Start value of object y: "; cout << y.getFirst( ) << " " << y.getSecond( ) << endl; x += y; // add y to x cout << "After operation x += y...\n"; cout << "New value of object x: "; cout << x.getFirst( ) << " " << x.getSecond( ) << endl; cout << "New value of object y: "; cout << y.getFirst( ) << " " << y.getSecond( ) << endl; return 0; } IntPair::IntPair(int firstValue, int secondValue) : first(firstValue), second(secondValue) {/*Body intentionally empty*/} IntPair IntPair::operator++(int ignoreMe) //postfix version { int temp1 = first; int temp2 = second; first++; second++; return IntPair(temp1, temp2); } IntPair IntPair::operator++( ) //prefix version { first++; second++; return IntPair(first, second); } IntPair& IntPair::operator+=(const IntPair& x) { first += x.first; second += x.second; return *this; } void IntPair::setFirst(int newValue) { first = newValue; } void IntPair::setSecond(int newValue) { second = newValue; } int IntPair::getFirst( ) const { return first; } int IntPair::getSecond( ) const { return second; }