C Pleas read the entire question before writing your code an
C++
Pleas read the entire question before writing your code and make sure that the output example matches the output!
Please do not modify the driver program. No state pertaining to the Triangle or Circle may be stored in the Shape objects. Use Heron’s formula for calculating the area of a Triangle:
A = (s (s - a)(s - b)(s - c)), s = (a + b + c)/2
Question:
Download lab12_Q1.cpp . Triangles, and Circles are Shapes, geometric objects which can havetheir perimeter and area calculated. Imple ment a Shape abstract class , which can be used in the following manner:
void describeShape(Shape &s) {
double area = s.getArea();
string type = s.getType();
double perim = s.getPerimeter();
cout << “This “ << type << “ has a perimeter of: “
<< perim << “ and an area of: “ << area << endl;
}
The Shape class is provided for you. Impleme nt Triangle and Circle classes and use the given driver program to produce the following output:
Output:
The triangle has a permieter of: 12 and an area of: 6
The Circle has a perimeter of: 25.1328 and an area of: 50.2656
Press any key to continue...
Lab12_Q1.cpp given below:
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
class Shape {
public:
virtual string getType() = 0;
virtual double getPerimeter() = 0;
virtual double getArea() = 0;
};
//Implement class Triangle here
//Implement class Circle here
void describeShape(Shape &s) {
double a = s.getArea();
double p = s.getPerimeter();
string t = s.getType();
cout << \"This \" << t << \" has a perimeter of: \" << p
<< \" and an area of: \" << a << endl;
}
int main() {
Triangle t;
Circle c;
c.setDims(4);
t.setDims(3, 4, 5);
describeShape(t);
describeShape(c);
return 0;
}
Solution
class circle:public shape
{
public:
double area;
int radius;
void setDims(int R)
{
r =R;
}
int area()
{
area=(3.14 *r*r);
return area;
}
int area()
{
perimeter=(2*3.14 *r);
return perimeter;
}
void getType()
{
string s2 =\"Circle\";
return s2;
}
};
class triangle:public shape
{
public:
double area;
int perimeter;
int a,b,c;
void setDims(int aa,int bb ,int cc)
{
a =aa;
b =bb;
c=cc;
}
int getArea()
{
s=(a+b+c)/2.0;
area=(sqrt)(s*(s-a)*(s-b)*(s-c));
return area;
}
int getPerimeter()
{
perimeter=a+b+c;
return perimeter;
}
string getType()
{
string s1 =\"Triangle\";
return s1;
};


