C++ (please check your code if it works before submit it, thank you) Define an a
ID: 3695647 • Letter: C
Question
C++ (please check your code if it works before submit it, thank you)
Define an abstract base class called BasicShape. The BasicShape class shouldhave the following members:
Protected Member Variable: area (a double used to hold the shape’s area).
Private Member Variable: name (a string to indicate the shape’s type)
Constructor and Public Member Functions:
BasicShape(double a, string n): A constructor that sets value of member area witha and member name with n.
calcArea(): This public function should be a pure virtual function.
print(): A pubic virtual function that prints the value of data member area
getName():A pubic function that returns the value of data member name
Define a class named Circle. It should be derived from the BasicShape class.
It should have the following members:
Private Member Variable: radius (a double used to hold the circle’s radius)
Constructor and Public Member Functions:
Circle(double a, string n, double r): constructor that should call the base class constructor to set value of member area with a and name with n. 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, inherited member area and inherited member name. This function should use 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 andprint function.
In main(): define a Circle object with radius 10, name Round and area initialized to 0.
From main(), call the function poly such that it will polymorphically invoke calcAreafunction and print function of the Circle object
Explanation / Answer
Ans;
#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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.