Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
9th Edition
ISBN: 9780133862218
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
Implement the Rectangle class as discussed this week.  Use the class declaration below.  Your job is to implement the functions. Implement the missing functions yourself. class Rectangle{  private:    double width;    double length;    char *name;  void initName(char *);  public://constructorsRectangle();Rectangle(double, double,              char*);//destructor    ~Rectangle();    void setWidth(double);    void setLength(double);    void setWidth(char *);    void setLength(char *);    void setName(char *);    double getWidth() const;    double getLength() const;    void printName() const    { cout << name; }}; A few notes on the functions: initName(char *): this is a private member function.  It should be the only function which dynamically allocates a char array to hold the name.   setName(char *): this is a public member function which changes the name of the rectangle to a new name.  It does not dynamically allocate memory, it only changes name. Demonstrate your class works…
Read out the following program carefully and explain each mentioned section (colored) of this program. Plus which type of inheritance this is?. #include <iostream> using namespace std;   class A {    public:    int a = 5;    A() {       cout << "Constructor for class A" << endl;    } }; class B {    public:    int b = 10;    B() {       cout << "Constructor for class B" << endl;    } }; class C: public A, public B {    public:    int c = 20;    C() {       cout << "Constructor for class C" << endl;       cout<<"Class C inherits from classes" << endl;    } }; int main() {    C obj;    cout<<"a = "<< obj.a <<endl;    cout<<"b = "<< obj.b <<endl;    cout<<"c = "<< obj.c <<endl;    return 0; }
Suppose, we have two related (child) classes named Doctor and Patient. Data membersrequired by each class are given below. data members of doctors name type remarks name string full name  of doctor age int age of person spec string speialization of doctor         data members of patient  name type remarks name string full name of person age int age of he person diag string diagnosed problem Using the information given above, create the following three classes. a. A parent class named Person containing common data members and functionality ofthe child classes; Doctor and Patientb. The class Doctor which is publicly inherited from the class Personc. The class Patient which is publicly inherited from the class Person Each class must contain at least two constructors in addition to the appropriate set() and show() functions. Functions of child classes should call appropriate functions of the parent class to perform the common functionality. All the function…

Chapter 15 Solutions

Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
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