C++ Please read the entire question before writing your code and make sure that
ID: 3799127 • Letter: C
Question
C++
Please read the entire question before writing your code and make sure that the output matches the given example!
Note: largerArea() must not produce terminal output, the value must be passed to the caller
through the result pointer variable.
Question: Download lab12_Q2.cpp . The following function accepts objects by reference and indicates the result of the operation by storing a value in the variable pointed to by result. Implement the function using the classes defined in Question 1. (Question 1 is added at the ended for reference to answer this question)
/**
* Determines the larger area between two Shape objects
* The larger area is stored in result
*/
void largerArea(Shape &a, Shape &b, double *result);
Use the given driver program to produce the following output:
Output:
This Triangle has a perimeter of: 12 and an area of: 6
The circle has a perimeter of: 12.5664 and an area of:12.56564
The larger area is:12.5664
This Triangle has a perimeter of: 24 and an area of: 24
The circle has a perimeter of: 12.5664 and an area of:12.56564
The larger area is:24
Press any key to continue...
Lab12_Q2.cpp is 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;
}
/**
* Determines the larger are between the two Shape objects
* The larger area is stored in result
*/
void largerArea(Shape &a, Shape &b, double *result) {
//Complete the function
}
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;
}
*********************************************************************************************************************************************************************************************************
Reference: Question 1
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;
}
Explanation / Answer
#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
class Triangle :public Shape
{
private:
int s1,s2,s3;
public:
string getType() //overriding pure virtual functions
{
return "triangle";
}
double getPerimeter()
{
return (s1+s2+s3);
}
double getArea()
{
double s = (s1+s2+s3)/2;
return (sqrt(s*(s-s1)*(s-s2)*(s-s3)));
}
void setDims(int s1,int s2,int s3)
{
this->s1 = s1;
this->s2 = s2;
this->s3 = s3;
}
};
//Implement class Circle here
class Circle:public Shape
{
private:
int radius;
public:
string getType() //overriding pure virtual functions
{
return "circle";
}
double getPerimeter()
{
return (2*3.14*radius);
}
virtual double getArea()
{
return (3.14*radius*radius);
}
void setDims(int radius)
{
this->radius = radius;
}
};
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)
{
if(a.getArea() > b.getArea())
*result = a.getArea();
else
*result = b.getArea();
}
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:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.