1.

Compare the usefulness of default argument and function overloading, supporting your answer with appropriate examples.

Answer»

Default values can be provided the function prototype itself and the function may be called even if an argument is missing. But there is one limitation with it, if you want to default a middle argument, then all the argument on its right must also be defaulted. For instance, consider the following function prototype:

float amount(float p,

int time=2, float rate=0.08);

cout<<amount(2000,0.13);

Here, C++ will take 0.13 to be the argument value for time and hence invoke amount() with values 2000, 0(0.13 converted to int) and 0.08

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;

}



Discussion

No Comment Found

Related InterviewSolutions