Function Overloading in CPP

When a class contains more than one function with the same name but with different parameters is called the method overloading. By this, a single function can perform multiple tasks that depend upon the number of parameters.

#include<iostream.h>
#include<conio.h>

class A
{
public:
void fun()
{
cout << "fun called" << endl;
}
void fun(int x)
{
cout << x << endl;
}
void fun(int x,int y)
{
cout << x+y << endl;
}
void fun(int x,char str[])
{
cout << x <<"\t"<< str << endl;
}
};
void main()
{
clrscr();
A a1;
a1.fun();
a1.fun(100);
a1.fun(100,200);
a1.fun(100,"tanmay");
getch();
}

Output:
fun called
100
300
100 tanmay

 

Leave a Reply