Define an abstract base class called BasicShape. The BasicShape class should hav
ID: 3777094 • 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) Private Member Variable: name (a string to indicate the shape’s type)
c) Constructor and Public Member Functions:
BasicShape(double a, string n): A constructor that sets value of member area
with a and member name with n.
calcArea(): This public function should be a pure virtual function.
print(): A public virtual function that only prints the value of data member area
getName():A public function that returns the value of data member name
2. Define a class named Circle. It should be derived from the BasicShape class.
It should have the following members:
a) Private Member Variable: radius (a double used to hold the circle’s radius)
b) Constructor and Public Member Functions:
Circle(double a, string n, double r): constructor that should call the base class
constructor to initialize the member area with a and name with n. The constructor
will also set the 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.
3. 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 with initial area 0, name Round and radius 10.
From main(), call the function poly such that it will polymorphically invoke calcArea
function and print function of the Circle object.
Explanation / Answer
/**
C ++ program that tests the BasicShape and Circle classes
and print the name, area and radius to console.
*/
//main.cpp
//header files
#include<iostream>
#include<string>
using namespace std;
//class definition
class BasicShape
{
protected:
double area;
private:
string name;
public:
BasicShape(double a, string n)
{
area=a;
name=n;
}
//pur virtual function has no body
virtual void calcArea() = 0;
virtual void print()
{
cout<<"Area : "<<area<<endl;
}
string getName()
{
return name;
}
};
//Circle class definition
class Circle: public BasicShape
{
private:
double radius;
public:
Circle(double a, string n, double r) :BasicShape(a,n)
{
radius=r;
}
//Override calcArea
void calcArea()
{
area =3.14159 * radius * radius;
}
//Override pritn method
void print()
{
cout<<"Radius : "<<radius<<endl;
cout<<"Area : "<<area<<endl;
cout<<"Name : "<<BasicShape::getName()<<endl;
}
};
//poly method that takes BasicShape object
void poly (BasicShape *bs)
{
bs->calcArea();
bs->print();
}
int main()
{
//Create a class object
Circle c(0,"Round",10);
//set circle object address to the basic shape
BasicShape *bs=&c;
//call poly
poly(bs);
system("pause");
return 0;
}
---------------------------------------------------------------------------
Sample Output:
Radius : 10
Area : 314.159
Name : Round
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.