Problem

Listed below are definitions of two classes that use inheritance, code for their implement...

Listed below are definitions of two classes that use inheritance, code for their implementation, and a main function. Put the code into appropriate files with the necessary include statements and preprocessor statements so that the program compiles and runs. It should output “Circle has radius 2 and area 12.5664”.

class Shape{public:      Shape();      Shape(string name);      string getName();      void setName(string newName);      virtual double getArea() = 0;private:      string name;};Shape::Shape(){      name="";}Shape::Shape(string name){      this->name = name;}string Shape::getName(){      return this->name;}void Shape::setName(string newName){      this->name = newName;}class Circle : public Shape{public:      Circle();      Circle(int theRadius);      void setRadius(int newRadius);      double getRadius();      virtual double getArea();private:      int radius;};Circle::Circle() : Shape("Circle"), radius(0){ }Circle::Circle(int theRadius) : Shape("Circle"), radius(theRadius){ }void Circle::setRadius(int newRadius){      this->radius = newRadius;}double Circle::getRadius(){      return radius;}double Circle::getArea(){      return 3.14159 * radius * radius;}int main(){      Circle c(2);      cout << c.getName() << " has radius " <<         c.getRadius() << " and area " <<         c.getArea() << endl;      return 0;}

Add another class, Rectangle, that is also derived from the Shape class. Modify the Rectangle class appropriately so it has private width and height variables, a constructor that allows the user to set the width and height, functions to retrieve the width and height, and an appropriately defined getArea function that calculates the area of the rectangle.

The following code added to main should output “Rectangle has width 3 has height 4 and area 12.0”.

Rectangle r(3,4);cout << r.getName() << " has width " <<      r.getWidth() << " has height " <<      r.getHeight() << " and area " <<      r.getArea() << endl;

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 15
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT