Professional Documents
Culture Documents
14 Function Overloading
14 Function Overloading
PDPU
FUNCTION OVERLOADING …
Known as function polymorphism also.
Overloading means using same thing for
different purposes.
Feature of C++ and not C.
Using the concept, we can design a family of
functions with one function name but with
different argument lists.
Compile-Time Polymorphism
FUNCTION OVERLOADING …
The function would perform different
operations depending on the argument list in
the function call.
The correct function to be invoked is
determined by checking the number and type
of the arguments and not from what function
returns.
Function Overloading Example
void main()
{
clrscr();
numbers x;
x.add();
x.add(50,70);
getch();
Please go through
}; the program
“exovrld1.cpp”
Example …
defining class numbers …
class numbers
{
private:
int a , b;
public:
void add (void);
void add (int, int);
};
Example …
defining member functions …
void main()
{
shape square, rect, circle;
square.area(5);
rect.area(10,20);
circle.area(10.00);
getch();
Please go through
}; the program
“exovrld2.cpp”
Example …
Defining Class shape…
class shape
{
private:
int length, breadth; float a, radius;
public:
void area (int); // area of a square.
void area (int, int); // area of a rectangle.
void area (double); // area of a circle.
};
Example …
Defining member functions…
… we write square.area(5.0);
… we write circle.area(5);
… we write another function called
void shape :: area (float) ;
… we write another function called
int shape :: area (int , int);
SUMMARY …