WEST VIRGINIA UNIVERSITY

COLLEGE OF ENGINEERING

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

CPE 291C COMPUTER ENGINEERING LABORATORY ON OBJECT-ORIENTED PROGRAMMING IN C++

LAB #8

 Objectives:

 Practice the use of Multiple class inheritance and the concept of polymorphism using virtual functions.

Procedure:

Part I:

  1. Program ex9.prj in the examples directory defines the class Stack for stack type data objects as a derived class from class List. The print member function in class list is declared as a virtual function. Delete the keyword virtual, run the program, and observe the difference between the two implementations. Given the meaning of a virtual function discussed in class, comment on the need for using virtual functions in this example.
  2. The program listdemo.prj shows a simple example of using polymorphism. Notice that the member functions show(), hide(), and Drag() of class Point are all declared as virtual functions. Comment on the need for declaring these functions as virtual functions in this example. Is it also useful to declare the member functions show() and hide() of class Circle to be virtual ?, if so declare the functions as virtual and modify function main() to illustrate the use of this change.

Part II: Simulating a slot machine

Consider the program that simulates the actions in a slot machine Slot.cpp. The slot machine consists of three turning wheels randomly displaying colored shapes in one row. The program displays a row of randomly changing shapes. The rate of change should slow down and then reach zero to simulate the effect of the wheels slowing down and coming to a stop.. The shapes needed for the slot machine program are: a Cherry drawn as a red Ball; a Grape drawn as a blue Ball, a square drawn as a white rectangle with equal sides; and a pyramid drawn as a green triangle with equal sides. The program contains the definition of class noshape which is defined to introduce the operation of erasing a displayed shape. This operation is needed in order to display different shapes on the same place on the screen to mimic a turning wheel. Class Wheel defines the shapes in a wheel and provides a function to draw one of the shapes at random. Function main simulates the actions using the sound(), delay(), and nosound() library functions. You are asked to do the following:

  1. Run the program and observe its operation using different values for the loop count and delay time values in main(). Comment on the rational for using multiple inheritance in this program.
  2. Specify which member function in the above program should be declared as virtual function and why? Modify the code to make use of the concept of polymorphism. (Hint implement the shapes in the Wheel class by an array of type pointer to Shape, add a member function to the class to populate the elements of this array, other member functions of Wheel as well as the main() routine should also be changed).