PureAbstractBaseClassAlt.7z
Notice: This challenge has been solved in two ways. The first one is written according to the problem statement on page 986. The other solution is based on code kindly provided to this person, who only adapted the code a little.
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
Circle.cpp
#include "Circle.h"
/* **********************************************************
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.cpp
#include "Rectangle.h"
/* **********************************************************
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);
}
BasicShapeDm.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\n\n";
cout << "Circle Data\n\n";
cout << "Center X: " << circle->getCenterX() << "\n";
cout << "Center Y: " << circle->getCenterY() << "\n";
cout << "Radius: " << circle->getRadius() << "\n\n";
cout << "Recatangle Data\n\n";
cout << "Length: " << rectangle->getLength() << "\n";
cout << "Width: " << rectangle->getWidth() << "\n\n";
cout << "The area of your shapes is:\n\n";
cout << fixed << setprecision(2);
for (size_t i = 0; i < shapes.size(); i++)
{
cout << shapeName[i] << ": " << shapes[i]->getArea() << " mm\n";
}
cout << "\nThank you for using this 'pure' demo! Have a nice day!";
delete circle;
delete rectangle;
cin.get();
cin.ignore();
return 0;
}
No comments:
Post a Comment