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

Re-write your derived classes from Lab 8 to override a pure virtual function in

ID: 3865459 • Letter: R

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 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 << " 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<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 << " 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<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 << " Enter radius value: ";
cin >> a;
Circle cirObj(a);
{
return (3.14 * radius * radius);
}

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

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 << " Select Shape: T - Triangle R - Rectangle C - Ci

rcle 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;

}

Triangle:

area = ½ * base * height

Rectangle:

area = width * height

Circle:

area = PI * radius2

Explanation / Answer

Answer for the given Queston:

See the below c++ code updated as per given in problem statement like change area to pure virtual functiona and used the vector<Shape*> arr objects to call all the classes at one instance.

#include<iostream>
using namespace std;

//Base Class
class Shape
{
public:
//Method that calculates area and it is pure virtual function   
virtual double area() = 0;
};

//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();
};


// Overriding the base class area method in triangle
double triangle::area()
{
double a, b, c;
cout << " Enter base value:";
cin >> a ;
cout << " Enter height value:";
cin >> b ;
return (0.5 * base * height);
}

//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();
};

double Rectangle::area()
{
double a,b;
cout << " Enter width value: ";
cin >> a;
cout << " Enter height value: ";
cin >> b;
  
return (width * height);
  
}

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

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


include <iostream>
using namespace std;

void printArea( const vector<Shape*> & a)
{
for( int i = 0; i < a.size( ); i++ )
cout<<"Area is :"<<a[ i ]->area( )<<endl;
}

//Main function   
int main()
{
double a, b;
char ch;
//triangle k(10.3,25.5);
//Rectangle n(18.7,78.5);
//Circle j(47.5);

vector<Shape *> arr;

arr.push_back( new triangle(10.3,25.5) );
arr.push_back( new Rectangle n(18.7,78.5) );
arr.push_back( new Circle j(47.5) );

printArea( arr );
cout << "Total area is " << totalArea( arr ) << endl;
//cleanup( arr );   
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote