View the step-by-step solution to:

# Re-write derived classes from Lab 8 to override a pure virtual function in the base class to compute the area of the shapes.

Re-write derived classes from Lab 8 to override a pure virtual function in the base class to compute the area of the shapes. Re-write the application from Lab 8 to make and store input shapes as base class pointers. In addition, modify the application to allow the user to make and store any number of shapes (use an STL class to store the base class pointers, i.e. vector<Shape *> shapes). After the user has finished creating shapes, compute and print the area for all the shapes in a single loop using only the base class pointers with dynamic binding.

main.cpp

#include "Circle.h"

#include "Triangle.h"

#include "Rectangle.h"

#include <stdlib.h>

#include <vector>

#include <iostream>

using namespace std;

main()

{

vector<Shape *>data;

Circle c;

Triangle t;

Rectangle r;

int choice;

float tmp1, tmp2;

cout << "Create a shape" << endl;

cout << "t1) Circle" << endl;

cout << "t2) Triangle" << endl;

cout << "t3) Rectangle" << endl;

cout << "t4) Done" << endl;

cin >> choice;

switch( choice )

{

case 1:

cout << "Enter <radius>" << endl;

cin >> tmp1;

c = Circle( tmp1 );

c.Area();

cout << c;

data.push_back(&c);

break;

case 2:

cout << "Enter <base> <height>" << endl;

cin >> tmp1 >> tmp2;

t = Triangle( tmp1, tmp2 );

t.Area();

cout << t;

data.push_back(&t);

break;

case 3:

cout << "Enter <width> <height>" << endl;

cin >> tmp1 >> tmp2;

r = Rectangle( tmp1, tmp2 );

r.Area();

cout << r;

data.push_back(&r);

break;

case 4:

exit(0);

break;

}

}

Circle.cpp

#include "Circle.h"

// Virtual methods

void Circle::Area()

{

}

Rectangle.cpp

#include "Rectangle.h"

// Virtual methods

void Rectangle::Area()

{

area = width * height;

}

Shape.cpp

#include "Shape.h"

ostream &operator<<( ostream &strm, Shape &s )

{

strm << "area: " << s.area << endl;

return strm;

}

Triangle.cpp

#include "Triangle.h"

// Virtual methods

void Triangle::Area()

{

area = float(0.5) * base * height;

}

Circle.cpp

#ifndef CIRCLE_H_INCLUDED

#define CIRCLE_H_INCLUDED

#include "Shape.h"

class Circle : public Shape

{

protected:

public:

Circle( float r=0.0 ) { radius = r; }

~Circle() {}

// Compute area

void Area();

};

#endif // CIRCLE_H_INCLUDED

Rectangle.h

#ifndef RECTANGLE_H_INCLUDED

#define RECTANGLE_H_INCLUDED

#include "Shape.h"

class Rectangle : public Shape

{

protected:

float width, height;

public:

Rectangle() { width = height = 0.0; }

Rectangle( float w, float h ) { width = w; height = h; }

~Rectangle() {}

// Compute area

void Area();

};

#endif // RECTANGLE_H_INCLUDED

shape.h

#ifndef SHAPE_H_INCLUDED

#define SHAPE_H_INCLUDED

using namespace std;

#include <iostream>

class Shape

{

protected:

float area;

public:

Shape() { area = 0.0; }

// Overloaded output operator as friend

friend ostream &operator<<( ostream &, Shape & );

// Access methods

void setArea( float a ) { area = a; }

float getArea() { return area; }

};

#endif // SHAPE_H_INCLUDED

Triangle.h

#ifndef TRIANGLE_H_INCLUDED

#define TRIANGLE_H_INCLUDED

#include "Shape.h"

class Triangle : public Shape

{

protected:

float base, height;

public:

Triangle() { base = height = 0.0; }

Triangle( float b, float h ) { base = b; height = h; }

~Triangle() {}

// Compute area

void Area();

};

#endif // TRIANGLE_H_INCLUDED

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

### -

Educational Resources
• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents