C PROGRAMMING PART A PART B CODE NEEDED lab12Q1cpp lab12Q2cp
C++ PROGRAMMING
PART A:
PART B:
CODE NEEDED:
lab12_Q1.cpp:
lab12_Q2.cpp:
Download lab12 Q1 cpp. Triangles, and Circles are Shapes, geometric objects which can have their perimeter and area calculated. Implement a Shape abstract class, which can be used in the following mann void describeShape (Shape double area s.getArea0 string type s get Type0; double perim s.get Perimeter 0 cout \"This type has a perimeter of perim and an area o area endl. The Shape class is provided for you. Implement Triangle and Circle classes and use the given driver program to produce the following output This Triangle has a perimeter of 12 and an area of: 6 This Circle has a perimeter of: 25.1328 and an area of: 50.2656 Press any key to continue 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 Nos (a b c)/2Solution
Solution: See the code below
1. Problem 1:
--------------------------------------------
#include<iostream>
#include<math.h>
#include<string>
#define PI 22/7
using namespace std;
class Shape {
public:
virtual string getType() = 0;
virtual double getPerimeter() = 0;
virtual double getArea() = 0;
};
//Implement class Triangle here
class Triangle:public Shape
{
private:
//class members
double a; //first side of triangle
double b; //second side of triangle
double c; //third side of triangle
public:
//sets dimensions of shape
void setDims(double a,double b,double c)
{
this->a=a;
this->b=b;
this->c=c;
}
//returns type of shape
string getType()
{
return \"Triangle\";
}
//calculates perimeter of shape
double getPerimeter()
{
return (a+b+c);
}
//returns area of shape
double getArea()
{
double s=getPerimeter()/2;
double area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
};
//Implement class Circle here
class Circle:public Shape
{
private:
//class members
double r; //radius of circle
public:
//sets dimensions of shape
void setDims(double r)
{
this->r=r;
}
//returns type of shape
string getType()
{
return \"Circle\";
}
//calculates perimeter of shape
double getPerimeter()
{
return (2*PI*r);
}
//returns area of shape
double getArea()
{
double area=PI*r*r;
return area;
}
};
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;
}
-------------------------------------------
Output:
------------------------------------------
This Triangle has a perimeter of: 12 and an area of: 6
This Circle has a perimeter of: 24 and an area of: 48
-----------------------------------------------
2. Problem 2:
---------------------------------------------------
#include<iostream>
#include<math.h>
#include<string>
#define PI 22/7
using namespace std;
class Shape {
public:
virtual string getType() = 0;
virtual double getPerimeter() = 0;
virtual double getArea() = 0;
};
//Implement class Triangle here
class Triangle:public Shape
{
private:
//class members
double a; //first side of triangle
double b; //second side of triangle
double c; //third side of triangle
public:
//sets dimensions of shape
void setDims(double a,double b,double c)
{
this->a=a;
this->b=b;
this->c=c;
}
//returns type of shape
string getType()
{
return \"Triangle\";
}
//calculates perimeter of shape
double getPerimeter()
{
return (a+b+c);
}
//returns area of shape
double getArea()
{
double s=getPerimeter()/2;
double area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
};
//Implement class Circle here
class Circle:public Shape
{
private:
//class members
double r; //radius of circle
public:
//sets dimensions of shape
void setDims(double r)
{
this->r=r;
}
//returns type of shape
string getType()
{
return \"Circle\";
}
//calculates perimeter of shape
double getPerimeter()
{
return (2*PI*r);
}
//returns area of shape
double getArea()
{
double area=PI*r*r;
return area;
}
};
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;
}
/**
* Determines the larger are between the two Shape objects
* The larger area is stored in result
*/
void largerArea(Shape &a, Shape &b, double *result) {
double area_a=a.getArea(); //area of shape a
double area_b=b.getArea(); //area of shape b
*result = ((area_a>area_b)? area_a : area_b);
}
int main() {
Triangle t;
Circle c;
c.setDims(2);
t.setDims(3, 4, 5);
describeShape(t);
describeShape(c);
double result;
largerArea(t, c, &result);
cout << \"The larger area is: \" << result << endl << endl;
t.setDims(6, 8, 10);
describeShape(t);
describeShape(c);
largerArea(t, c, &result);
cout << \"The larger area is: \" << result << endl;
return 0;
}
--------------------------------
Output:
---------------------------------
This Triangle has a perimeter of: 12 and an area of: 6
This Circle has a perimeter of: 12 and an area of: 12
The larger area is: 12
This Triangle has a perimeter of: 24 and an area of: 24
This Circle has a perimeter of: 12 and an area of: 12
The larger area is: 24
--------------------------------------------




