12.2 GLOBAL OVERLOAD DEFINITIONS FOR OPERATORS


12.2 GLOBAL OVERLOAD DEFINITIONS FOR OPERATORS

Let's consider the following example class as a candidate for operator overloading:

       Class MyComplex {           double re, im;      public:           MyComplex( double r, double i ) : re(r), im(i) {}           double getReal() { return re; }           double getImag() { return im; }      }; 

This class is supposed to represent a complex number by keeping together its real and imaginary parts. In the arithmetic of complex numbers, one wants to be able to carry out the same operations as in the arithmetic of, say, integers and floating point types. So given two complex numbers as

      MyComplex c1( 3, 5 );      MyComplex c2( 1, 4 ); 

it would be convenient if we could say

      MyComplex sum = c1 + c2;      MyComplex diff = c1 - c2;      ...      ... 

To be able to do this, we need to overload the binary operators such as ‘+,' ‘-,' and so on, for the MyComplex class. This overloading can be accomplished by providing global overload definitions such as

      MyComplex operator+( MyComplex arg1, MyComplex arg2) {          double d1 = arg1.getReal() + arg2.getReal();          double d2 = arg1.getImag() + arg2.getImag();          return MyComplex( d1, d2);      }      MyComplex operator-( MyComplex arg1, MyComplex arg2 ) {          double d1 = arg1.getReal() - arg2.getReal();          double d2 = arg1.getImag() - arg2.getImag();          return MyComplex( d1, d2 );      } 

and so on for the other arithmetic operators such as ‘*' for multiplication, ‘/' for division, and so on. Since we would also need to display the results of complex number arithmetic, we would also want to overload the output operator ‘<<':

      ostream&operator<< ( ostream&os, const MyComplex&arg ) {          double d1 = arg.getReal();          double d2 = arg.getImag();          os << "(" << d1 << "," << d2 <<")"<< endl;          return os;      } 

The function operator<< needs to return the ostream object that is its first argument so that we can chain these operators together in the following manner:

      cout << expr1 << exp2 << exp3 .. 

Shown below is a working program that includes the above overload definitions. In the code shown below, we have declared the access member functions getReal() and getImag() to be of type const (see Chapter 11 as to why).

 
//OverloadBinaryGlobal.cc #include <iostream> using namespace std; class MyComplex { double re, im; public: MyComplex( double r, double i ) : re(r), im(i) {} double getReal() const { return re; } double getImag() const { return im; } }; MyComplex operator+( const MyComplex arg1, const MyComplex arg2 ) { //(A) double d1 = arg1.getReal() + arg2.getReal(); //(B) double d2 = arg1.getImag() + arg2.getImag(); //(C) return MyComplex( d1, d2 ); } MyComplex operator-( MyComplex arg1, const MyComplex arg2 ) { //(D) double d1 = arg1.getReal() + arg2.getReal(); double d2 = arg1.getImag() + arg2.getImag(); return MyComplex( d1, d2 ); } ostream&operator<< ( ostream& os, const MyComplex& arg ) { //(E) double d1 = arg.getReal(); double d2 = arg.getImag(); os << "(" << d1 <<","<< d2 << ")" << endl; return os; } int main() { MyComplex first(3, 4); MyComplex second(2, 9); cout << first; // (3, 4) cout << second; // (2, 9) cout << first + second; // (5, 13) cout << first - second; // (1, -5) return 0; }




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

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