Virtual Overloaded Operators

 < Day Day Up > 



Overloaded operators can be overridden in derived classes like ordinary virtual functions. I will use two classes named Foo and Bar to demonstrate how to override the equality operator in a class deriving from Foo. Example 14.28 gives the Foo class declaration containing an overloaded equality operator.

Listing 14.28: foo.h

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

Notice the use of the keyword virtual in the declaration of the equality operator on line 8. Example 14.29 gives the declaration of class Bar.

Listing 14.29: bar.h

start example
  1  #ifndef _BAR_H  2  #define _BAR_H  3  #include "foo.h"  4  5  class Bar : public Foo{  6   public:  7       Bar(int _i = 0);  8       ~Bar();  9       virtual bool operator==(Bar& rhs); 10       virtual bool operator==(Foo& rhs); 11   private: 12       int i; 13  }; 14  #endif
end example

Bar extends Foo and declares two equality operators. The one declared on line 9 overloads the equality operator to take Bar objects. The version declared on line 10 overrides Foo's equality operator because it has the same function signature.

Examples 14.30 and 14.31 give the source code for foo.cpp and bar.cpp respectively.

Listing 14.30: foo.cpp

start example
  1  #include "foo.h"  2  #include <iostream>  3  using namespace std;  4  5  Foo::Foo(int _i):i(_i){}  6  7  Foo::~Foo(){}  8  9  bool Foo::operator==(Foo& rhs){ 10       cout<<"Foo == called."<<endl; 11       return i == rhs.i; 12  } 13 14  int Foo::getI(){return i;}
end example

Listing 14.31: bar.cpp

start example
  1  #include "bar.h"  2  #include <iostream>  3  using namespace std;  4  5  Bar::Bar(int _i):Foo(_i), i(_i){}  6  7  Bar::~Bar(){}  8  9  bool Bar::operator==(Bar& rhs){ 10       cout<<"Bar == called"<<endl; 11       return i == rhs.i; 12  } 13 14  bool Bar::operator==(Foo& rhs){ 15       cout<<"Bar(Foo) == called."<<endl; 16       return i == rhs.getI(); 17  }
end example

Example 14.32 on the following page gives a main() function showing the equality operator being used on Foo and Bar objects. Figure 14-12 below shows the results of running example 14.32.

Listing 14.32: main.cpp

start example
  1  #include <iostream>  2  using namespace std;  3  #include "foo.h"  4  #include "bar.h"  5  6  int main(){  7       Foo f1(1), f2(2);  8       if(f1==f2){  9            cout<<"Objects have same i value!"<<endl; 10            } 11            else cout<<"Objects have different i value!"<<endl; 12 13    Foo* fptr = new Bar(5); 14 15      if(f1== (*fptr)){ 16           cout<<"Objects have same i value!"<<endl; 17           } 18           else cout<<"Objects have different i value!"<<endl; 19 20   Bar b1(1), b2(2); 21 22     if(b1==b2){ 23           cout<<"Objects have same i value!"<<endl; 24           } 25            else cout<<"Objects have different i value!"<<endl; 26 27      return 0; 28  }
end example

click to expand
Figure 14-12: Results of Running Example 14.32



 < 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