Class 12 Sumita Arora C++ ch03 Function Overloading
Class 12 Sumita Arora C++ ch03 Function Overloading
Class 12 Sumita Arora C++ ch03 Function Overloading
Function Overloading
SHORT ANSWER QUESTIONS
1. How does the compiler interpret more than one definitions having same name? What steps does it follow to
distinguish these?
Ans. The compiler will follow the following steps to interpret more than one definitions having same name:
(i) if the signatures of subsequent functions match the previous function’s, then the second is treated as a re-
declaration of the first.
(ii) if the signature of the two functions match exactly but the return type differ, the second declaration is treated as
an erroneous re-declaration of the first and is flagged at compile time as an error.
(iii) if the signature of the two functions differ in either the number or type of their arguments, the two functions are
considered to be overloaded.
2. Discuss how the best match is found when a call to an overloaded function is encountered? Give example(s) to
support your answer.
Ans. In order to find the best possible match, the compiler follows the following steps:
1. Search for an exact match is performed. If an exact match is found, the function is invoked. For example,
2. If an exact match is not found, a match trough promotion is searched for. Promotion means conversion of integer
types char, short, enumeration and int into int or unsigned int and conversion of float into double.
3. If first two steps fail then a match through application of C++ standard conversion rules is searched for.
4. If all the above mentioned steps fail, a match through application of user-defined conversions and built-in
conversion is searched for.
For example,
void afunc(int);
void afunc(char);
void afunc(double);
afunc(471); //match through standard conversion. Matches afunc(int)
3. Discuss the benefits of constructor overloading. Can other member function of a class be also overloaded? Can a
destructor be overloaded? What is your opinion?
Ans. Constructor overloading are used to increase the flexibility of a class by having more number of constructors for a
single class. By this we can initialize objects more than one way.
Yes, Other member function of a class can also be overloaded.
A destructor cannot be overloaded.
4. Write the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used
in the following program jointly illustrated by the functions [I] to [IV]:
#include<iostream.h>
void Line() //Fuction [I]
{ for(int L=1;L<=80;L++) cout<<"-";
cout<<endl;
}
void Line(int N) //Fuction [II]
{ for(int L=1;L<=N;L++) cout<<"*";
cout<<endl;
}
void Line(char C,A,int N) //Fuction [III]
{ for(int L=1;L<=N;L++) cout<<C;
cout<<endl;
}
void Line(int M,int N) //Fuction [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);
}
Ans. Output:
####
9 18 27
The name of feature of Object Oriented Programming used in the above program jointly illustrated by the functions
[I] to [IV] is known as ‘function overloading’.
5. Here are some desired effects. Indicate whether each can be accomplished with default arguments, with function
overloading, with both, or which neither. Provide appropriate prototypes.
(i) repeat (10, ‘-‘) displays the indicated character (‘-‘ in this case) given number of times (10 here). While repeat()
displays ‘*’ character 12 times. Also repeat(‘#’) displays the given character (‘#’ here) 12 times and repeat (7)
displays ‘*’ given no of times (7 here).
(ii) average (4,7) returns int average of two int arguments, while average (4.0, 7.0) returns the double average of
two double values.
(iii) mass (density, volume) returns the mass of an object having a density of density and a volume of volume,
while mass (density) returns the mass having a density of density and a volume of 1.0 cubic meters. All quantities
are type double.
(iv) average (4,7) returns an int average of the two int arguments when called is one file, and it returns a double
average of the two int arguments when called in a second file in the same program.
(v) handle (a-character) returns the reversed case of the passed character or prints it twice depending upon
whether you assign the return value to it or not.
Ans. (i) It can be accomplished with function overloading.
void repeat(int n, char c);
void repeat();
void repeat(char c);
void repeat(int n);
Function overloading can handle all possible argument combinations. It overcomes the limitation of default
argument but also compiler is saved from the trouble of testing the default value.
Example:
float area(float a)
{
return a*a;
}
float area(float a,float b)
{
retur a*b;
}
10. Raising a number n to a power p is the same as multiplying n by itself p times. Write as overloaded 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 for p in case p is omitted in the function
call.
Ans. double power(double n,int p=2)
{
double res=pow(n,p);
return res;
}
int power(int n,int p=2)
{
int res=pow(n,p);
return res;
}
LONG ANSWER QUESTIONS
Using above information, write a complete C++ program that lets you sell, purchase or order a specific item or
items.
Ans. Code snippet of the above problem is given below use it and complete the program.