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.
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
Submit all code for the shape class in the space below.
Submit all code for the house class 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;
};
class Car : public Shape, public Moveable
Car(double iXPos, double iYPos);
virtual void move(double ixShift, double iyShift);
virtual void draw();
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.
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.