Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Polymorphism Question: Define an abstract base class called BasicShape . The

ID: 3837085 • Letter: C

Question

C++ Polymorphism Question:

Define an abstract base class called BasicShape. The BasicShape class should

have the following members: (Reference: Fig 12.9 and 12.10)

  

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 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

Define a class named Circle. It should be derived from the BasicShape class.

It should have the following members: (Reference: Fig 12.11 and 12.12)

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 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.

After you have created these classes, create a test program (Reference: Fig 12.17)

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.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

My Code: (I change the access to public in my derived class, but it needs to be private. How do i modify my code to do this?)

#include

#include

using namespace std;

class BaseClass { //This is the Base Class (1)

protected:

double area;

private:

string name;

public:

BaseClass(double a,string n )

: area(a), name(n)

{}

virtual void calcArea(){

  

}

void print(){

cout << area << endl;

}

string getName(){

return name;

}

};

class Circle : public BaseClass { //This is the derived class Circle of BaseClass (2)

private:

double radius;

public:

Circle(double a, string n ,double r)

:BaseClass(a,n), radius(r)

{}

void calcArea(){

area = 3.14159 * radius * radius;

}

void print(){

cout << "Radius: " << radius << endl << "Name: " << BaseClass::getName() << endl;

BaseClass::print();

}

};

void poly (BaseClass * const shapePtr){ //Function that takes BaseClass * parameter (3)

shapePtr->calcArea();

shapePtr->print();

}

int main(){ //main execution

Circle aCircle(0, "Round", 10);

BaseClass *polyCircle = &aCircle;

poly(polyCircle);

}

Explanation / Answer

The code that you have pasted for the question is almost 95% correct except the declaration of pure virtual function. I have fixed that . Also changed the name of the class to BaseClass to BasicShape as mentioned in the question. Other things are correct to answer the question. What are you referring when you say "(I change the access to public in my derived class, but it needs to be private. How do i modify my code to do this?)" . If you can be precise, I can help you out. Thanks.

Modified code and output

#include <iostream>

using namespace std;
class BasicShape { //This is the Base Class (1)
protected:
double area;
string name;
public:
BasicShape(double a, string n ): area(a), name(n)
{}
virtual void calcArea()=0; //pure virtual function

//virtual function to print only area
virtual void print(){
cout << "Area: "<< area << endl;
}

string getName(){
return name;
}
};

class Circle : public BasicShape { //This is the derived class Circle of BasicShape (2)
private:
double radius;
public:
Circle(double a, string n ,double r)
:BasicShape(a,n), radius(r)
{}
void calcArea(){
area = 3.14159 * radius * radius;
}
void print(){
cout << "Radius: " << radius << endl << "Name: " << BasicShape::getName() << endl;
BasicShape::print();
}
};

void poly (BasicShape * const shapePtr){ //Function that takes BasicShape * parameter (3)
shapePtr->calcArea();
shapePtr->print();
}

int main(){ //main execution
Circle aCircle(0, "Round", 10);
BasicShape *polyCircle = &aCircle;
poly(polyCircle);
}

output

Radius: 10
Name: Round
Area: 314.159

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote