FUNCTION OVERLOADING IN A CLASS

Examine the program named FUNCOVER.CPP for an example of function name overloading within a class. In this program the constructor is overloaded as well as one of the methods to illustrate what can be done.

This file illustrates some of the uses of overloaded names and a few of the rules for their use. You will recall that the function selected is based on the number and types of the formal parameters only. The type of the return value is not significant in overload resolution.

In this case there are three constructors. The constructor which is actually called is selected by the number and types of the parameters in the definition. In line 77 of the main program the three objects are declared, each with a different number of parameters and inspection of the results will indicate that the correct constructor was called based on the number of parameters.

In the case of the other overloaded methods, the number and type of parameters is clearly used to select the proper method. You will notice that one method uses a single integer and another uses a single float type variable, but the system is able to select the correct one. As many overloadings as desired can be used provided that all of the parameter patterns are unique.

You may be thinking that this is a silly thing to do but it is, in fact, a very important topic. Throughout this tutorial we have been using an overloaded operator and you haven’t been the least confused over it. It is the cout operator which operates as an overloaded function since the way it outputs data is a function of the type of its input variable or the field we ask it to display. Many programming languages have overloaded output functions so you can output any data with the same function name.

Be sure to compile and execute this program.