Question: Need help with this one (object oriented c++ ) ty! Define a pure abstract base class called BasicShape . *Pre-requisite 15.6 Polymorphism and Virtual Member
Need help with this one (object oriented c++) ty!
Define a pure abstract base class called BasicShape.
*Pre-requisite
- 15.6 Polymorphism and Virtual Member Functions
- 15.7 Abstract Base Classes and Pure Virtual Functions
The BasicShape 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 must 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 circles center.
- centerY, a long integer used to hold the y coordinate of the circles center.
- radius, a double used to hold the circle's radius.
Public Member Functions:
- constructoraccepts values for centerX, centerY, and radius. Should call the overridden calcArea function described below.
- getCenterXreturns the value in centerX.
- getCenterYreturns the value in centerY.
- calcAreacalculates 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. It 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:
- constructoraccepts values for width and length. Should call the overridden calcArea function described below.
- getWidthreturns the value in width.
- getLengthreturns the value in length.
- calcAreacalculates the area of the rectangle (area = length * width) and stores the result in the inherited member area.
After you have created these classes, use the provided driver program below that defines a Circle object and a Rectangle object. Demonstrate that each object properly calculates and reports its area.
include #include "Circle.h" // in this example code, BasicShape Class is located in this Circle.h file. #include "Rectangle.h" using namespace std; int main() { long x, y, length, width; double rad; cout << "Demonstrating a Circle" << " Enter the Circle's x, y center coord: "; cin >> x >> y; cout << "Enter the radius: "; cin >> rad; Circle c(x,y,rad); cout << "The center is center at ( " << x << ", " << y << " ) with the radius of " << rad << " The area of the circle is " << c.getArea() << "."; cout << " Demonstrating a Rectangle" << " Enter the Rectangle's width and length: "; cin >> width >> length; Rectangle r(width, length); cout << "The width is " << width << " and the length is " << length << " The area of the rectangle is " << r.getArea() << ". "; } Sample Test Run
Running /home/ubuntu/workspace/comsc200/m05/as5.cpp Please enter the coordinate of the circle's center: 2 3 Please enter the radius of the circle: 4 The area of the circle is 50.2654. Please enter the width and length of the rectangle: 2 3 The area of the rectangle is 6.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
