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
double total = 0.0;
for (auto e : v) {
total += e->area(); // Possible due to polymorphism
}
return total;
}
double totalPerimeter(vector
double total = 0.0;
for (auto e: v) {
total += e->perimeter(); // Possible due to polymorphism
}
return total;
}
void shapeDescription(vector
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.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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started