Question SOLVED: 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

$30.00

Category:

Description

Question SOLVED: 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 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<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.

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 <iostream>
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 << “\n Enter base value:”;
cin >> a ;
cout << “\n Enter height value:”;
cin >> b ;
triangle triObj(a, b);

{

return (0.5 * base * height);

cout << “\n Triangle Area: ” << triObj.area() << ” \n”;
};
}

rectangle.h

#ifndef _rectangle_h_
#define _rectangle_h_
#include “shape.h”
#include<iostream>
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 << “\n Enter width value: “;
cin >> a;
cout << “\n Enter height value: “;
cin >> b; \

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

circle.h

#ifndef _circle_h_
#define _circle_h_
#include “shape.h”
#include<iostream>
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 << “\n Enter radius value: “;
cin >> a;
Circle cirObj(a);
{
return (3.14 * radius * radius);
}

cout << “\n Circle Area: ” << cirObj.area() << ” \n”;
}

lab8.cpp

include <iostream>

#include “shape.h”

#include “triangle.h”

#include “rectangle.h”

#include “circle.h”

#include<iostream>

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 << “\n\n Select Shape: \n \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 << “\n Invalid Shape selected… \n”;

}

}

cout << “\n\n” << endl;

return 0;

}


error: Content is protected !!