explain function overloading with example


Function overloading: overloading refers to the use of the same thing for different purposes. C++ also permits overloading of functions.this means that we can use the same function name to create functions that perform a variety of different task. This is known as function polymorphism in in object oriented programming.

Using the concept of function overloading we can design a family of functions with one function name but with different argument lists.the function would perform different operations depending on the argument list in the function call. The correct function to be invoked is is determined by checking the number and type of the argument but not on the function type.

a function call first matches the prototype having the same number and type of arguments and then call the appropriate function for execution. Ab best match must be unique. the function selection involves the following steps: 

The compiler first tries to find an exact match in which the types of actual arguments are the same, and use that function.

If an exact match is not found, the compiler uses the internal promotions to the actual arguments.

When either of them fails,the compiler tries to use the built-in conversion to the actual arguments and then uses the function whose match is unique. If the conversions is possible to have multiple matches, when the compiler will generate an error message.

If all of the steps fails, then the compiler will try to use user-defined conversions in combination with integral promotions and built-in conversions to find a unique match.

<
Overloading of a function should be done with caution. We should not overload and related functions and should reserves function overloading for functions that perform closely related task. Sometimes the default argument may be instead of overloading. This may reduce the number of functions to be defined.




0 Comments