Mega Code Archive

 
Categories / C++ / Overload
 

+ is overloaded for three_d + three_d and for three_d + int

#include <iostream> using namespace std; class three_d {   int x, y, z; public:   three_d() { x = y = z = 0; }   three_d(int i, int j, int k) { x = i; y = j; z = k; }   // Add two three_d objects together.   three_d operator+(three_d rh_op);   // Add an integer to a three_d object.   three_d operator+(int rh_op);   // Subtract two three_d objects.   three_d operator-(three_d rh_op);   // Overload assignment.   three_d operator=(three_d rh_op);   // Overload ==.   bool operator==(three_d rh_op);   // Overload - for unary operation.   three_d operator-();   // Let the overloaded inserter be a friend.   friend ostream &operator<<(ostream &strm, three_d op);   // Let the overloaded + be a friend.   friend three_d operator+(int lh_op, three_d rh_op); }; // Overload binary + so that corresponding coordinates are added. three_d three_d::operator+(three_d rh_op) {   three_d temp;   temp.x = x + rh_op.x;   temp.y = y + rh_op.y;   temp.z = z + rh_op.z;   return temp; } // Overload binary + so that an integer can be added to a three_d object. three_d three_d::operator+(int rh_op) {   three_d temp;   temp.x = x + rh_op;   temp.y = y + rh_op;   temp.z = z + rh_op;   return temp; } // Overload binary - so that corresponding coordinates are subtracted. three_d three_d::operator-(three_d rh_op) {   three_d temp;   temp.x = x - rh_op.x;   temp.y = y - rh_op.y;   temp.z = z - rh_op.z;   return temp; } // Overload unary - so that it negates the coordinates. three_d three_d::operator-() {   three_d temp;   temp.x = -x;   temp.y = -y;   temp.z = -z;   return temp; } // Overload assignment for three_d. three_d three_d::operator=(three_d rh_op) {   x = rh_op.x;   y = rh_op.y;   z = rh_op.z;   return *this; } // Overload == for a three_d object. bool three_d::operator==(three_d rh_op) {   if( (x == rh_op.x) && (y == rh_op.y) && (z == rh_op.z) )     return true;   return false; } // These are non-member operator functions. // // Overload << as a custom inserter for three_d objects. ostream &operator<<(ostream &strm, three_d op) {   strm << op.x << ", " << op.y << ", " << op.z << endl;   return strm; } // Overload + for int + obj. three_d operator+(int lh_op, three_d rh_op) {   three_d temp;   temp.x = lh_op + rh_op.x;   temp.y = lh_op + rh_op.y;   temp.z = lh_op + rh_op.z;   return temp; } int main() {   three_d objA(1, 2, 3), objB(10, 10, 10), objC;   cout << "This is objA: " << objA;   cout << "This is objB: " << objB;   // Obtain the negation of objA.   objC = -objA;   cout << "This is -objA: " << objC;   // Add objA and objB together.   objC = objA + objB;   cout << "objA + objB: " << objC;   // Subtract objB from objA.   objC = objA - objB;   cout << "objA - objB: " << objC;   // Add obj + int.   objC = objA + 10;   cout << "objA + 10: " << objC;   // Add int + obj.   objC = 100 + objA;   cout << "100 + objA: " << objC;   // Compare two objects.   if(objA == objB) cout << "objA is equal to objB.\n";   else cout << "objA is not equal to objB.\n";   return 0; }