C++ functions: Overloading Functions

Overloading functions enables you to define the functions with the same name as long as their signatures are different.

The max function that was used earlier works only with the int data type. But what if you need to determine which of two floating-point numbers has the larger value? The solution is to create another function with the same name but different parameters, as shown in the following code:

double max(doub1e numl, double num2)

{

if (numl > num2)

return numl;

else

return num2;

}

If you call max with int parameters, the max function that expects int parameters will be invoked; if you call max with double parameters, the max function that expects double parameters will be invoked. This is referred to as function overloading; that is, two functions have the same name but different parameter lists within one file. The C++ compiler deter­mines which function is used based on the function signature.

The program in Listing 6.6 creates three functions. The first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values. All three functions are named max.

Listing 6.6 TestFunctionOverloading.cpp

1 #include <iostream>
2
using namespace std;
3
4
// Return the max between two int values
5 int max(int num1, int num2)
6 {
7   
if (num1 > num2)
8     
return num1;
9   
else
10     return num2;
11 }
12
13
// Find the max between two double values
14 double max(double num1, double num2)
15 {
16     
if (num1 > num2)
17     
return num1;
18     
else
19     return num2;
20 }
21
22
// Return the max among three double values
23 double max(double num1, double num2, double num3)
24 {
25     
return max(max(num1, num2), num3);
26 }
27
28
int main()
29 {
30     
// Invoke the max function with int parameters
31     cout << “The maximum between 3 and 4 is ” << max(3, 4) << endl;
32
33     
// Invoke the max function with the double parameters
34     cout << “The maximum between 3.0 and 5.4 is ”
35     << max(3.0, 5.4) << endl;
36
37     
// Invoke the max function with three double parameters

38     cout << “The maximum between 3.0, 5.4, and 10.14 is ”
39     << max(3.0, 5.4, 10.14) << endl;
40
41     
return 0;
42 }

When calling max(3, 4) (line 31), the max function for finding the maximum of two integers is invoked. When calling max(3.0, 5.4) (line 35), the max function for finding the maximum of two doubles is invoked. When calling max(3.0, 5.4, 10.14) (line 39), the
max function for finding the maximum of three double values is invoked.

Can you invoke the max function with an int value and a double value, such as max(2, 2.5)? If you can, which of the max functions is invoked? The answer to the first question is yes. The answer to the second is that the max function for finding the maximum of two double values is invoked. The argument value 2 is automatically converted into a double value and passed to this function.

You may be wondering why the function max(double, double) is not invoked for the call max(3, 4). Both max(double, double) and max(int, int) are possible matches for max(3, 4). The C++ compiler finds the most specific function for a function invocation. Since the function max(int, int) is more specific than max(double, double), max(int, int) is used to invoke max(3, 4).

Sometimes there are two or more possible matches for an invocation of a function, and the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation causes a compile error. Consider the following code:

#include <iostream>

using namespace std;

int maxNumber(int num1, double num2)

{

if (num1 > num2)

return num1;

else

return num2;

}

double maxNumber(double num1, int num2)

{

if (num1 > num2)

return num1;

else

return num2;

}

int main()

{

cout << maxNumber(1, 2) << endl;

return 0;

}

Both maxNumber(int, double) and maxNumber(doub1e, int) are possible candi­dates to match maxNumber(1, 2). Since neither is more specific, the invocation is ambigu­ous, resulting in a compile error.

If you change maxNumber(1, 2) to maxNumber(1, 2.0), it will match the first maxNumber function. So, there will be no compile error.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *