Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Knowledge Discovery In Databases

Authors: Gregory Piatetsky-Shapiro, William Frawley

1st Edition

0262660709, 978-0262660709

More Books

Students also viewed these Databases questions

Question

Develop a marketing campaign for a training course or program.

Answered: 1 week ago

Question

2 2 8 . .

Answered: 1 week ago