C++ function overloading
overloading in function refers to the use of the same thing for different purpose. C++ language also permits overloading of functions. This means that we can use the same functions names to create functions that performs a variety of different tasks. This is known as function polymorphism in Object oriented programming.
using the concept of function overloading, we can design a family of function of functions with one function name but with different argument (parameters) list. The function would perform different operations
depending on the argument list. The function would perform different operations depending on the argument(parameters) list in the function call. The correct function to be invoked is determined by checking the number of agruments and type of the arguments but not on the function type.
for example an overloaded add() function handles different types of data as shown below
// declarations
add( a, b);
add( a, b,c);
add( x, y)
add( p, q);
add( p, q);
// FUNCTION calls
std::cout<<add(7,10);
std::cout<<add(67,34,23);
std::cout<<add(34.5,23.6);
std::cout<<add(12,34.7);
std::cout<<add(34.7,12);
|
A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique. The function selection involves the following steps
value=amount(5000,7); //so here one arguments missing
|
passes the value of 5000 to principal variable and 7 to period variable and then lets the function use default value of 0.15 for rate variable. The call
1. The compiler first tries to find an exact match in which the types of actual arguments are the same, and use that function.
2. if an exact match is not found, the compiler uses the integral promotions to the actual arguments such as
3. When either of them fails, the compiler tries to use the build in conversions(implicit assignment conversions) to the actual arguments and then uses the function whose match is unique. if the conversion is possible to have multiple matches, then the compiler will generate an error message. suppose we use the following two functions
long square(long n)
double square(double x)
A function call such as
square(12);
will cause an error because int argument can be converted to either long or double, thereby creating an ambiguous situation as to which version of square() should be used.
4. if all of the steps fail, then the compiler will try the user defined conversion in combination with integral promotions and built in conversion to find a unique match user defined conversions are often used in handling class objects.
example of function overloading
<iostream>
using namespace std;
// declarations (prototypes)
volume();
volume(,);
volume(,,);
main()
{
std::cout<<volume(10)<<"\n";
std::cout<<volume(2.5,8)<<"\n";
std::cout<<volume(100L,75,15)<<"\n";
0;
}
volume()
{
(s*s*s);
}
volume( r, h)
{
(3.14519*r*r*h);
}
volume( l, b, h)
{
(l*b*h);
}
|
The output is:
1000
157.26
112500
|
overloading of the function should be done with caution. We should not overloaded unrelated functions and should reserve function overloading for function that perform closely related tasks. sometimes , the default arguments may be used instead of overloading. This may reduce the number of functions to be defined.
overloaded functions are extensively used for handling class objects. They will be illustrated later when the classes are discussed in next pages