Data Structures - Chapter 14 - Programming Assignment
Implement and the Carnivore Class

Data Structures and Other Objects Using C++
Second Edition
by Michael Main and Walter Savitch
ISBN 0-201-70297-5, Softcover, 816 pages


The Assignment:
You will implement the Carnivore class described in Exercise Exercise 11 on page 650. NOTE: Read all the material below because the solution given in the book has two bugs!
Purposes:
Expose you to the notion of a derived class.
Before Starting:
Read Sections 14.1 and 14.2.
File that you must write:
  1. organism.h: Header file for the Organism class and various derived classes. You don't have to write much of this file. Just copy our version from ~main2270/programs/organism.h and add your name and other information at the top. You'll also need to add the Carnivore class declaration at the bottom.
  2. organism.cxx: This file should contain the implementations of the Organism class and the various derived classes (Plant, Herbivore, and Carnivore). You should start with our version from ~main2270/programs/organism.cxx. You'll need to add the Carnivore's constructor and the chase member function.
Other files that you may find helpful:
  1. ecosystem.cxx: A demonstration program that shows how the new Carnivore class might be used in a program that simulates a small ecosystem. By the way, the program works at the moment, but the constants that I set at the top may not be very stable. At the moment (April 29) these constants cause the foxes to quickly die out and then the goose population explodes. We'll play around with these constants when I discuss the program in class on May 2. Note: In order to compile and run the ecosystem program you'll also need to grab bag8.h, bag8.template, link2.h, and link2.template.
  2. makefile.eco: This is a makefile for the assignment. You can copy it to your directory and change the name to just makefile.

The Carnivore Class
Discussion of the Assignment

You should be able to program this in a few minutes after you read Sections 14.1 and 14.2. However, if your copy of the textbook is the first printing, then the chase member function on page 660 has two bugs:

  1. The correct name of the constant is RAND_MAX (rather than MAX_RAND);
  2. The division rand()/MAX_RAND is incorrectly an integer division. You need to change this so that the result of the division is a double number.


Michael Main (main@colorado.edu)