Overloading Arithmetic Operators: , -, , ,

 < Day Day Up > 



Overloading Arithmetic Operators: +, -, *, /, %

The arithmetic operators are overloaded as non-member functions. They can be declared to be friends of the class of objects upon which they operate. To demonstrate overloading arithmetic operators I will revert to my favorite class named Foo. Example 14.13 gives the code for the Foo class declaration.

Listing 14.13: foo.h

start example
  1  #ifndef _FOO_H  2  #define _FOO_H  3  4  class Foo{  5   public:  6     Foo(int ival = 0);  7     ~Foo();  8     int getI();  9     Foo& operator=(int _i); 10     friend int operator+(Foo& lhs, Foo& rhs); 11     friend int operator-(Foo& lhs, Foo& rhs); 12   private: 13      int i; 14  }; 15  #endif
end example

In this example two arithmetic operators are overloaded: operator+() and operator-(). Notice that each operator function takes two arguments, both of type Foo. So, what is being said here is that two Foo objects can be added together and that one Foo object can be subtracted from another. Each function returns an integer value that results from the operation. Example 14.14 gives the code for the Foo.cpp file showing the implementation of each of these operators.

Listing 14.14: foo.cpp

start example
  1  #include "foo.h"  2  3  Foo::Foo(int ival):i(ival){}  4  5  Foo::~Foo(){};  6  7  int Foo::getI(){return i;}  8  9  Foo& Foo::operator=(int _i){ 10       i = _i; 11       return *this; 12  } 13 14  /**** Friend +, - functions *****/ 15   int operator+(Foo& lhs, Foo& rhs){ 13       return lhs.i + rhs.i; 17  } 18 19  int operator-(Foo& lhs, Foo& rhs){ 20  return lhs.i - rhs.i; 21  }
end example

Because of their friend status to class Foo, the overloaded operator functions enjoy free access to a Foo object's private attributes. Example 14.15 gives a main() function showing the overloaded arithmetic operators in use on some Foo objects.

Listing 14.15: main.cpp

start example
  1  #include <iostream>  2  using namespace std;   3  #include "foo.h"   4  5  int main(){  6  7      Foo f1(1), f2(2), f3(3);  8  9      cout<<f1.getI()<<endl; 10      cout<<f2.getI()<<endl; 11      cout<<f3.getI()<<endl; 12 13      f1 = f2 + f3; 14      f2 = f1 + f3; 15      f3 = f2 + f1; 16 17      cout<<f1.getI()<<endl; 18      cout<<f2.getI()<<endl; 19      cout<<f3.getI()<<endl; 20 21      f1 = f1 - f1; 22 23      cout<<f1.getI()<<endl; 24 25      return 0; 26  }
end example

Figure 14-7 shows the results of running example 14.15.


Figure 14-7: Results of Running Example 14.15

A Few Words About Error Checking

Example 14.14 demonstrates a very simplistic implementation of two overloaded arithmetic operators. In reality, you should check to ensure the result of the operation does not exceed the capacity of the returned type without taking appropriate action.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net