C++ Class Construct  «Prev  Next»
Lesson 6 Overloading member functions
Objective Overload member functions in the same class.

Overloading Member Functions in C++

Function Overloading

C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call. Function overloading is used to create several functions of the same name that perform similar tasks, but on different data types. For example, many functions in the math library are overloaded for different numeric types the C++ standard requires float, double and long double overloaded versions of the math library functions.

Procedural Overloading of Function

Figure 3-6 uses overloaded square functions to calculate the square of an int and the square of a double. C++ treats whole number literal values as type int. Similarly, line 24 invokes the double version of function square by passing the literal value 7.5, which C++ treats as a double value. In each case the compiler chooses the proper function to call, based on the type of the argument. The last two lines of the output window confirm that the proper function was called in each case.
Figure 3-6
#include <iostream>
using namespace std;

 // function square for int values
 int square(int x ){
  cout << "square of integer "  << x << " is ";
  return x * x;
 } 

 // function square for double values
 double square(double y ){
  cout << "square of double " << y << " is ";
  return y * y;
 } 
 // ----- main
 int main(){
  cout << ; // calls int version
  cout << endl;
  cout << ; // calls double version
  cout << endl;
 }
Output of program below:
square of integer 7 is 49
square of double 7.5 is 56.25

Overloading Functions within Class

Member functions within the same class can be overloaded. Remember that a function is called based on its signature, which is the list of argument types in its parameter list. Consider adding to the class ch_stack a pop operation which has an integer parameter that is the number of times the ch_stack should be popped. It could be added as the following function prototype within the class:

class ch_stack {
   .....
   char pop(int n);      //within ch_stack
   .....
};

char ch_stack::pop(int n)
{
   assert(n <= top);
   while(n-- > 1)
     top--;
   return s[top--];
}

The definition that is invoked depends on the actual arguments to pop:
data.pop();           //invokes standard pop
data.pop(5);          //invokes iterated pop

A member function is conceptually part of the type. There is no distinct member function pop for each ch_stack object.
There is quite a bit more to overloading functions, but this requires a more in-depth look at polymorphism, which is covered in the third course in the C++ for C Programmers series, Designing Reusable Code in C++.

The C++ Programming Language