Chapter 3 Function Overloading Function Overloading. Calling Overloaded Function.
Function Overloading When Several Function declaration are specified for a single function name in the same scope, the function name is said to be overloaded. C++ allows the function to have the same name if it can distinguish them by their number and type of arguments. float divide (int a, int b); float divide (float x, float y); Here divide have name of two function but their argument type is different. Hence such types of function is called function overloading.
Need of Function Overloading Object have characteristics and associated behavior. A People s behavior is differ from different situation. For Ex. A function behavior is different from different situation then we need function overloading. Function overloading not only implements polymorphism but also reduce number of comparisons in a programs.
Declaration and Definition The key to function overloading is a function s argument list which is also known as the function signature. It is the signature, not the function type that enables function overloading. void square( int x, float y); void cube( int k); void prnsqr( int i);
Cont.. After declaring overloading function you must define them separately. void prnsqr( int i) { cout<< Integer <<i<< s square is <<i*i; }
Restrictions on Overloaded function Any two function in a set of overload functions must have different argument list. Overloaded function with argument list of same type, based on return type alone is an error. Member function cannot be overloaded on the basis one being static and the other nonstatic.
Finding the best match A call to an overloaded function is resolved to a particular instance of the function through a process known as argument matching. For this there are three possible case. A match A match is found for the function call. No match No match is found for the function call. Ambiguous match More than one defined instance match for the function call.
Search for an exact match If the type of the actual argument exactly matches the type of one defined instance, the compiler invokes the particular instance. Ex- void afun (int); // overload function; void afun (double) // overload function; afun(0); // exact match call afun(int); Because 0 is the type of int. Thus exactly matches.
A match through promotions. If no exact match is found, an attempt is made to achieve a match through promotion of the actual argument. Ex- void afun(int); // overload function; void afun (float) // overload function; afun( c ); // match through promotions. Call afun(int); Because c is the type of char and thus protmoted to type int after no exact match is found.
A match through application of Standard C++ conversion rules. If no exact match or match through a promotion is found, an attempt to made to achieve a match through a standard conversion of the actual argument. void afun(char); void afun(double); afun(71); //Match through standard conversion called afun(double); 71 is converted to double type value;
Cont.. But if the actual argument may be converted to multiple formal argument types, the compiler will generate an error. void afun(long); void afun(double); afun(35); // Error ambiguous match; Here 35 can be converted into both logn type and double type;
A match through application of a user defined conversion; If all the steps fail then the compiler will try the user defined conversion in the combination with integral promotions and built in conversion to find the unique match; Any function whether it is a class member or just an ordinary function can be overloaded in C++;
Default Argument Vs Overloading Using default argument gives the appearance of overloading, because the function may be called with optional number of argument. float amount (float principal, int rate = 0.5, int time =2) Now this function may be called as follows; cout<<amount(5000);//pr=5000 rate=0.5 time=2 cout<<amount(5000,2); //pr=5000 rate=2 time=2 cout<<amount(5000,3,3); //pr=5000 rate=3 time=3