Function Overloading
Function Overloading
OVERLOADING
A PART OF POLYMORPHISM
CONTEXT
What is function overloading.
Why we use function overloading.
Way of implementation
Area(breadth ,
Area(radius) Area(side)
width)
Breadth * width
(side)^2
2* radius
MERITS & DEMERITS
Merits:
Consistency
Readability
Better performance
Demerits
Sometime ambiguous
Ways to overload a function
By changing number of Arguments.
class printData
{
public:
void print(int i)
{ cout << "Printing int: " << i << endl;
}
void print(double f)
{
cout << "Printing float: " << f << endl;
}
void print(char* c)
{ cout << "Printing character: " << c << endl;
}
};
int main(void)
{
printData pd;
// Call print to print integer pd.print(5);
// Call print to print float pd.print(500.263);
}
character pd.print("Hello C++"); return 0; }When the
above code is compiled and executed, it produces the
following result:
Printing int: 5 Printing float: 500.263 Printing character:
Hello C++.