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

Re-write your 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 create and store input shapes as base class pointers. In addition, modify the application to allow the user to create and store any number of shapes (use an STL class to store the base class pointers, i.e. vector 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.

Here was lab 8

Design and write classes in a class hierarchy to describe different types of geometric shapes. Provide information in the classes in order to compute the area of each of the shapes. Create three separate classes (Triangle,Rectangle,Circle) that are derived from a separate base class (Shape). Write a program that allows a user to create any of the different shape types. Prompt the user to select the shape type and enter the necessary dimensions (i.e. refer to formulas below; rectangle needs width and height, circle needs radius, triangle needs height and base). After the user has finished creating the shape, compute and print out the area.

Use the following formulas for your program:

Triangle:

area = * base * height

Rectangle:

area = width * height

Circle:

area = PI * radius2

shape.h

#ifndef _shape_h_ #define _shape_h_

//Base Class \ class Shape { public: //Method that calculates area \ double area() { return 0; } };

#endif

triangle.h

#define _triangle_h_ #include "shape.h" #include using namespace std;

//Triangle class \ class triangle : public Shape { private: double base, height;

public:

//Constructor triangle(double b = 1.0, double h = 1.0) { base = b; height = h; } double tarea(); double area(); };

#endif

triangle.cpp

File Edit Options Buffers Tools C++ Help #include "triangle.h" #include "shape.h"

double triangle::tarea() { double a, b, c; cout << " Enter base value:"; cin >> a ; cout << " Enter height value:"; cin >> b ; triangle triObj(a, b);

{

return (0.5 * base * height);

cout << " Triangle Area: " << triObj.area() << " "; }; }

rectangle.h

#ifndef _rectangle_h_ #define _rectangle_h_ #include "shape.h" #include using namespace std;

//Rectangle class \ class Rectangle : public Shape { private: double width, height;

public: //Constructor \ Rectangle(double w = 1.0, double h = 1.0) { width = w; height = h; }

double rarea(); double area(); };

#endif

rectangle.cpp

include "rectangle.h" #include "shape.h"

double Rectangle::rarea() { double a,b; cout << " Enter width value: "; cin >> a; cout << " Enter height value: "; cin >> b; \

Rectangle recObj(a, b); { return (width * height); }; cout << " Rectangle Area: " << recObj.area() << " "; }

circle.h

#ifndef _circle_h_ #define _circle_h_ #include "shape.h" #include using namespace std;

//Circle class \ class Circle : public Shape { private: double radius;

public: //Constructor \ Circle(double r = 1.0) { radius = r; } double carea(); double area();

};

#endif

circle.cpp

#include "circle.h" #include "shape.h"

double Circle::carea() { double a,b; cout << " Enter radius value: "; cin >> a; Circle cirObj(a); { return (3.14 * radius * radius); }

cout << " Circle Area: " << cirObj.area() << " "; }

lab8.cpp

include

#include "shape.h"

#include "triangle.h"

#include "rectangle.h"

#include "circle.h"

#include

using namespace std;

//Main function

int main()

{

double a, b;

char ch;

triangle k(a,b);

Rectangle n(a,b);

Circle j(a);

//Iterate till user want to quit

while(1)

{

//Reading type of shape

cout << " Select Shape: \t T - Triangle \t R - Rectangle \t C - Ci\

rcle \t E - Exit: ";

cin >> ch;

switch(ch)

{

//Triangle

case 'T':

case 't':

{

k.tarea();

break;

}

//Rectangle

case 'R':

case 'r':

{

n.rarea();

break;

}

//Circle

case 'C':

case 'c':

{

j.carea();

break;

}

//Exit

case 'e':

case 'E': return 0;

default: cout << " Invalid Shape selected... ";

}

}

cout << " " << endl;

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!