Function Overloading: -> Using same function name that can perform different task.
eg. int add(int a,int b);
int add(int a,int b,int c);
float add(float a,float b);
Here,we have same function name add but we have different parameters to perform calculations
1. Program to find maximum of 2 or 3 numbers by function overloading.
#include<iostream.h>
#include<conio.h>
void maximum(int a,int b,int c);
void maximum(int a,int b);
void main(){
int a,b,c,n;
cout<<"Enter either two or three to compare:"<<endl;
cin>>n;
if(n==2)
{
maximum(a,b);
}
else if(n==3)
maximum(a,b,c);
else
{
cout<<"Enter Valid number: 2 or 3"<<endl;
}
getch();
}
void maximum(int a,int b)
{
cout<<"Enter the 2 numbers to compare:"<<endl;
cin>>a>>b;
if(a>b)
cout<<a<<" is maximum"<<endl;
else
cout<<b<<" is maximum"<<endl;
}
void maximum(int a,int b,int c)
{
cout<<"Enter the 3 numbers to compare:"<<endl;
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
cout<<a<<" is maximum"<<endl;
else
cout<<c<<" is maximum"<<endl;
}
else if(b>c)
cout<<b<<" is maximum"<<endl;
else
cout<<c<<" is maximum"<<endl;
}
Output:
2.Program to find the area of 3 objects: cube,cylinder and rectanglur box using function overloading.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define pi 3.14
void area(float l,float b);
void area(int l);
void area(float r);
void main()
{
float l,b,r,n;
cout<<"Enter any number from 1-3 to find Area of following optionsin \n 1. Rectangular box \n 2. Cube \n 3. Cylinder"<<endl;
cin>>n;
if(n==1)
area(l,b);
else if(n==2)
area(l);
else if(n==3)
area(r);
else
cout<<"Enter number between 1 - 3";
getch();
}
void area(float l,float b)
{
float a;
cout<<"Enter length of the rectangle:";
cin>>l;
cout<<"Enter breadth of the rectangle:";
cin>>b;
a=l*b;
cout<<"Area of the rectangular box is: "<<a;
}
void area(int l)
{
float a;
cout<<"Enter length of the cube:";
cin>> l;
a=pow(1,2);
cout<<"Area of the cube is: "<<a;
}
void area(float r)
{
float a;
cout<<"Enter radius of the cylinder:";
cin>>r;
a=pi *pow(r,2);
cout<<"Area of the cylinder is: "<<a;
}
Output:
3. Program to calculate the volume of cylinder,cube and rectangular bofunction Overloading.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define pi 3.14
void volume(float l,float b,float h);
void volume(float l);
void volume(float r, float h);
void main()
{
float l,b,r,h,a,i;
for(i=0;i<3;i++)
{
cout<<"Enter number from 1-3 to perform calculations in \n 1. Rectangular Box \n 2 Cube \n 3. Cylinder"<<endl<<endl;
cin>>a;
if(a==1)
volume(l,b,h);
else if(a==2)
volume(l);
else if(a==3)
volume(r,h);
else
cout<<"Enter valid number not exceeding 3";
}
getch();
}
void volume(float l,float b,float h)
{
float v;
cout<<"Enter length of the rectangle:";
cin>>l;
cout<<"Enter breadth of the rectangle:";
cin>>b;
cout<<"Enter height of the rectangle:";
cin>>h;
v=l*b*h;
cout<<"Volume of the rectangular box is: "<<v<<endl;
}
void volume(float l)
{
float v;
cout<<"Enter length of the cube.";
cin>>l;
v=pow(l,3);
cout<<"Volume of the cube is: "<<v<<endl;
}
void volume(float r, float h)
{
float v;
cout<<"Enter radius of the cylinder.";
cin>>r;
cout<<"Enter height of the cylinder:",
cin>>h;
v=pi*pow(r,2)*h;
cout<<"Volume of the cylinder is: "<<v<<endl;
}
Output:
4. Program to calculate the volume and surface area of Sphere using function overloading.
Comments
Post a Comment