FUNCTION NAME OVERLOADING
Examine the file named OVERLOAD.CPP for an example of a program with the function names overloaded.
overload do_stuff; // This is optional
int do_stuff(const int); // This squares an integer
int do_stuff(float); // This triples a float & returns int
float do_stuff(const float, float); // This averages two floats
int index = 12;
float length = 14.33;
float height = 34.33;
cout << "12 squared is " << do_stuff(index) << "n";
cout << "24 squared is " << do_stuff(2 * index) << "n";
cout << "Three lengths is " << do_stuff(length) << "n";
cout << "Three heights is " << do_stuff(height) << "n";
cout << "The average is " << do_stuff(length,height) << "n";
int do_stuff(const int in_value) // This squares an integer
return in_value * in_value;
int do_stuff(float in_value) // Triples a float & return int
return (int)(3.0 * in_value);
// This averages two floats
float do_stuff(const float in1, float in2)
return (in1 + in2)/2.0;
// Result of execution
// 12 squared is 144
// 24 squared is 576
// Three lengths is 42
// Three heights is 102
// The average is 24.330002
This is not possible in ANSI-C, but is perfectly legal and in fact used quite regularly in C++. At first this will seem a bit strange, but it is one of the keystones of object oriented programming. You will see its utility and purpose very clearly in later chapters of this tutorial.
You will notice in this example program that there are three functions, in addition to the main function, and all three have the same name. Your first question is likely to be, “Which function do you call when you call do_stuff()?” That is a valid question and the answer is, the function that has the correct number of formal parameters of the correct types. If do_stuff() is called with an integer value or variable as its actual parameter, the function beginning in line 23 will be called and executed. If the single actual parameter is of type float, the function beginning in line 28 will be called, and if two floats are specified, the function beginning in line 34 will be called.
It should be noted that the return type is not used to determine which function will be called. Only the formal parameters are used to determine which overloaded function will be called.
The keyword overload used in line 4 tells the system that you really do intend to overload the name do_stuff, and the overloading is not merely an oversight. This is only required in C++ version 1.2. C++ version 2.0 and greater do not require the keyword overload but allows it to be used optionally in order to allow the existing body of C++ code to be compatible with newer compilers. It is not necessary to use this keyword because, when overloading is used in C++, it is generally used in a context in which it is obvious that the function name is overloaded.
The actual selection of which function to actually call is done at compile time, not at execution time so the program is not slowed down. If each of the overloaded function names were changed to different names, each being unique, there would be no difference in execution size or speed of the resulting program.
Overloading of function names may seem very strange to you, and it is strange if you are used to the rules of K&R or ANSI-C programming. As you gain experience with C++, you will feel very comfortable with this and you will use it a lot in your C++ programming.
Note the use of the keyword const used in some of the function prototypes and headers. Once again, this prevents the programmer from accidentally changing the formal parameter within the function. In a function as short as these, there is no real problem with an accidental assignment. In a real function that you occasionally modify, you could easily forget the original intention of the use of a value and attempt to change it during an extended debugging session.