Question: #include #include using namespace std; class Shape { public: virtual void printDescription()=0; // Prints the type of shape object virtual double area()=0; // Returns the

#include

#include

using namespace std;

class Shape {

public:

virtual void printDescription()=0; // Prints the type of shape

object

virtual double area()=0; // Returns the area of the shape object

virtual double perimeter()=0; // Returns the perimter of the shape

object

virtual ~Shape()=0;

};

Shape::~Shape() {cout << "Shape destroyed" << endl;} //Needs base class

destructor

double totalArea(vector& v) {

double total = 0.0;

for (auto e : v) {

total += e->area(); // Possible due to polymorphism

}

return total;

}

double totalPerimeter(vector& v) {

double total = 0.0;

for (auto e: v) {

total += e->perimeter(); // Possible due to polymorphism

}

return total;

}

void shapeDescription(vector& v) {

for (auto e: v) {

e->printDescription(); // Possible due to polymorphism

}

}

// Implement Rectangle and Circle classes so that test program works

correctly. Rectangles are specified by length and width. Circles are

specificed by radius. Inline methods

int main()

{

vector canvas;

canvas.push_back(new Rectangle(1.0, 2.0));

canvas.push_back(new Circle(1.0));

canvas.push_back(new Rectangle (3.0, 3.0));

canvas.push_back(new Circle(2.0));

shapeDescription(canvas); // Prints description of shapes on canvas

cout << "Total area of shapes on canvas: " << totalArea(canvas) <<

endl; // Answer: 26.7075

cout << "Total perimeter of shapes on canvas " <<

totalPerimeter(canvas) << endl; // Answer: 36.849

for (auto fig: canvas) { //Dellocate objects

delete fig;

}

// Should print rectangle destroyed, shape destroyed, circle

destroyed, shape destroyed, rectangle destroyed, shape destroyed, circle

destroyed, shape destroyed

}

return 0;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!