Function overloading program to calculate area using class in c++
Answers
Answer:
C++ program to find the area of circle & rectangle using function...
Step 1: Start.
Step 2: Declare a class over with data members and member functions.
Step 3: Define and declare the function volume() to find the volume of circle and rectangle.
Step 4: Create an object for the class over.
Step 5: Read the radius of the circle.
✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌✌
Explanation:
Q. Write a C++ program to calculate the area of triangle, rectangle and circle using constructor overloading. The program should be menu driven.
Answer:
Constructors have same name of the class but with number of arguments. Constructors can be overloaded.
Following program is displaying the work of overloaded constructors.
#include<iostream>
#include<math.h>
#include<cstdlib>
using namespace std;
class area
{
float ar;
public:
area(float r)
{
ar=3.14*r*r;
}
area(float l, float b)
{
ar=l*b;
}
area(float a, float b, float c)
{
float s;
s=(a+b+c)/2;
ar=s*(s-a)*(s-b)*(s-c);
ar=pow(ar,0.5);
}
void display()
{
cout<<"\n Area : "<<ar;
}
};
int main()
{
int ch;
float x, y, z;
do
{
<<"\n\n 1. Area of Circle";
cout<<"\n 2. Area of Rectangle";
cout<<"\n 3. Area of Triangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1 :
{
cout<<"\n Enter Radius of the Circle : ";
cin>>x;
area a1(x); //Class area, object is created : a1
a1.display();
}
break;
case 2 :
{
cout<<"\n Enter Length and Breadth of the Rectangle : ";
cin>>x>>y;
area a2(x,y);
a2.display();
}
break;
case 3 :
{
cout<<"\n Enter Sides of the Triangle : ";
cin>>x>>y>>z;
area a3(x,y,z);
a3.display();
}
break;
case 4 :
exit(0);
default :
cout<<"\n\n Invalid Choice ...";
}
} while(ch!=4);
return 0;
}