Image

C++ - Functions - 6. Function Overloading

6. Function Overloading

A function having same name but different arguments then such type of function is called as function overloading but in the same class.

e.g 1: WAP to understand function overloading
#include<iostream.h>
#include<conio.h>
void test(int);
void test(float);
void test(int,float);
void main()
{
 int a=6;
 float b=3.1452;
 clrscr();
 test(a);
 test(b);
 test(a,b);
getch();
}
void test(int a){
 cout<<"\nInteger value "<<a;
}
void test(float b){
 cout<<"\nFloat value   "<<b;
}
void test(int a,float b){
 cout<<"\nInteger value "<<a;
 cout<<"\nFloat value   "<<b;
}
O/P
Integer value 6
Float value 3.1452
Integer value 6
Float value 3.1452
e.g 2: WAP to return absolute value of variable using function overloading
#include<iostream.h>
#include<conio.h>
void absolute(int);
void absolute(float);
void absolute(int i){
  if(a<0)
    i=-i;
 cout<<"\n value = "<<i;
}
void absolute(float f){
 if(f<0)
   f=-f;
 cout<<"\n value = "<<f;
}
void main()
{
 int a=-6;
 float b=-5.5;
 clrscr();
 absolute(a);
 absolute(b);
 getch();
}