CS12, Program 5
Assigned: May 30th,
2002
Due: June 7th,
2002, 11pm
Topics
·
Inheritance
·
Virtual
functions and polymorphism
·
More
pointers
Background
There are many species of frogs, each with its own idiosyncratic
behaviors, but all still frogs.
For example, while all frogs move, some climb and others jump along the
ground. In this example, you will
create two inherited classes that represent two types of Frogs with different
movement.
The
Program
First
you will need to write your classes.
·
The
base class, called Frog. This is
pretty much our basic Frog class, with one additional virtual public method,
display(), to display information about the Frog. Each derived class will have its own display. One additional change to the frog class
is that the walk() method needs to be virtual (add the keyword virtual just
before the prototype in frog.h).
·
A
derived class, ChubbyGroundFrog.
Chubby ground frogs move just like regular Frogs, so they can use the
walk() method of the base class. ChubbyGroundFrog::display() will display
information about the Frog by printing the message (assume the Frog is at
position 5):
Chubby
Ground Frog at position 5.
·
A
derived class, StickyTreeFrog.
Sticky Tree Frogs climb trees.
They use the same random motion as regular Frogs, except that they move
up and down. The position data member now stores the distance from the
ground. An important issue when
moving up and down is that the Frog cannot move into the ground. The StickyTreeFrog class will redefine
its own walk() method that will be very similar to the Frog walk() method,
except that it won’t allow negative numbers. In other words, if the random walk would cause the frog to
move into a negative position, the frog should not move. StickyTreeFrog::display()
will display information about the Frog by printing the message (assume the
Frog is at position 8):
Sticky
Tree Frog at height 8.
·
A
Swamp class that contains a list of pointers to Frogs. You may modify the Swamp from program
4. We will not worry about the
swamp radius here, and you will not need the delete or find methods, A working
partial solution (only the methods you need for this assignment) is available
(files
swamp.h and swamp.cpp).
If you use your own program (please do if it's working), you do not need
to remove the radius or the delete and find methods. Having extra
methods in your program will not hurt it. Deleting frogs that hopped
too far or jumped too high is ok too.
You should make the following changes to the Swamp class:
o
The
node structure should change to store a pointer to a Frog instead of a Frog
object, and the frogs will have to be dynamically allocated.
o
The
Swamp class should have two add() methods, one for each type of frog. Add methods will be almost identical,
except for the type of Frog that is dynamically allocated. Alternately, you can modify the add()
method to take a second parameter that will determine which frog should be
added.
o
The
print() method changes to traverse the list and call display() with each
frog. If p is a pointer to a
FrogNode, and ribbit is the name of the Frog pointer in the node, the call
would be
p->ribbit->display();
Next,
you will write a program, similar to program 4.
·
Create a
swamp.
·
Add 10 frogs
to the swamp, five of each type (in any order).
·
Walk each
the frogs 25 times using the partyHop() method (partyHop() will not change).
·
Print the
contents of the swamp using the print() method.
What
to turn in
Eleven files, all inside one folder with a name of your choice:
·
dice.h
·
dice.cpp
·
frog.h
·
frog.cpp
·
cgfrog.h
·
cgfrog.cpp
·
stfrog.h
·
stfrog.cpp
·
swamp.h
·
swamp.cpp
·
main.cpp
Please
note:
·
Your program must compile and run in Linux.
·
Do
not use any other names or extensions for your files (yes, I mean .cpp and not
.cc). These names are case
sensitive – do not use capital letters anywhere in the file names. Failure to name your files as above
will cost you 20 (twenty) points.
·
Please
make sure that at least one of your files includes your name, login and lab
section. Each is worth 16 points
in this assignment. That’s
1/3 of the grade. Be warned.