INTRODUCTION TO
PROGRAMMING IN
C++
Programming Project: Extending the
Marine Biology Case Study
Using Inheritance
Alyce Brady
Kalamazoo College
Extend the Marine Biology case study simulation to support fish, sharks,
and insects, all of which eat, breed, move, and, eventually, die.
Fish, sharks, and insects are all swimming animals, so you should
abstract the common behavior of all three classes into a
SwimmingAnimal superclass.
-
Processing Order:
-
All sharks are processed first, then the fish, and finally the insects.
-
Breeding:
-
If an animal is of breeding age, and if the time since its last meal
is no more than 2 time steps, then it breeds. It does this by
replacing itself with up to five children, one in the its own
position and one in each of the empty neighboring positions to the
North, South, East, or West. The newly created children (including
the one that replaces the original animal) are all of age 0. The
number of time steps since their last meal is also 0.
-
Eating:
-
If a fish or shark is not at the correct age to breed, then it attempts to
eat a random neighboring animal lower in the food chain, moving to that
animal's location in the process and resetting the time since the
animal's last meal to 0. If there are no neighboring animals
to eat, then the number of time steps since the animal's last meal is
incremented.
-
Moving:
-
If an animal has not bred or moved as a result of eating, then it
randomly chooses an empty neighboring position to which to move.
-
Aging and Dying:
-
After breeding, eating, or moving, an animal ages one time step. If
it reaches its expected life span, or if it has not eaten within the
last three time steps (fish and sharks only), then the animal dies.
(In other words, it is removed from the environment.)
-
Species-Specific Information:
-
- Sharks breed at age 5 and die when they reach age 7. They eat
fish.
- Fish breed at age 3 and die when they reach age 5. They eat
insects.
- Insects breed at age 1 (but not age 0!) and die when they reach age 3.
They probably eat something, but whatever it is is not represented in
our simulation.
-
Miscellaneous:
-
- In order to recognize whether a neighbor is a potential feast, your
program will need to be able to ask any animal what its type is. This
is not as easy to do in C++ as we might like, so you should provide a
function in the superclass that returns a string that indicates the
type. Each subclass will obviously have to redefine the function to
return the proper string. You can use this function in client code
without hard-coding what the string is, as in the following example.
// Assume that "animal" is a pointer to some kind of animal.
Fish f;
if ( animal->Type() == f.Type() )
// lunch!
Whatever the string is that Type returns, animal's type
will only match f's type if animal is a fish.
- Remember that the objects you are dealing with are clones. Therefore,
it is important that you update the environment
after the animal has done everything for the time
step (including aging). Any changes made to the clone object after a
call to Update will not be reflected in the environment.