C++ program to calculate area of rectangle using constructor
Asked by admin @ in Computer Science viewed by 346 People
C++ program to calculate area of traingle circle and rectangle using constructor overloading
Asked by admin @ in Computer Science viewed by 346 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 310 persons
Asked by admin @ in Computers and Technology viewed by 351 persons
Asked by admin @ in Computer Science viewed by 398 persons
Asked by admin @ in Computer Science viewed by 357 persons
Asked by admin @ in General viewed by 413 persons
Asked by maham237 @ in Mathematics viewed by 339 persons
Asked by pranav94 @ in Engineering viewed by 469 persons
Asked by admin @ in Mathematics viewed by 370 persons
Asked by admin @ in Mathematics viewed by 393 persons
Asked by admin @ in Mathematics viewed by 464 persons
Asked by admin @ in Mathematics viewed by 288 persons
Asked by admin @ in Mathematics viewed by 277 persons
Asked by admin @ in Physics viewed by 283 persons
Asked by admin @ in Computer Science viewed by 330 persons
Asked by admin @ in Computer Science viewed by 333 persons
Asked by admin @ in Computer Science viewed by 19016 persons
Asked by admin @ in Computer Science viewed by 15794 persons
Asked by admin @ in Computer Science viewed by 10964 persons
Asked by admin @ in Computer Science viewed by 10397 persons
Asked by admin @ in Computer Science viewed by 8127 persons
Asked by admin @ in Computer Science viewed by 7837 persons
Asked by admin @ in Computer Science viewed by 6480 persons
Asked by admin @ in Computer Science viewed by 5785 persons
Asked by admin @ in Computer Science viewed by 5684 persons
Asked by admin @ in Computer Science viewed by 5471 persons
Asked by admin @ in Computer Science viewed by 4977 persons
Asked by admin @ in Computer Science viewed by 4967 persons
Asked by admin @ in Computer Science viewed by 4680 persons
Asked by admin @ in Computer Science viewed by 4483 persons