C++ program to calculate area of rectangle using constructor
Asked by admin @ in Computer Science viewed by 350 People
C++ program to calculate area of traingle circle and rectangle using constructor overloading
Asked by admin @ in Computer Science viewed by 350 People
C++ program to calculate area of traingle circle and rectangle using constructor overloading
Answered by admin @
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;
}
Asked by admin @ in Computers and Technology viewed by 313 persons
Asked by admin @ in Computers and Technology viewed by 355 persons
Asked by admin @ in Computer Science viewed by 404 persons
Asked by admin @ in Computer Science viewed by 362 persons
Asked by admin @ in General viewed by 416 persons
Asked by maham237 @ in Mathematics viewed by 340 persons
Asked by pranav94 @ in Engineering viewed by 470 persons
Asked by admin @ in Mathematics viewed by 376 persons
Asked by admin @ in Mathematics viewed by 397 persons
Asked by admin @ in Mathematics viewed by 465 persons
Asked by admin @ in Mathematics viewed by 291 persons
Asked by admin @ in Mathematics viewed by 282 persons
Asked by admin @ in Physics viewed by 284 persons
Asked by admin @ in Computer Science viewed by 332 persons
Asked by admin @ in Computer Science viewed by 335 persons
Asked by admin @ in Computer Science viewed by 19019 persons
Asked by admin @ in Computer Science viewed by 15806 persons
Asked by admin @ in Computer Science viewed by 10971 persons
Asked by admin @ in Computer Science viewed by 10401 persons
Asked by admin @ in Computer Science viewed by 8135 persons
Asked by admin @ in Computer Science viewed by 7841 persons
Asked by admin @ in Computer Science viewed by 6486 persons
Asked by admin @ in Computer Science viewed by 5788 persons
Asked by admin @ in Computer Science viewed by 5691 persons
Asked by admin @ in Computer Science viewed by 5474 persons
Asked by admin @ in Computer Science viewed by 4980 persons
Asked by admin @ in Computer Science viewed by 4970 persons
Asked by admin @ in Computer Science viewed by 4682 persons
Asked by admin @ in Computer Science viewed by 4487 persons