CPP Program to Implement the Concept of Function Overloading

Aim :

To write a C++ program to implement the concept of Function Overloading

Function Overloading in C++

  • You can have multiple definitions for the same function name in the same scope.
  • The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.
  • You cannot overload function declarations that differ only by return type.

Algorithm:

  1. Start the program.
  2. Create the class with variables and functions.
  3. In the main (), declare the variables.
  4. Use Volume () function to find the volume of cylinder, cube and rectangle.
  5. Define the volume () function with necessary arguments for calculation.
  6. The values that are passed inside the function call will be matched with the definition part and appropriate calculations are done.
  7. All three volumes are displayed accordingly by display() function.
  8. Stop the program.

Program:

#include<iostream>
using namespace std;
double area (int r)
{
return 3.14*r*r;
}
int area(int b,int h)
{
return b*h;
}
void area(int h,int b,float* res)
{
*res=0.5*h*b;
}	
int main()
{
int choice,i=0;	
while(i<1)
{
cout<<"\n1.CIRCLE\n2.RECTANGLE\n3.TRIANGLE\n4.EXIT\n";
cin>>choice;
if(choice==1)
{
int r;
cout<<"ENTER THE RADIUS\n";
cin>>r;
cout<<"AREA OF CIRCLE: "<<area(r)<<endl;
}
else if(choice==2)
{
int b,h;
cout<<"ENTER THE HEIGHT AND BREADTH\n";
cin>>b>>h;
cout<<"AREA OF RECTANGLE: "<<area(b,h)<<endl;
}
else if(choice==3)
{
int b,h;
float result;
cout<<"ENTER THE HEIGHT AND BREADTH\n";
cin>>b>>h;
area(h,b,&result );
cout<<"AREA OF TRIANGLE:"<<result<<endl;
}
else {
i=5;
cout<<"PROGRAM EXITED\n";
}
}
return 0;
}

Execution:

Input
1 3 2 5 4 3 6 2 4

Output

1.CIRCLE
2.RECTANGLE
3.TRIANGLE
4.EXIT
ENTER THE RADIUS
AREA OF CIRCLE: 28.26

1.CIRCLE
2.RECTANGLE
3.TRIANGLE
4.EXIT
ENTER THE HEIGHT AND BREADTH

AREA OF RECTANGLE: 20

1.CIRCLE
2.RECTANGLE
3.TRIANGLE
4.EXIT
ENTER THE HEIGHT AND BREADTH

AREA OF TRIANGLE:6

1.CIRCLE
2.RECTANGLE
3.TRIANGLE
4.EXIT
PROGRAM EXITED

Result:

Thus, the Implement the Concept of Function Overloading was executed successfully.

Leave a Comment