Pure Abstract Base Class Project to test the Basic Shape Circle and Rectangle using C++
Pure Abstract Base Class Project
Define a pure abstract base class called BasicShape. The Baaicshape class should
have the following members:
Private Member Variable:
area, a double used to hold the shape’s area.
Public Member Functions:
getArea. This function should return the value in the member variable area.
calcArea. This function should be a pure virtual function.
Next, define a class named Circle. It should be derived from the Basicshape class.
It should have the following members:
Private Member Variables:
centerX, a long integer used to hold the x coordinate of the circle’s center.
centerY, a long integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle’s radius.
Public Member Functions:
constructor-accepts values for centerX, centerY, and radius. Should call the
overridden calcArea function described below.
getCenterx-returns the value in centerX.
getCenterY-rerurns the value in centerY.
calcArea—calculates the area of the circle (area = 3.14159 …. radius …. radius)
and stores the result in the inherited member area.
Next, define a class named Rectangle. Ir should be derived from the BasicShape
class. It should have the following members:
Private Member Variables:
width, a long integer used to hold the width of the rectangle.
length, a long integer used to hold the length of the rectangle.
Public Member Functions:
constructor-accepts values for width and length. Should call the overridden
calCArea function described below.
get’Width-returns the value in width.
getLength– returns the value in length.
calCArea-calculates the area of the rectangle (area = length ” width) and stores
the result in the inherited member area.
After you have created these classes, create a driver program that defines a Circle
object and a Rectangle object. Demonstrate that each object properly calculates and
reports its area.
Answer:
// Pure Abstract Base Class Project // Driver program is used to test the BasicShape (Abstract Base Class), Circle // (Derived Class) and Rectangle (Derived class). // The program asks for needed information for both a circle and rectangle and // returns the area. #include <iostream> #include "Circle.h" #include "Rectangle.h" using namespace std; int main() { long x, y, length, width; double rad; // Demonstrate a Circle. cout << "Please enter the x coordinate of the circle's center: "; cin >> x; cout << "Please enter the y coordinate of the circle's center: "; cin >> y; cout << "Please enter the radius of the circle: "; cin >> rad; Circle c(x,y,rad); cout << "The area of the circle is " << c.getArea() << "."; // Demonstrate a Rectangle. cout << "\n\nPlease enter the length of the rectangle: "; cin >> length; cout << "Please enter the width of the rectangle: "; cin >> width; Rectangle r(width, length); cout << "The area of the rectangle is " << r.getArea() << ".\n"; return 0; }
Specification file for BasicShape base class
#ifndef BASICSHAPE_H #define BASICSHAPE_H class BasicShape { private: double area; public: double getArea() {return area;} virtual void calcArea() = 0; void setArea(double a) { area = a; } }; #endif
Specification file for Circle derived class
#ifndef CIRCLE_H #define CIRCLE_H #include "BasicShape.h" class Circle : public BasicShape { private: long centerX; long centerY; double radius; public: Circle(long x, long y, double rad) { centerX = x; centerY = y; radius = rad; calcArea(); } long getCenterX() { return centerX; } long getCenterY() { return centerY; } void calcArea() { double temp = 3.14159 * radius * radius; BasicShape::setArea(temp); } double getArea() { return BasicShape::getArea();} }; #endif
Specification file for Rectangle derived class
#ifndef RECTANGLE_H #define RECTANGLE_H #include "BasicShape.h" class Rectangle : public BasicShape { private: long width; long length; public: Rectangle(long w, long l) { width = w; length = l; calcArea(); } long getWidth() { return width; } long getLength() { return length; } void calcArea() { double temp = length * width; BasicShape::setArea(temp); } }; #endif
Leave a reply