Overloading Class Member Functions

 < Day Day Up > 



Class member functions can be overloaded just like ordinary functions. Three of the four special member functions can be overloaded with the only exception being the destructor. In this section I will introduce you to member function overloading and show you a few examples of how you do it and, more importantly, demonstrate to you why you would want to overload a member function.

Function Signatures

Class member functions have function signatures just like ordinary functions. A function’s signature is determined by the number and type of its parameters. An overloaded member function is a function having the same name as another member function but a different function signature. The following functions are overloaded:

float computeSum(float a, float b);  float computeSum(float array[]);

Both functions have the same name, computeSum(), but the first function takes two floats as arguments while the second function takes an array of floats. You can overload a function name as many times as required, which leads to the obvious question.

Why would you want to overload member functions?

There are several good reasons to overload class member functions. The primary reason to overload is to obtain different functionality via one function name. Take for example a class constructor function. By overloading a class constructor you allow the creation of class objects in different ways. An alternative to overloading the constructor is to provide default values for all constructor parameters. This creates a default constructor that can be called with no arguments or with as many arguments as required. You saw an example of this in the constructor section above. The presence of a properly done default constructor generally eliminates the need to overload the constructor for the purpose of initializing objects in different ways.

Another reason to overload member functions is to provide one set of class public interface functions and another set of functions having the same names as a private interface. These private interface functions would be available only to other member functions within the class.

Let us take a look at an example of a class with overloaded member functions. Example 11.16 gives the header file for a class named Foo.

Listing 11.16: foo.h

start example
 1  #ifndef _FOO_H  2  #define _FOO_H  3  4  class Foo{  5    public:  6      Foo(int ival = 0, float fval = 0.0);  7      Foo(Foo& f_object);  8      Foo& operator=(Foo& rhs);  9      ~Foo(); 10      void printAttributes(); 11      void setI(int ival); 12      void setF(float fval); 13      void incrementF(); 14      void incrementF(float increment_value); 15      void incrementI(); 16      void incrementI(int increment_value); 17 18    private: 19      static int foo_count; 20      int i; 21      float f; 22      void printI(); 23      void printF(); 24  }; 25  #endif
end example

The Foo class does not do much but it does a nice job of illustrating some important concepts. A Foo object will have two attributes: an integer i and a float f. There is a class-wide static variable named foo_count which will be used to keep track of how many Foo objects exist. In addition to the private attributes there are two private member functions named printI() and printF(). Because these functions are declared private they, like the private attributes, are only accessible by other member functions. In other words, they are not horizontally accessible.

Two public functions incrementI() and incrementF() are overloaded. Calling either with no parameters will result in the associated attribute being incremented by one. Calling either function with an argument will result in the associated attribute being incremented by the argument’s value.

Example 11.17 gives the Foo class implementation file.

Listing 11.17: foo.cpp

start example
 1  #include "foo.h"  2  #include <iostream>  3  using namespace std;  4  5  int Foo::foo_count = 0;  6  7       Foo::Foo(int ival, float fval): i(ival), f(fval){  8  9           if((++foo_count) == 1) 10             cout<<"There is "<<foo_count<<" foo object."<<endl; 11            else 12               cout<<"There are "<<foo_count<<" foo objects."<<endl; 13       } 14 15       Foo::Foo(Foo& f_object){ 16         i = f_object.i; 17         f = f_object.f; 18 19         if((++foo_count) == 1) 20               cout<<"There is "<<foo_count<<" foo object."<<endl; 21              else 22                 cout<<"There are "<<foo_count<<" foo objects."<<endl; 23       } 24 25       Foo& Foo::operator=(Foo& rhs){   26           i = rhs.i; 27           f = rhs.f; 28           return *this; 29      } 30 31       Foo::~Foo(){ 32         if((--foo_count) == 1) 33             cout<<"There is "<<foo_count<<" foo object."<<endl; 34            else 35               cout<<"There are "<<foo_count<<" foo objects."<<endl; 36       } 37 38       void Foo::printAttributes(){ 39          printI(); 40          printF(); 41       } 42 43       void Foo::setI(int ival){ 44          i = ival; 45       } 46 47      void Foo::setF(float fval){ 48         f = fval; 49       } 50 51      void Foo::incrementF(){ 52         f += 1.0; 53      } 54      void Foo::incrementF(float increment_value){ 55         f += increment_value; 56       } 57      void Foo::incrementI(){ 58         ++i;  59       } 60 61      void Foo::incrementI(int increment_value){   62         i += increment_value;   63      } 64 65      void Foo::printI(){   66       cout<<"The value of i = "<<i<<endl;   67      } 68 69     void Foo::printF(){   70        cout<<"The value of f = "<<f<<endl;   71     }
end example

The two private member functions printI() and printF() are called by the printAttributes() function located on line 38. The foo_count static variable is used to keep track of the number of Foo objects there are in existence. Notice how it is used in the constructor, copy constructor and destructor. Keeping track of the number of objects comes in handy in many ways.

Example 11.18 shows Foo class objects being used in a main() function.

Listing 11.18: main.cpp

start example
 1  #include "foo.h"  2  3  int main(){  4     Foo f1(1, 2.5), f2(f1), f3;  5     f3 = f1;  6  7     f1.printAttributes();  8     f2.printAttributes();  9     f3.printAttributes(); 10 11     f1.incrementI(); 12     f1.incrementF(); 13     f1.printAttributes(); 14 15     f1.incrementI(5); 16     f1.incrementF(5.5); 17     f1.printAttributes(); 18 19     return 0; 20  }
end example

Figure 11-9 shows the output obtained by running example 11.18.

click to expand
Figure 11.9: Results of Running Example 11.18



 < 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