Sunday, July 14, 2013

Function Overloading

Motivation : Assume you want to write a program where you are required to compute the area of :
1> a triangle
2> a rectangle
3> a square

You can Create different functions(with different names) for computing the area of all the different shapes.
or
You can use an extra argument and if-else(I encourage you to try to write both of the above solutions).

C++ provides us with another way and it is the known as "function overloading"

Def : A Collection of functions having the same name and different arguments.

Note : no restriction on return types.

Arguments may differ in 

1> Type
2> Number

Syntax : 
                    return type function name ( arg1) {
                                        //body                   
                   }
                   return type function name ( arg2) {
                                        //body
                   }
                   return type function name ( arg3) {
                                  //body
                   }


                  return type function name ( arg4) {
                                        //body              
    }
Note : argi, argj (i != j) differ in either (1) or (2) or both

look at the pic below for more clear understanding :




Now it's time to write a program :


Explanation : The above program is really a simple implementation of function overloading.
the function calls are matched to the corresponding bodies according to the number of arguments.
so,

    function  call line number       corresponding function body line number
                             19                                                                    11
                             23                                                                     8
                             28                                                                     4


Now, let's try to understand the basic mechanism of function overloading.
When c++ encounters a function call it tries to find the best matching body for the corresponding call.
C++ does it by the following way :
At first it tries to find an exact match i.e. the number of arguments and the type of each and every argument must match.

If c++ does not find a perfect match than it goes for match through promotion that is it applies all the implicit conversions like (int, char, short, enumeration)  all are promoted to int by the c++ compiler.























If c++ does not find a perfect match than it goes for match through standard c++ conversion techniques, that is it applies all the implicit conversions like (int, long)  all are implicitly converted into (double, float) by the c++ compiler.
Ambiguous call : This happens when more than one function matches a function call. In this case the compiler throws an error saying it cannot resolve the function call.



 Default Arguments

It is a technique where you can supply default values for function arguments (from right) such that if we call the function and don't supply values for these arguments than the default values are used.(Not that clear is it!!!!

Syntax : 

rt function name( dt var1, dt var2, dt vark = valk.........., dt  vark = valn)

rt -> return type and (0=


















let's look at the following program :



 as with all other posts i have uploaded the source code of all the programs here in google drive here : Learning c++


No comments:

Post a Comment