Coupon Accepted Successfully!


Principles of Function Overloading

  1. If two functions have the similar type and number of arguments (data type), the function cannot be overloaded. The return type may be similar or void, but argument data type or number of arguments must be different. For example,
    1. sum(int,int,int);
      Here, the above function can be overloaded. Though the data type of arguments in both the functions are similar, number of arguments are different.
    2. sum(int,int,int);
      In the above example, number of arguments in both the functions are same, but data types are different. Hence, the above function can be overloaded.
  2. Passing constant values directly instead of variables also results in ambiguity. For example,
    int sum(int,int);
    float sum(float,float,float);
    Here, sum() is an overloaded function for integer and float values. Values are passed as follows:
    The compiler will flag an error because the compiler cannot distinguish between these two functions. Here, internal conversion of float to int is possible. Hence, in both the above calls integer version of function sum() is executed. To overcome this problem, the user needs to do the following things.
    (1) Declare prototypes of all the overloaded functions before function main().
    (2) Pass argument using variables as follows:
    sum(a,b); // a and b are integer variables
    sum(e,r,t,y); // e,r,t, and y are float variables
    Refer program number 7.22 for confirmation. Also try this program with direct values and by declaring function prototype inside main().
  3. The compiler attempts to find an accurate function definition that matches in types and number of arguments and invokes that function. The arguments passed are checked with all declared function. If matching function is found then that function gets executed.
  4. If there are no accurate match found, compiler makes the implicit conversion of actual argument. For example, char is converted to int and float is converted to double. If all the above steps have failed, then compiler performs user-built functions.

The following example illustrates this point:

7.21 Write a program to overload a function and create the situation that the compiler does integral conversion.

Explanation: In the above program, the function square is overloaded. The first prototype of function sqr(int) is proposed for integer variable and second for double variable. In function main() the “A” character data type value is passed to the function sqr(). The compiler invokes the integer version of the function sqr() because the char data type is compatible with integer. In the second call, a float value is passed to the function sqr(). This time the compiler invokes the double data type version of function sqr().

When accurate match is not found the compiler makes internal conversion as seen in the above example. C++ automatically attempts to convert the arguments used to call a function into the type of arguments expected by the function.

  1. If internal conversion fails, user-defined conversion is carried out with implicit conversion and integral promotion. The user-defined conversion is used with class objects.
  2. Sometimes while making internal conversion, ambiguity is created if one data type is compatible with two or more data types. If such a situation occurs, the compiler displays an error message.
    Consider the following example. Following are two versions of sqr() function, one is for long data type and other for double data type.
    If an integer value is passed to the function sqr(), the compiler will have confusion of which version is to be executed and will result in an error message “Ambiguity between ‘sqr(long)’ and ‘sqr(double)’”.
  3. The program has two versions of function, that is one for float and second for double data type. In such a situation if we pass a float number, the double version of function is selected for execution. But the same is not applicable with integer and long integer.

7.22 Write a program to define overloaded function add() for integer and float and perform the addition.

Explanation: In the above program, two versions of function add() are declared. One for integer values and the other for float values. Three integers and float values are entered through the keyboard. According to the data type, the compiler executes appropriate function. The prototype of functions should be written before main() function. If we write the prototype inside the main() function, the float version of the function will not be executed.

7.23 Write a program to define overloaded function. Invoke the overloaded function through another overloaded function.

xplanation: In the above program, the function add() is overloaded. The add(void) function does not require any argument but returns integer. The float add() function requires three arguments of float type. Here, the arguments are fa, fb, fc, fd, and id and are declared as global so that any function can call them. After entering three float values, the add(void) function is called. The add(void) function calls the float type add(), the float type add() function calculates addition and returns results to function add(void). The add(void) function then returns addition as an integer. In the second call, the compiler calls the float add() function; this function returns addition of float values. The output of the program is shown above.

Test Your Skills Now!
Take a Quiz now
Reviewer Name