Anda di halaman 1dari 7

MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI

CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT
CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

1. What is polymorphism? How does function overloading implement polymorphism?

• Solution:
Polymorphism is the ability for a message or data to be processed in more than one form.
Overloading refers to the use of the same things for different purposes. C++ also permits
overloading of functions. This means that we can use the same function name to create functions
w 81
that perform a variety of different tasks. This is known as function polymorphism in OOP.
w 0
2. What is function overloading?
09

• Solution:
w 1
A function name having several definitions that are differentiable by the number or types of their
arguments. This is called function overloading.
.m 44
3. What is the significance of function overloading in C++?

• Solution:
Function overloading not only implements polymorphism but also reduces number of comparisons in
a program so that the program runs fast. The overloaded functions can simplify the programmer’s
at 31
life by reducing the number of function names to be remembered.

4. What is the role of the function’s signature in disambiguation process?


he 5
• Solution:
When a function is declared more than once in a program, the compiler will interpret the second
declaration as:
• If the signatures of the functions match, then the second function is treated as a re-
m
declaration of the first.
• If the functions differ in their return type, only then, the second declaration is considered as
an erroneous re-declaration. For example:
at
• float sum(float f);
• double sum(float x);//error
• Note: Functions with the same signature and same name but different return types
are not allowed in C++.
ic
• float sum(float f);//different signatures, hence allowed
• double sum(double x);
.
If the signatures of the functions differ in either the number or type of their arguments, then the
in
functions are considered to be overloaded.

5. Discuss the use of extended function names in function overloading.

• Solution:
We can overload a function in C++ with the same name but different signatures. The signature
differs by the number of arguments or by the type of arguments, or both. When we use the concept
of function overloading, we can design a family of functions with one function name but with different
signatures.

1
CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA,
DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi
EMAIL:1 rktctb@yahoo.com web site www.mathematic.in
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI
CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT
CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

6. What factors make two definitions with the same function name significantly
different?

• Solution:
When two functions have the same number and type of arguments in the same order, then they
have the same signature. The variable name doesn’t matter. Example:

Void sum(int a,int b);


w 81
Void sum(int x,int y);
w 0
7. List the steps involved in finding the best match for a function call.
09
w 1
• Solution:
The compiler first tries to find an exact match in which the types of the actual arguments are the
same and that function is used.
.m 44
If an exact match is not found, the compiler uses the integral promotions to the actual arguments.

When either of them fails, it tries to use the built-in conversions.


at 31
8. When should default arguments be preferred over function overloading vice-versa?
he 5
• Solution:
Default arguments will be preferred over function overloading when exactly one function definition is
executed. But in case of function overloading more than one function definition is executed
depending upon the argument combination where the function works differently.
m
9. What are the major limitations of default arguments?

• Solution:
at
Default arguments will not work for all combination of arguments.

Default function is the only one function definition.


ic
The compilation speed is less in case of default arguments.
. in
10. Can a programmer think of any limitations of function overloading? List them.

• Solution:
The programmer should not use function overloading just for the sake of program understandability.
The functions performing the same task with a set of different argument lists should be overloaded.

11. Write overloaded prototypes of inquote(), a function that displays its argument in
double quotation marks. Write three versions: one for an int argument,one for double
argument and one for a char argument.

Solution:

2
CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA,
DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi
EMAIL:2 rktctb@yahoo.com web site www.mathematic.in
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI
CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT
CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

for an int argument  void inquote(int x);

for double argument void inquote( double s);

for a char argument  void inquote(char c);


w 81
12. Write overloaded prototype of themax(), a function that displays maximum value of
its arguments. Write three versions" one for single int value, one for two int values , and
one for an array of int values.
w 0
Solution:
09

for single int value  void themax(int x);


w 1
for two int values void themax(int i,int j);
.m 44
for an array of int values void themax(int *a);

13. Write overloaded prototype of volume(), a function that returns volumes of different
structures. Write three versions: one for cube’s volume that takes one float side of the
at 31
cube, one for cylinder’s volume that takes float radius and float height of a cylinder, and
one for rectangle that takes float length, float breadth and float height of the box.
he 5
Solution:
for cube’s volume  float volume ( float side);

For cylinders’ volume float volume (float radius, float height);


m
For rectangle float volume (float length, float breadth, float height);

14. How does the compiler interpret more than one definition having the same name?
at
What steps does it follow to distinguish these?

Solution:
ic
When a function is declared more than once in a program, the compiler will interpret the second
declaration as :
• If the signatures of the functions match, then the second function is treated as a re-
.
declaration of the first.
in
• If the functions differ in their return type, only then the second declaration is considered as an
erroneous re-declaration. For example:
• float sum(float f);
• double sum(float x);//error
• Note: functions with the same signature and same name but different return types
are not allowed in C++.
• float sum(float f);//different signatures, hence allowed
• double sum(double x);

If the signatures of the functions differ in either the number or type of their arguments, then the
functions are considered to be overloaded.

3
CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA,
DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi
EMAIL:3 rktctb@yahoo.com web site www.mathematic.in
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI
CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT
CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

Restrictions on overloaded functions

1. The overloaded functions must have different argument lists.


2. The function overloading is only based on the return type alone which is an error.

Typedef declaration and enumerated data type does not define new types.
w 81
15. Discuss how the best match is found when a call to an overloaded function is
encountered? Give examples to support your answer.
w 0
Solution:
A function call first matches the prototype having the same number of and type of arguments and
09

then calls the appropriate function for execution. A best match must be unique.
w 1
Steps involved in finding the best match.
.m 44
1. The compiler first tries to find an exact match in which the types of the 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:
at 31
char to int
he 5
float to double

to find a match

3. When either of them fails or tries to use the built-in conversions ( the implicit assignment
m
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.
For example:
at
long sum(long n);

double sum(double x);


ic
a function call such as
.
sum(20)
in
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 sum() should be called.

4. If all the steps fail, then the compiler will try the user-defined conversion in combination with the
integral promotions and built-in conversions to find the unique match.

16. Here are some desired effects. Indicate whether each can be accomplished with
default arguments with function overloading with both, or with neither. Provide appropriate
prototypes.

4
CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA,
DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi
EMAIL:4 rktctb@yahoo.com web site www.mathematic.in
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI
CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT
CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

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
number 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
w 81
density and 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
w 0
in the same program.
v. handle (a-character) returns the reversed case of the passed character or prints it
09

twice depending upon whether you assign the return value to it or not.
w 1
Solution:
i. void repeat( int i =10,char a= ‘-‘);
.m 44
void repeat( );

void repeat( char a= ‘-‘);


at 31
void repeat( int i =7);

ii. int average(int x=4,int y=7);


he 5
double average(double x=4.0,double y =7.0);

iii. double mass(double density=1.0, double volume=1.0);


m
double mass(double density =1.0);

iv. int average(int x=4,int y=7);


at
double average(int x=4,int y=7);
ic
v. char handle( char a);

void handle(char a);


. in
17. Write function definition for question 16 of type A. Remember that the cube’s volume
is side3. Cyclinder’s volume is ∏r2h. Rectangular box’s volume is length x breadth x height.

Solution:
float volume( float side)

{ return (side*side*side);}

float volume(float radius, float height)

5
CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA,
DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi
EMAIL:5 rktctb@yahoo.com web site www.mathematic.in
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI
CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT
CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

{ return(3.14*radius*radius*height);}

float volume(float length, float breadth,float height)

{return(length * breadth* height);}

18. Compare the usefulness of default arguments and function overloading. Support your
w 81
answer with appropriate examples.

Solution:
w 0
C++ allows us to call a function without specifying all its arguments. In such cases, the function
assigns a default value to the parameter which does not have a matching argument in the function
09

call. Default values are specified when the function is declared. The compiler looks at the prototype
w 1
to see how many arguments a function uses and alerts the program for possible default values. For
example:
.m 44
float area(int length,int height,int degree =90);

The default value is specified in a systematic manner as variable initialization. The function call is
made as follows:
at 31
float tri=area(10,20);// one argument missing

Passes the value 10 to length and 20 to height and lets the function default value of 90 for degree.
But in the call
he 5
float tri =area(20,10,45);//no argument missing

passes explicit value 45 to degree


m
The use of default arguments gives the appearance of overloading, because the function may be
called with optional number of arguments. But it is always the same function and is only called what
may be the number of arguments.
at
A default argument is checked for type at the time of declaration and evaluated at the time of call.
An important point to note is that the default arguments must be added from right to left.
ic
Examples:
.
int mul(int I,int j=5,int k=6);//valid
in
int mul(int i=5,int j);//invalid

int mul(int i=4,int j,int k=8);//in valid

int mul9int i=5,int j=6,intk =8);//valid

Note: Default arguments are useful in situations where some arguments always have the same
value.

The advantages of function overloading over default arguments:

6
CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA,
DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi
EMAIL:6 rktctb@yahoo.com web site www.mathematic.in
MATHEMATIC CENTER D96 MUNIRKA VILLAGE NEW DELHI 110067 & VIKAS PURI NEW DELHI
CONTACT FOR COACHING MATHEMATICS FOR 11TH 12TH NDA DIPLOMA SSC CAT SAT CPT
CONTACT FOR ADMISSION GUIDANCE B.TECH BBA BCA, MCA MBA DIPLOMA AND OTHER COURSES 09810144315

a. Default arguments will not work for all combination of arguments but function overloading
does.
b. In function overloading there are multiple function definitions but, in case of default function
- there is only one function definition.
c. The compilation speed is much more in the case of function overloading.

19. Raising a number n to a power p is the same as multiplying n by itself p times. Write
an overloaded function power() having two versions for it. The first version power() takes
w 81
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.
w 0
Solution:
09

Version 1: double power(double n, double p);


w 1
Version 2: int power(int n,int p=2);
.m 44
at 31
he 5
m
at
ic
. in

7
CONTACT FOR MATHEMATICS GROUP/HOME COACHING FOR CLASS 11TH 12TH BBA,BCA,
DIPLOMA CPT CAT SSC AND OTHER EXAMS Also home tutors for other subjects in south and west delhi
EMAIL:7 rktctb@yahoo.com web site www.mathematic.in

Anda mungkin juga menyukai