Function Overloading

 < Day Day Up > 



Multiple functions of the same name can be declared and used in the same program. Functions with the same name but different parameter types or parameter list lengths are said to be overloaded. A function with the same name but different parameter types or parameter list length is said to have a different function signature.

Which of the overloaded functions is actually called is resolved by the compiler based on the types of arguments passed to the overloaded function. Return types play no role in resolving overloaded functions. Let us look at a simple function named functionA() that is overloaded in five different ways. Here is the header file:

Listing 9.40: functiona.h

start example
1  #ifndef FUNCTION_A_H  2  #define FUNCTION_A_H  3  4  void functionA();  5  void functionA(int i);  6  void functionA(float f);  7  void functionA(int i, int j);  8  void functionA(char message[]);  9 10  #endif
end example

functionA() is overloaded to take no arguments, one integer argument, one float argument, two integer arguments, or a character array. Each version of functionA() has a different function signature and will display different behavior when called as you will see by examining the following function definitions:

Listing 9.41: functiona.cpp

start example
1  #include "functiona.h"  2  #include <iostream>  3  using namespace std;  4  5  void functionA(){  6     cout<<"functionA: no arguments"<<endl;  7  }  8  9  void functionA(int i){ 10     cout<<"functionA: int argument = "<<i<<endl; 11  } 12 13  void functionA(float f){ 14     cout<<"functionA: float argument = "<<f<<endl; 15  } 16 17  void functionA(int i, int j){ 18     cout<<"functionA: two int arguments = "<<i<<", "<<j<<endl; 19  } 20 21  void functionA(char message[]){ 22     cout<<"functionA: char string = "<<message<<endl; 23  }
end example

As you can see, each version of the function, when called, will result in a different message being displayed on the screen. The compiler will resolve the issue of which version of functionA() to call based on what type of argument appears in the argument list at the time of the function call. Example 9.42 gives a main() function showing all five versions of functionA() in action:

Listing 9.42: main.cpp

start example
1  #include <iostream>  2  #include "functiona.h"  3  using namespace std;   4  5  int main(){  6     int ival1 = 1, ival2 = 2;  7     float fval = 25.345;  8     char char_array[] = "\"Hello World!\"";  9 10     functionA(); 11     functionA(ival1); 12     functionA(fval); 13     functionA(ival1, ival2); 14     functionA(char_array); 15 16     return 0; 17  }
end example

Figure 9-13 shows the results of running this program.

click to expand
Figure 9-13: Results of Calling Overloaded Function functionA()

You will routinely overload functions in your C++ programming career, especially class constructor functions.



 < 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