Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134521176
Author: 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
What are the similarities and differences between massively parallel processing systems and grid computing. with references
Modular Program Structure. Analysis of Structured Programming Examples. Ways to Reduce Coupling. Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.

Chapter 15 Solutions

Problem Solving with C++ (10th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
What are typical arc welding currents and voltages?

Degarmo's Materials And Processes In Manufacturing

In this chapter, we use the metaphor of a cookie cutter and cookies that are made from the cookie cutter to des...

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

The ____________ is always transparent.

Web Development and Design Foundations with HTML5 (8th Edition)

The force of F = 30 N acts on the bracket as shown. Determine the moment of the force about the a a axis of th...

INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)

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
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
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