KEMBAR78
Function Overloading in C++ programming language | PPTX
Function Overloading
• Overloading🡪 refers to the use of same thing
for different purpose
• Function overloading-> We can use the same
function name to perform a verity of different
task
• Provided that the function should vary the
number or types of the arguments
Using the concept of function ovaerloading
• We can design functions with same function
name but with different argument list
• Difference in
• Type or
• number of arguments
Examples
function prototype /declaration
• int add(int a,int b);
• int add(int a,int b,int c);
• float add(float a,int b);
Function call
• add(5,10);
• add(5,6,3);
• add(4.6,7.9);
Function definition
int add (int a, int d)
{
int c ;
c=a+d;
return c
}
int add (int a, int d,int e)
{
int c ;
c=a+d+e;
return c
}
******************
float add (float a, int d)
{
float c ;
c=a+d;
return c
}
• The function would perform different
operations depending on the argument list in
the function call
#include<iostream.h>
float volume(float l);
float volume(float r,float h);
float volume(float l,float b,float h);
void main()
{
float l,r,b,h,t;
clrscr();
cout<<“nEnter the Length of Cube: n”;
cin>>l;
t=volume(l);
cout<<“nnVolume of Cube: “<<t;
cout<<“nnEnter the Radius & Hieght of
Cylinder: n”;
cin>>r>>h;
t=volume(r,h);
cout<<“nnVolume of Cylinder: “<<t;
cout<<“nnEnter the Length,Breadth & Hieght of
Rectangle: n”;
cin>>l>>b>>h;
t=volume(l,b,h);
cout<<“nnVolume of Rectangle: “<<t;
getch();
}
float volume(float l) //Cube
{
float f;
f=l*l*l;
return f;
}
float volume(float r,float h)
//Cylinder
{
float vl=3.14*r*h;
return vl;
}
float volume(float l,float b,float h)
{ float f=l*b*h;
return f;
}
• Function Overloading is an example of
compile time polymorphism

Function Overloading in C++ programming language

  • 1.
    Function Overloading • Overloading🡪refers to the use of same thing for different purpose • Function overloading-> We can use the same function name to perform a verity of different task • Provided that the function should vary the number or types of the arguments
  • 2.
    Using the conceptof function ovaerloading • We can design functions with same function name but with different argument list • Difference in • Type or • number of arguments
  • 3.
    Examples function prototype /declaration •int add(int a,int b); • int add(int a,int b,int c); • float add(float a,int b);
  • 4.
    Function call • add(5,10); •add(5,6,3); • add(4.6,7.9);
  • 5.
    Function definition int add(int a, int d) { int c ; c=a+d; return c } int add (int a, int d,int e) { int c ; c=a+d+e; return c } ****************** float add (float a, int d) { float c ; c=a+d; return c }
  • 6.
    • The functionwould perform different operations depending on the argument list in the function call
  • 7.
    #include<iostream.h> float volume(float l); floatvolume(float r,float h); float volume(float l,float b,float h); void main() { float l,r,b,h,t; clrscr(); cout<<“nEnter the Length of Cube: n”; cin>>l; t=volume(l); cout<<“nnVolume of Cube: “<<t; cout<<“nnEnter the Radius & Hieght of Cylinder: n”; cin>>r>>h; t=volume(r,h); cout<<“nnVolume of Cylinder: “<<t; cout<<“nnEnter the Length,Breadth & Hieght of Rectangle: n”; cin>>l>>b>>h; t=volume(l,b,h); cout<<“nnVolume of Rectangle: “<<t; getch(); } float volume(float l) //Cube { float f; f=l*l*l; return f; } float volume(float r,float h) //Cylinder { float vl=3.14*r*h; return vl; } float volume(float l,float b,float h) { float f=l*b*h; return f; }
  • 8.
    • Function Overloadingis an example of compile time polymorphism