CS12, Lab 9
Week of March 4 – March 8
Topics
·
Projects
with multiple classes
·
Inheritance
There is very
little code to write in this lab.
The challenge here is to look at a project that is composed of multiple
files containing three classes that interact in different ways and understand
how everything relates. The files
are abundantly commented.
There are three places where a short amount of code needs to be added
(and a fourth, longer segment for extra credit).
The classes that make up this program and their interactions are as follows:
Classes
·
Dice objects
generate random numbers between 1 and some upper limit.
·
Balloon
objects simulate hot air balloons that can go up or down a random number of
feet.
·
AutoBalloon
objects are Balloons that can also climb or descend to a specified altitude and
cruise around an altitude.
Interactions
·
Both the
Balloon class and the AutoBallon Class use the Dice class.
·
The
AutoBallon class inherits from the Balloon
class. This means that an AutoBalloon object
is everything that a Balloon object is, and then some.
To do
·
Download the files
dice.h
dice.cpp
balloon.h
balloon.cpp
auto.h
auto.cpp
fly.cpp and
makefile.
The Dice class files are
slightly different from the one used so far (there is an added
constructor).
·
Compile the program by typing make fly ·
Right now all the program does is declare an AutoBalloon
object. Run it to see an
illustration of the order in which the constructors and destructors are
called.
·
Add the
appropriate lines of code to the ascend() and descend() methods of the
AutoBalloon class. The code
segments are very short. Use
Balloon methods (recall that AutoBalloons are Balloons) instead of replicating
code. ·
Add the
required lines of code to the main() (in fly.cc). You need only two statements. ·
Run your
program several times. The output
will be different each time because of the random rolls of the Dice. Extra Credit Complete the
AutoBalloon method cruise(). Add a
call to the method in the main().