Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <vector> using namespace std; class Shape { public:

ID: 3700176 • Letter: #

Question

#include <iostream>

#include <vector>

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<Shape*>& v) {

double total = 0.0;

for (auto e : v) {

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

}

return total;

}

double totalPerimeter(vector<Shape*>& v) {

double total = 0.0;

for (auto e: v) {

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

}

return total;

}

void shapeDescription(vector<Shape*>& 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<Shape*> 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;

Explanation / Answer

#include <iostream>
#include <vector>

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<Shape*>& v) {
double total = 0.0;
for (vector<Shape*>::iterator e = v.begin(); e != v.end(); e++) {
total += (*e)->area(); // Possible due to polymorphism
}
return total;
}

double totalPerimeter(vector<Shape*>& v) {
double total = 0.0;
for (vector<Shape*>::iterator e = v.begin(); e != v.end(); e++) {
total += (*e)->perimeter(); // Possible due to polymorphism
}
return total;
}

void shapeDescription(vector<Shape*>& v) {
for (vector<Shape*>::iterator e = v.begin(); e != v.end(); e++) {
(*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

class Rectangle : public Shape{
private:
double length, width;
public:
Rectangle(double l, double w);
double area();
double perimeter();
~Rectangle();
void printDescription();
};

Rectangle::Rectangle(double l, double w) : Shape(){
length = l;
width = w;
}
double Rectangle::area() {
return length * width;
}
double Rectangle::perimeter() {
return 2 * (length + width);
}
Rectangle::~Rectangle(){
cout << "rectangle destroyed";
}
void Rectangle::printDescription(){
cout << "Rectangle ";
}

class Circle : public Shape{
private:
double radius;
public:
Circle(double r);
double area();
double perimeter();
~Circle();
void printDescription();
};

Circle::Circle(double r) : Shape(){
radius = r;
}
double Circle::area() {
return 3.14 * radius * radius;
}
double Circle::perimeter() {
return 2 * 3.14 * radius;
}
Circle::~Circle(){
cout << "circle destroyed";
}
void Circle::printDescription(){
cout << "Circle ";
}

int main(){
vector<Shape*> 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 (vector<Shape*>::iterator fig = canvas.begin(); fig != canvas.end(); fig++) { //Dellocate objects
delete *fig;
}
// Should print rectangle destroyed, shape destroyed, circle destroyed, shape destroyed, rectangle destroyed, shape destroyed, circle destroyed, shape destroyed

return 0;
}