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

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Overload » PlusScreenshots 
+ 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(123), objB(101010), 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 == objBcout << "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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.