#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-D coordinates
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) { x = i; y = j; z = k; }
three_d operator++();
three_d operator++(int notused);
three_d operator--();
three_d operator--(int notused);
friend ostream &operator<<(ostream &strm, three_d op);
};
// Overload prefix ++ for three_d.
three_d three_d::operator++() {
x++;
y++;
z++;
return *this;
}
// Overload postfix ++ for three_d.
three_d three_d::operator++(int notused) {
three_d temp = *this;
x++;
y++;
z++;
return temp;
}
// Overload prefix -- for three_d.
three_d three_d::operator--() {
x--;
y--;
z--;
return *this;
}
// Overload postfix -- for three_d.
three_d three_d::operator--(int notused) {
three_d temp = *this;
x--;
y--;
z--;
return temp;
}
// The three_d inserter is a non-member operator function.
ostream &operator<<(ostream &strm, three_d op) {
strm << op.x << ", " << op.y << ", " << op.z << endl;
return strm;
}
int main()
{
three_d objA(1, 2, 3), objB(10, 10, 10), objC;
cout << "Original value of objA: " << objA;
cout << "Original value of objB: " << objB;
// Demonstrate ++ and -- as stand-alone operations.
++objA;
++objB;
cout << "++objA: " << objA;
cout << "++objB: " << objB;
--objA;
--objB;
cout << "--objA: " << objA;
cout << "--objB: " << objB;
objA++;
objB++;
cout << endl;
cout << "objA++: " << objA;
cout << "objB++: " << objB;
objA--;
objB--;
cout << "objA--: " << objA;
cout << "objB--: " << objB;
cout << endl;
objC = objA++;
cout << "After objC = objA++\n objC: " << objC <<" objA: " << objA << endl;
objC = objB--;
cout << "After objC = objB--\n objC: " << objC <<" objB: " << objB << endl;
objC = ++objA;
cout << "After objC = ++objA\n objC: " << objC <<" objA: " << objA << endl;
objC = --objB;
cout << "After objC = --objB\n objC: " << objC <<" objB: " << objB << endl;
return 0;
}
|