Pure Abstract Base Class Project. Define a class called BasicShape which will be
ID: 3731795 • 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: a). REQUIRED FILES ARE FOUR AND NOT MORE THAN FOUR - 1) PureAbstractBaseClass.cpp 2)BasicShape.h 3). Circle.h and 4). Rectangle.h
NOTE: b). Both dimensions (length, width and radius) of both the Rectangle and the Circle have to be entered by the user using “cout and cin “ in the PureAbsractBaseClass.cpp file. Pls test with Visual Studio. C++
Explanation / Answer
######## BasicShape.h #########
#ifndef BASICSHAPE_H
#define BASICSHAPE_H
using namespace std;
class BasicShape
{
protected:
double area; // To hold area
public:
// Accessor function to return area
double getArea() const
// Pure virtual function
virtual double calcArea() const = 0;
};
#endif
############ Circle.h ########
#ifndef CIRCLE_H
#define CIRCLE_H
#include "BasicShape.h"
using namespace std;
class Circle : public BasicShape
{
private:
long centerX; // To hold x coordinate
long centerY; // To hold y coordinate
double radius; // To hold radius
public:
// Constructor
Circle(long, long, double );
// Accessors
long getCenterX() const;
long getCenterY() const;
// Overridden calcArea function
virtual double calcArea() const;
};
//***********************************************
// Constructor that accepts arguments and uses *
// the this-> pointer to access the class member*
//***********************************************
Circle::Circle(long centerX, long centerY, double radius)
{ this->centerX = centerX;
this->centerY = centerY;
this->radius = radius;
// Call the overriden calcArea function
calcArea(); }
//***********************************************
// getCenterX function returns the x coodinates *
//***********************************************
long Circle::getCenterX() const
{ return centerX; }
//***********************************************
// getCenterY function returns the y coodinates *
//***********************************************
long Circle::getCenterY() const
{ return centerY; }
//***********************************************
// Circle::calcArea() calculates the area of a *
// circle and returns it *
//***********************************************
double Circle::calcArea() const
{ double area = 3.14159*radius*radius;
return area; }
#endif
############# Rectangle.h ##########
#ifndef RECTANGLE_H
#define RECTANGEL_H
#include "BasicShape.h"
class Rectangle : public BasicShape
{
private:
long width; // To hold width
long length; // To hold length
public:
// Constructor
Rectangle(long, long);
//Accessors
long getWidth() const;
long getLength() const;
// Overridden calcArea function
virtual long calcArea() const;
};
//***********************************************
// Constructor that accepts arguments and uses *
// the this-> pointer to access the class member*
//***********************************************
Rectangle::Rectangle(long width, long length)
{ this->width = width;
this->length = length;
// Call the overriden calcArea function
calcArea(); }
//***********************************************
// getWidth function returns the width *
//***********************************************
long Rectangle::getWidth() const
{ return width; }
//***********************************************
// getLength function returns the length *
//***********************************************
long Rectangle::getLength() const
{ return length; }
//***********************************************
// Rectangle::calcArea() calculates the area of a
// Rectangle *
//***********************************************
long Rectangle::calcArea() const
{ long area = length*width; }
#endif
############# PureAbsractBaseClass.cpp ########
//***********************************************
// Pure Abstract Base Class *
// This program defines a Circle object and a *
// Rectangle object and calculates the area of *
// each. *
//***********************************************
#include "Circle.h"
#include "Rectangle.h"
#include <iostream>
using namespace std;
// Function prototype
void displayArea();
void main()
{
displayArea();
}
void displayMessage()
{
// Create a Circle Object
Circle circle(5, 5, 10);
Rectangle rec(5,5);
cout << "Circle Output" << endl;
cout << "_____________" << endl;
cout << "Center x: " << circle.getCenterX() << endl;
cout << "Center y: " << circle.getCenterY() << endl;
cout << "Area: " << circle.calcArea() << endl << endl;
cout << "Rectangle Output" << endl;
cout << "________________" << endl;
cout << "Length: " << rec.getLength() << endl;
cout << "Width: " << rec.getWidth() << endl;
cout << "Area: " << rec.calcArea() << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.