// Define + and = for class ThreeD #include using namespace std; class ThreeD { public: ThreeD(); // default constructor ThreeD(int i, int j, int k); // explicit constructor ~ThreeD() {} // destructor ThreeD operator+(ThreeD op2); // op1 is implied ThreeD operator=(ThreeD op2); // op1 is implied void show(); private: int x, y, z; // 3-d coordinates }; int main() { ThreeD a(1, 2, 3), b(10, 10, 10), c; cout << "Original value of a: "; a.show(); cout << "Original value of b: "; b.show(); cout << "Original value of c: "; c.show(); cout << endl; c = a + b; // add a and b cout << "Value of c after c = a + b: "; c.show(); cout << endl; c = b = a; // demonstrate multiple assignment cout << "Value of a after c = b = a: "; a.show(); cout << "Value of b after c = b = a: "; b.show(); cout << "Value of c after c = b = a: "; c.show(); return 0; } ThreeD::ThreeD() // default constructor { x = y = z = 0; } ThreeD::ThreeD(int i, int j, int k) // explicit constructor { if(i < 0 || j < 0 || k < 0) { cout << "Negative coordinate - program terminated\n"; exit (1); } x = i; y = j; z = k; } // overload + ThreeD ThreeD::operator+(ThreeD op2) { ThreeD temp; // integer additions in the standard sense temp.x = x + op2.x; // temp.x = this->x + op2.x; temp.y = y + op2.y; // temp.y = this->y + op2.y; temp.z = z + op2.z; // temp.z = this->z + op2.z; return temp; // return the new object } // overload = ThreeD ThreeD::operator=(ThreeD op2) { // integer assignments in the standard sense x = op2.x; // this->x = op2.x; y = op2.y; // this->y = op2.y; z = op2.z; // this->z = op2.z; // dereference the this pointer // and return the modified object return *this; } // show X, Y, and Z coordinates void ThreeD::show() { cout << x << ", "; cout << y << ", "; cout << z << endl; }