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

Pure Abstract Base Class Project. Define a class called BasicShape which will be

ID: 3730353 • Letter: P

Question

Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This class will have 3 private data members. It will have 2 long integer data members called centerX and centerY. The last data member is a double called radius. It will have a constructor that accepts values for centerX, centerY and radius. This should call the overridden calcArea function of this class. This class defines its version of the calcArea function which determines the area of the circle using the formula area = 3.14159 * radius * radius. The class will also provide two functions called getCenterX and getCenterY which return the correct values. Define a class called Rectangle. It should be a derived class of the BasicShape class. This class will have 2 private data members called width and length. Both data members should be long integers. Its constructor will have parameters for both the width and length. It will also override the calcArea function. For this class the calcArea function will use the formula area = length * width. It will provide member function called getWidth and getLength which should return the correct values. NOTE:REQUIRED FILES ARE FOUR AND NOT MORE THAN FOUR - 1) PureAbstractBaseClass.cpp 2)BasicShape.h 3). Circle.h and 4). Rectangle.h

Explanation / Answer

Below is your program. Please add -std=c++11 in Compiler options to run this. As it is based on C++ 11 standard specification. Let me know if you have any issues.

BasicShape.h

#ifndef BASIC_SHAPE_H_
#define BASIC_SHAPE_H_

class BasicShape
{
protected:
double area;

public:
virtual ~BasicShape()
{
}

double getArea() const
{ return area; }

virtual void calcArea() = 0;
};

#endif

--------------------------------------------------------------------------------------------------------------------------------------------------------

Circle.h

#ifndef CIRCLE_H_
#define CIRCLE_H_

#include "BasicShape.h"

class Circle : public BasicShape
{
private:
long centerX;
long centerY;
double radius;

public:
Circle(long, long, double);

~Circle()
{
}

// Accessors
long getCenterX() const
{ return centerX; }

long getCenterY() const
{ return centerY; }

double getRadius() const
{ return radius; }

virtual void calcArea() override;
};

#endif

/* **********************************************************
Rectangle::Rectangle() - long, long, double
- Constructor
********************************************************** */

Circle::Circle(long xCoord, long yCoord, double rad) : BasicShape()
{
centerX = xCoord;
centerY = yCoord;
radius = rad;

this->calcArea();
}

/* **********************************************************
Circle::calcArea()
Calculates the area of a circle object, and assigns the
result to the inherited area member variable.
********************************************************** */

void Circle::calcArea()
{
area = (3.14159 * (radius * radius));
}

-------------------------------------------------------------------------------------------------------------------------------------------

Rectangle.h

#ifndef RECTANGLE_H_
#define RECTANGLE_H_

#include "BasicShape.h"

class Rectangle : public BasicShape
{
private:
long length;
long width;

public:
Rectangle(long, long);
  
~Rectangle()
{
}

long getLength() const
{ return length; }

long getWidth() const
{ return width; }

virtual void calcArea() override;
};

#endif

/* **********************************************************
Rectangle::Rectangle() : long, long
- Constructor
********************************************************** */

Rectangle::Rectangle(long rectLength, long rectWidth) : BasicShape()
{
width = rectLength;
length = rectWidth;

this->calcArea();
}

/* **********************************************************
Rectangle::calcArea()
Calculates and returns the area of a rectangle object and
assigns the result to the inherited area member variable.
********************************************************** */

void Rectangle::calcArea()
{
area = (length * width);
}

-----------------------------------------------------------------------------------------------------------------------------------------

PureAbstractBaseClass.cpp

#include "Circle.h"
#include "Rectangle.h"

#include <array>
using std::array;

#include <iomanip>
using std::setprecision;

#include <iostream>
using std::cin;
using std::cout;
using std::fixed;

#include <string>
using std::string;

int main()
{
const int NUM_OBJ = 2;

// Dynamically created shape objects
Circle *circle = new Circle(4, 4, 5.74);
Rectangle *rectangle = new Rectangle(15, 7);

// Array of pointers to shape objects
array<BasicShape *, NUM_OBJ> shapes { circle, rectangle };

// Array of shape names
array<string, NUM_OBJ> shapeName { "Circle ", "Rectangle" };

cout << "'PURE' ABSTRACT BASE CLASS DEMO ";

cout << "Circle Data ";
cout << "Center X: " << circle->getCenterX() << " ";
cout << "Center Y: " << circle->getCenterY() << " ";
cout << "Radius: " << circle->getRadius() << " ";

cout << "Recatangle Data ";
cout << "Length: " << rectangle->getLength() << " ";
cout << "Width: " << rectangle->getWidth() << " ";

cout << "The area of your shapes is: ";
cout << fixed << setprecision(2);
  
for (size_t i = 0; i < shapes.size(); i++)
{
cout << shapeName[i] << ": " << shapes[i]->getArea() << " mm ";
}

cout << " Thank you for using this 'pure' demo! Have a nice day!";

delete circle;
delete rectangle;
  
cin.get();
cin.ignore();
return 0;
}

-----------------------------------------------------------------------------------------------------------------------------------------------------

Output

'PURE' ABSTRACT BASE CLASS DEMO

Circle Data

Center X: 4
Center Y: 4
Radius: 5.74

Recatangle Data

Length: 7
Width: 15

The area of your shapes is:

Circle : 103.51 mm
Rectangle: 105.00 mm

Thank you for using this 'pure' demo! Have a nice day!

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