Explain function overloading?

Submitted by: Administrator
Function overloading is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either different types of parameters or a different number of parameters. It is only through these differences that the compiler knows which function to call in any situation.

Consider following program which overloads MyFunction() by using different types of parameters:

#include <iostream>
Using namespace std;
int MyFunction(int i);
double MyFunction(double d);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(5.4)<<”n”; //calls MyFunction(double d);
return 0;
}
int MyFunction(int i)
{
return i*i;
}

double MyFunction(double d)
{
return d*d;
}

Now the following program overloads MyFunction using different no of parameters

#include <iostream>
Using namespace std;
int MyFunction(int i);
int MyFunction(int i, int j);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(1, 2)<<”n”; //calls MyFunction(int i, int j);
return 0;
}

int MyFunction(int i)
{
return i;
}

double MyFunction(int i, int j)
{
return i*j;
}

Please note, the key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types can not be overloaded.
Submitted by: Administrator

Read Online C++ Operator Overloading Job Interview Questions And Answers