#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int a,length,b;
float radius,base,ht;
cout<<"enter one side of the square";
cin>>a;
cout<<"enter length and breadth the rectangle:";
cin>>length>>b;
cout<<"enter the radius of the circle:";
cin>>radius;
cout<<"enter the base and height of the triangle:";
cin>>base>>ht;
cout<<"Area of square = "<<area(a);
cout<<"\nArea of rectangle = "<<area(length,b);
cout<<"\nArea of circle = "<<area(radius);
cout<<"\nArea of triangle = "<<area(base,ht);
}
int area(int a)
{
return(a*a);
}
int area(int length,int b)
{
return(length*b);
}
float area(float radius)
{
return(3.14*radius*radius);
}
float area(float base,float ht)
{
return((base*ht)/2);
}
Output
SOLUTION TO THE ABOVE QUESTION
In this question, we will include a menu on the main method to ask the user the operation he/she wants to perform, finding the area of a circle, rectangle, triangle or a square. Based on the option the user enters he/she will be prompted for the necessary parameters and the area of that shape be displayed together with its properties.
SOLUTION CODE
#include<iostream>
using namespace std;
//Function prototypes implimenting function overloading
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
//Menu for the program
cout<<"Enter the shape you want to calculate area for:"<<endl;
cout<<"1.Square"<<endl;
cout<<"2.Rectangle"<<endl;
cout<<"3.Circle"<<endl;
cout<<"4.Triangle"<<endl;
int option;
cout<<"Enter your option: ";
cin>>option;
if(option==1)
{
int a;
cout<<"Enter one side of the square: ";
cin>>a;
cout<<"Area of the square = "<<area(a);
}
else if(option==2)
{
int length,breadth;
cout<<"Enter length of the Rectangle: ";
cin>>length;
cout<<"Enter breadth of the Rectangle: ";
cin>>breadth;
cout<<"\nArea of the rectangle = "<<area(length,breadth);
}
else if(option==3)
{ float radius;
cout<<"Enter the radius of the circle: ";
cin>>radius;
cout<<"\nArea of the circle = "<<area(radius);
}
else if(option == 4)
{
float base,height;
cout<<"Enter the base of the triangle:";
cin>>base;
cout<<"Enter the height of the triangle:";
cin>>height;
cout<<"\nArea of the triangle = "<<area(base,height);
}
else
{
cout<<"You have entered an invalid option please try again"<<endl;
}
}
//definition of the functions
int area(int a)
{
return(a*a);
}
int area(int length,int b)
{
return(length*b);
}
float area(float radius)
{
return(3.14*radius*radius);
}
float area(float base,float ht)
{
return((base*ht)/2);
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment