Demonstrate operator overloading basics using the Box class. - C++ Class

C++ examples for Class:Operator Overload

Introduction

This example uses member functions to overload the binary +, -, =, and == operators, and overload the unary -.

The + is overloaded for Box + Box and for Box + int.

Non-member functions are used to create custom inserters for Box objects and to overload + for int + Box.

Demo Code


#include <iostream>
using namespace std;
class Box {/* w  w  w .ja  va  2s.  c  o m*/
   int x, y, z; // 3-D coordinates
   public:
   Box() { x = y = z = 0; }
   Box(int i, int j, int k) { x = i; y = j; z = k; }
   // Add two Box objects together.
   Box operator+(Box rh_op);
   // Add an integer to a Box object.
   Box operator+(int rh_op);
   // Subtract two Box objects.
   Box operator-(Box rh_op);
   // Overload assignment.
   Box operator=(Box rh_op);
   // Overload ==.
   bool operator==(Box rh_op);
   // Overload - for unary operation.
   Box operator-();
   // Let the overloaded inserter be a friend.
   friend ostream &operator<<(ostream &strm, Box op);
   // Let the overloaded + be a friend.
   friend Box operator+(int lh_op, Box rh_op);
};
// Overload binary + so that corresponding coordinates are added.
Box Box::operator+(Box rh_op)
{
   Box 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 Box object.
Box Box::operator+(int rh_op)
{
   Box 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.
Box Box::operator-(Box rh_op)
{
   Box 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.
Box Box::operator-()
{
   Box temp;
   temp.x = -x;
   temp.y = -y;
   temp.z = -z;
   return temp;
}
// Overload assignment for Box.
Box Box::operator=(Box rh_op)
{
   x = rh_op.x;
   y = rh_op.y;
   z = rh_op.z;
   return *this;
}
bool Box::operator==(Box rh_op)
{
   if( (x == rh_op.x) && (y == rh_op.y) && (z == rh_op.z) )
      return true;
   return false;
}
// Overload << as a custom inserter for Box objects.
ostream &operator<<(ostream &strm, Box op) {
   strm << op.x << ", " << op.y << ", " << op.z << endl;
   return strm;
}
// Overload + for int + obj.
Box operator+(int lh_op, Box rh_op) {
   Box 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()
{
   Box 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;
}

Result


Related Tutorials