Write a C++ program to implement function overloading.
Algorithm:
Step 1: Create a class.
Step 2: Declare the data members.
Step 3: Declare multiple member functions with the same name.
Step 4: Create an object for the class inside the main function.
Step 5: Call the overloaded functions by comparing the arguments list.
Step 6: Print the output.
Step 7: Stop the program.
Program Code:
//Function Overloading
#include<iostream.h>
#include<conio.h>
class volume
{
int l,b,h,a1,b1,c1;
int vol1,vol2,vol3;
public:
void get()
{
cout<<"Enter the value of l,b,h:";
cin>>l>>b>>h;
vol1=l*b*h;
}
void get(int x,int y,int z)
{
l=x;b=y;h=z;
vol2=l*b*h;
}
void get(int x,int y=12)//Default Argument
{
a1=x;b1=y;
vol3=a1*b1;
}
void display()
{
cout<<"The volumes are:";
cout<<"\nVol1="<<vol1<<"\nVol2="<<vol2<<"\nVol3="<<vol3;
}
};
void main()
{
clrscr();
volume v;
v.get();
v.get(5,6,7);
v.get(5);
v.display();
getch();
}
Output:
Enter the value of l,b,h:10
20
30
The volume is:
Vol1=6000
Vol2=210
Vol3=60
Algorithm:
Step 1: Create a class.
Step 2: Declare the data members.
Step 3: Declare multiple member functions with the same name.
Step 4: Create an object for the class inside the main function.
Step 5: Call the overloaded functions by comparing the arguments list.
Step 6: Print the output.
Step 7: Stop the program.
Program Code:
//Function Overloading
#include<iostream.h>
#include<conio.h>
class volume
{
int l,b,h,a1,b1,c1;
int vol1,vol2,vol3;
public:
void get()
{
cout<<"Enter the value of l,b,h:";
cin>>l>>b>>h;
vol1=l*b*h;
}
void get(int x,int y,int z)
{
l=x;b=y;h=z;
vol2=l*b*h;
}
void get(int x,int y=12)//Default Argument
{
a1=x;b1=y;
vol3=a1*b1;
}
void display()
{
cout<<"The volumes are:";
cout<<"\nVol1="<<vol1<<"\nVol2="<<vol2<<"\nVol3="<<vol3;
}
};
void main()
{
clrscr();
volume v;
v.get();
v.get(5,6,7);
v.get(5);
v.display();
getch();
}
Output:
Enter the value of l,b,h:10
20
30
The volume is:
Vol1=6000
Vol2=210
Vol3=60
Post A Comment:
0 comments: