Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 15, Problem 3P

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;

Blurred answer
Students have asked these similar questions
Create a final program in Python 3 that meets the requirements outlined below. Create an automobile class that will be used by a dealership as a vehicle inventory program.  The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage Your program should have appropriate methods such as: constructor add a new vehicle remove a vehicle update vehicle attributes At the end of your program, it should allow the user to output all vehicle inventory to a text file.
Summary In this lab, you create a derived class from a base class, and then use the derived class in a Python program. The program should create two Motorcycle objects, and then set the Motorcycle’s speed, accelerate the Motorcycle object, and check its sidecar status. Instructions Open the file named Motorcycle.py. Create the Motorcycle class by deriving it from the Vehicle class. Call the parent class __init()__ method inside the Motorcycle class's __init()__ method. In theMotorcycle class, create an attribute named sidecar. Write a public set method to set the value for sidecar. Write a public get method to retrieve the value of sidecar. Write a public accelerate method. This method overrides the accelerate method inherited from the Vehicle class. Change the message in the accelerate method so the following is displayed when the Motorcycle tries to accelerate beyond its maximum speed: "This motorcycle cannot go that fast". Open the file named MyMotorcycleClassProgram.py. In the…
#Python IDLE: #Below is my Pizza class ,how would I write the function described in the attached image,based on this class? # The Pizza class should have two attributes(data items): class Pizza:     # The Pizza class should have the following methods/operators):     # __init__ -     # constructs a Pizza of a given size (defaults to ‘M’)     # and with a given set of toppings (defaults to empty set).     def __init__(self, size='M', toppings=set()):         self.size = size         self.toppings = toppings     # setSize – set pizza size to one of ‘S’,’M’or ‘L’     def setSize(self, size):         self.size = size     # getSize – returns size     def getSize(self):         return self.size     # addTopping – adds a topping to the pizza, no duplicates, i.e., adding ‘pepperoni’ twice only adds it once     def addTopping(self, topping):         self.toppings.add(topping)     # removeTopping – removes a topping from the pizza     def removeTopping(self, topping):…

Chapter 15 Solutions

Problem Solving with C++ (9th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3); Author: CS Dojo;https://www.youtube.com/watch?v=8yjkWGRlUmY;License: Standard YouTube License, CC-BY