BIG C++
Cay Horstmann & Timothy Budd

Laboratory Notebook
Chapter 21 – Polymorphism


Your name:

Your email address:

Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.


Lab Objectives

To gain experience with

        describing the information contained within a class hierarchy

        the concept of a polymorphic variable and its relationship to class hierarchies

        the interaction between polymorphism and memory management

        how to distinguish between virtual and non-virtual overriding

        the concept of downcasting

        multiple inheritance

        designing and using software frameworks


 

P1. Shapes and Houses

 

In this problem, you will create two classes. The first, called Shape, serves as a base class for representing a generic shape object. Implement this class such that an object of its type can be drawn to the screen at a specified location. Furthermore, this class should define a single virtual member function named draw() for drawing the shape to the screen, a class constructor for initializing the object, and two accessor functions for determining the shape’s location for drawing. You should not provide implementation for the virtual draw() function within this class. The class definition is provided below. Provide implementation for the remaining class functions.

 

The second class is a derived class called House. This class defines and implements its inherited draw() function. Upon invoking the draw() function, the House class generates a simple line-drawing of a house by connecting five straight lines. Use the graphics library provided in Chapter 3 for this and all other problems in this lab. The position to draw the house on the screen should be initialized by its constructor. In turn, that constructor should initialize the private Shape variables accordingly.

 

Remember that all code for this lab should be written using the graphics library described in Chapter 3.

 

The class definitions are provided below.

 

class Shape

{

public:

virtual void draw() = 0;

Shape(double iXpos, double iYpos);

double getXPos();

double getYPos();

private:

double xPos;

double yPos;

};

 

Submit all code for the shape class in the space below.

 

 

class House : public Shape

{

public:

House(double iXpos, double iYpos);

virtual void draw();

};

 

Submit all code for the house class in the space below.


R1. Polymorphic Variables

Write a small main() function that creates a polymorphic variable from the previous two classes and then draws the contents of that variable, namely a House object, to the screen.

Submit your answer in the space below.


P2. Moveable Cars

 

In this problem, you will create two additional classes called Moveable and Car. The purpose of the Moveable class is to serve as a base class for any object that may be drawn to the screen at locations other than its initial position. This class has a virtual member function named move() for updating the relative shift of any derived object’s location. Do not implement the move() function within this class. The default constructor should set Moveable’s protected member variables to zero. The variables have been made protected in order to allow you some freedom in your programming.

 

Derive the Car class from both Moveable and Shape. In order to draw a car, use only four line segments. The class definitions are provided below. The car’s inherited draw() function should draw the car object to the screen, as was done for the house class, taking into account any stored shifts within the Moveable class. The car constructor takes two arguments to initialize Shape’s private variables.

Implement the inherited move() function for this class. The move() function should increment the protected Moveable variables by the amount specified in the function’s arguments. Therefore, if the first call to move sets the shifts to (3,1), then the next call to the move() function, with the arguments (-3, -1), should reset the shifts to zero.

 

class Moveable

{

public:

virtual void move(double ixShift, double iyShift) = 0;

Moveable();

protected:

double xShift;

double yShift;

};

 

Provide all Moveable code in the space below.

 

class Car : public Shape, public Moveable

{

public:

Car(double iXPos, double iYPos);

virtual void move(double ixShift, double iyShift);

virtual void draw();

};

Provide all Car code in the space below.


R2. Virtual Destructors

Any class that defines at least one virtual member function should also include a virtual destructor. Provide a virtual destructor for each of the four classes constructed. Within the body of each destructor, include code to display text information indicating that it was called.

Provide a virtual destructor for the Shape class in the space below.

Provide a virtual destructor for the House class in the space below.

Provide a virtual destructor for the Moveable class in the space below.

Provide a virtual destructor for the Car class in the space below.


P3. Shape Drawing

 

Write a small program that creates a vector of Shape* objects. The Shape pointers should point to both House and Car objects. Iterate through the vector and draw each object to the screen by invoking the draw() function.

 

Provide your answer in the space below.


R3. Run-Time Information

 

Write code that determines the type of each polymorphic variable within the Shape vector created in Problem P3. Perform this task using two different techniques.

 

Provide your answer, using the typeid operator, in the space below.

Provide your answer, using the dynamic cast operator, in the space below.


P4. Drawing Moveables

Modify the program created in Problem P3 such that all moveable objects are moved by a certain amount before drawing them to the screen. Use a dynamic cast to determine whether each object pointed to by a pointer within the Shape* vector is a moveable object.

Submit all code in the space below.


R4. Software Reuse

What topics covered within this programming example facilitate software reuse?

Provide your answers in the space below.


Do not forget to send your answers when you are finished.