Use a member function to overload the unary -. : Unary Operator « 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 » Unary OperatorScreenshots 
Use a member function to overload the unary -.
  
#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; }

  three_d operator+(three_d rh_op);

  three_d operator+(int rh_op);

  three_d operator-(three_d rh_op);

  three_d operator=(three_d rh_op);

  bool operator==(three_d rh_op);

  three_d operator-();

  friend ostream &operator<<(ostream &strm, three_d op);

  friend three_d operator+(int lh_op, three_d rh_op);
};

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

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

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

three_d three_d::operator-()
{
  three_d temp;

  temp.x = -x;
  temp.y = -y;
  temp.z = -z;
  return temp;
}

three_d three_d::operator=(three_d rh_op)
{
  x = rh_op.x;
  y = rh_op.y;
  z = rh_op.z;

  return *this;
}

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

ostream &operator<<(ostream &strm, three_d op) {
  strm << op.x << ", " << op.y << ", " << op.z << endl;

  return strm;
}

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;

  objC = -objA;
  cout << "This is -objA: " << objC;

  objC = objA + objB;
  cout << "objA + objB: " << objC;

  objC = objA - objB;
  cout << "objA - objB: " << objC;

  objC = objA + 10;
  cout << "objA + 10: " << objC;

  objC = 100 + objA;
  cout << "100 + objA: " << objC;

  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 a unary operator.Overload a unary 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.