Define an abstract base class called BasicShape . The BasicShape class should ha
ID: 3763732 • Letter: D
Question
Define an abstract base class called BasicShape. The BasicShape class should have the following members:
a) Protected Member Variable: area (a double used to hold the shape’s area).
b) Constructor and Public Member Functions:
BasicShape(double a): A constructor that sets value of member area with a.
calcArea(): This public function should be a pure virtual function.
print(): A public virtual function that prints the area
Define a class named Circle. It should be derived from the BasicShape class. It should have the following members:
a) Private Member Variables: radius (a double used to hold the circle’s radius)
b) Constructor and Public Member Functions:
Circle(double a, double r): constructor that should call the base class constructor to set value of member area with a. The constructor will also set value of member radius with r
calcArea(): Overridden function that calculates the area of the circle (area = 3.14159 * radius * radius) and stores the result in the inherited member area.
print(): Overridden function that will print the radius and area. This function should use the base class’s print function to print the area.
Define a class named Rectangle. It should be derived from the BasicShape class. It should have the following members:
a) Private Member Variables:
width, a integer used to hold the width of the rectangle.
length, a integer used to hold the length of the rectangle.
b) Constructor and Public Member Functions:
Rectangle(double a, int w, int l): constructor that should call the base class constructor to set value of member area with a. The constructor will also set value of member width with w and length with l.
calcArea(): Overridden function that calculates the area of the rectangle (area = length * width) and stores the result in the inherited member area.
print(): Overridden function that will print the length, width and area. This function should call the base class’s print function to print the area.
After you have created these classes, create a test program
Write a function named poly whose only parameter is a BasicShape pointer.
Function poly should use the BasicShape pointer to invoke calcArea function and print function.
In main(), define a Circle object and a Rectangle object.
From main(), call the function poly such that it will polymorphically invoke calcArea function and print function of the Circle object.
From main(), call the function poly again such that it will polymorphically invoke calcArea function and print function of the Rectangle object.
Explanation / Answer
#include<iostream>
using namespace std;
class BasicShape {
protected:
double area;
public:
BasicShape(double a) {
area=a;
}
void virtual calcArea()=0;
void print() {
cout<<"Area is: "<<area<<" ";
}
};
class Circle : public BasicShape {
private:
double radius;
public:
Circle(double a, double r):BasicShape(a) {
radius=r;
}
void calcArea() {
area = 3.14159*radius*radius;
}
void print() {
cout<<"Radius is: "<<radius<<" ";
BasicShape::print();
}
};
class Rectangle : public BasicShape {
private:
int width;
int length;
public:
Rectangle(double a, int w, int l) : BasicShape(a) {
width=w;
length=l;
}
void calcArea() {
area = width*length;
}
void print() {
cout<<"Length is: "<<length<<" ";
cout<<"Width is: "<<width<<" ";
BasicShape::print();
}
};
void poly(BasicShape *bs) {
bs->calcArea();
bs->print();
}
int main() {
Circle c(0, 12);
Rectangle r(0, 6, 12);
cout<<"Circle: "<<endl;
poly(&c);
cout<<" ";
cout<<"Rectangle: "<<endl;
poly(&r);
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.