+ is overloaded for three_d + three_d and for three_d + int. : Plus « Overload « C++






+ 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;
}
  
    
  








Related examples in the same category

1.Overload the + relative to MyClass.Overload the + relative to MyClass.
2.Overload + for 'ob + int' as well as 'ob + ob'.Overload + for 'ob + int' as well as 'ob + ob'.
3.String class with custom +/- operator
4.additional meanings for the + and = operations
5.overload the "+" operator so that several angles, in the format degrees minutes seconds, can be added directly.
6.Friendly operator+
7.overloaded '+' operator adds two Distances
8.overloaded '+' operator concatenates strings
9.Define operator +(plus) and cast to double operator
10.Overloading the + (or any other binary operator) using a friend allows a built-in type to occur on the left or right side of the operator.
11.friend overloaded + operator