C++ Function Overloading
Overloading:
It refers to the use of the same thing for different purposes. C++ also permits the overloading of functions. It means that we can use the same function name to create functions that perform a variety of different tasks.
Function Overloading in C++:
In Function Overloading, we can design a family of functions with one function name but with different argument lists. The function would perform different operations depending on the argument list in the function call.
Program:
#include<iostream.h> #include<conio.h> int area(int); int area(int, int); float area(float); int main() { cout<<"The area of Square:"<<area(12)<<"\n"; cout<<"The area of Rectangle:"<<area(12,15)<<"\n"; cout<<"The area of Circle:"<<area(7)<<"\n"; return 0; } int area(int s) { return(s*s); } int area(int l, int b) { return(l*b); } float area(float r) { return(3.14*r*r); }
Output:
The area of Square: 144 The area of Rectangle: 180 The area of Circle: 153.86