FUNCTION OVERLOADING
Why do you think function overloading must be a part of an Object Oriented Language ?
What is function overloading ? Write an example using C++ to illustrate the concept of functionoverloading.
How would you compare default arguments and function overloading ?
SOLUTION. With default arguments, default values can be provided in the function prototype itself and the function may be called even if an argument is missing (provided the default value for the missing argument is present in the prototype). But there is one limitation with it, if you want to default a middle argument, then all the arguments on its right must also be defaulted.
How is matching done in case of overloaded functions ?
SOLUTION. With a function call that uses the name of an overloaded function, the compiler follows the following steps in order to find the best match :
- Search for an exact match is performed. If an exact match is found, the function is invoked.
- If an exact match is not found, a match through promotion is searched for. Promotion means conversion
of integer types char, short, enumeration and int into int or unsigned int (whatever is capable of
representing all the values of the datatype being promoted) and conversion of float into double.
- If first two steps fail then a match through application of C + + standard conversion rules is searched for.
- If all the above mentioned steps fail, a match through application of user-defined conversions and built-in conversions is searched for.
- If all these steps fail, the compiler flashes a message reporting no match.
If more than one definition qualify for the function call, the compiler reports an ambiguous match
Write a C++ program that uses an area( ) function for the calculation of area of a triangle or a rectangle or a square. Number of sides (3 for triangle, 2 for rectangle and 1 for square) suggest about the shape for which area is to be calculated.
Write a C++ program that uses a function to check whether a given number is divisible by another number or not. However, if the second number is missing, the function checks whether the given number is prime or not.
#include <iostream.h>
void Line( )// Function [I]
{for (int L = 1 ; L <= 80; L++) cout < < "-";
cout < endl;
}
void Line (int N)// Function [II]
{for (int L = 1 ; L <= N; L++) cout < "*";
cout < endl;
}
void Line (char C, A, int N)// Function [III]
{for(int L= 1; L<=N; L++) cout < C;
cout < endl;
}
void Line (int M, int N)// Function [IV]
{
for(int L=1;L<=N;L++)cout < M*L; cout < endl;
}
void main ( )
{
int A = 9, B = 4, C = 3;
char K = '#';
Line (K, B);
Line (A, C);
}
Raising a number n to a power p is the same as multiplying n by itself p times. Write as over-loaded function power( ) having two versions for it. The first version takes double n and int p and returns a double value. Another version takes int n and int p returning int value. Use a default value of 2 forp in casep is omitted in the function call.
Write definitions for two versions of an overloaded function. This function's 1st version sum( ) takes an argument, int array, and returns the sum of all the elements of the passed array. The 2nd version of sum( ) takes two arguments, an int array and a character ('E' or '0'). If the passed character is 'E', it returns the sum of even elements of the passed array and if the passed character is '0', it returns the sum of odd elements. In case of any other character, it returns 0 (zero).