//-------------------------------------------------- // // Animal.h // // Header file for the Ark inheritance assignment. // //-------------------------------------------------- #include #include // Forward declaration since AnimalList needs to know // about the Animal class. The "Real" animal class comes // below AnimalList. class Animal; // // Utility structure used by the AnimalList class to implement // a linked list // struct AnimalListItem { AnimalListItem* nextItem; Animal* itemPtr; }; // // A list of Animal object pointers. This class is meant to // hold a list of Animal pointers, but it does not do any // operations on the pointers. Specifically, it does not create // or delete any Animal object pointers. The caller is responsible // for managing the pointers. // class AnimalList { public: // Default constructor. Creates an empty animal // list with no elements AnimalList(); // Copy constructor. AnimalList(const AnimalList& al); // Assignment operator AnimalList& AnimalList::operator=(const AnimalList& al); // Destructor. Deletes all of the AnimalListItems that had // been created. Does NOT delete the animal pointers. That // is up to the user ~AnimalList(); // Add an animal pointer to the list. If more than MAXANIMALS // are added, then an error will be printed. // void addAnimal(Animal* ap); // Delete all of the animals in the list. Note that this // is different than the destructor, which deletes all of // the list items, but does not delete the Animals themselves. void deleteAnimals(); // Clear the animal list so that it can be reused // void clear(); // Return the number of animals in the list // int numAnimals() const; // Append an animal list onto this one. Afterwards // this list will be both lists appended together. // void append(const AnimalList& al); // Reset the current iteration through the animal list back // to the beginning. This should be called before starting all // of the calls to nextAnimal() to get the animals. void resetIterator(); // Return the next animal of the current iteration. Returns // NULL if there are no more animals. Code that uses this // routine should look like this: // // alist.resetIterator(); // Animal *ap; // while (ap = al.nextAnimal()) // { // operate on ap // } // Animal* nextAnimal(); // These are private. Pay no attention private: int _numAnimals; AnimalListItem *_firstItem, *_lastItem; AnimalListItem *_iterPosition; // Private helper function to delete all of the list items void _deleteItems(); }; // // Gender enumerated type // enum Gender {genderUnknown, male, female}; // // Abstract base class for Animals on the ark. // class Animal { public: // // Constructor. Build a newborn animal. Allow user to // specify a gender. // Animal(); // // Returns the animals age. // int getAge(); // // Add to the animal's age // void addAge(int months); // // Gender functions // void setGender(Gender sex); int isMale(); int isFemale(); // // Add to the animal's age and then return any offspring // that the animal has had, based on the animal's age. // Mammals, Fish, and Birds breed differently, so this // method will be overriden in those classes // virtual AnimalList ageAndBreed() = 0; // // Print what kind of animal this is. This is abstract // and must be overridden in the derived classes. // virtual void printType() = 0; // // Print the gender of the animal // void printGender(); protected: // // Give birth to another animal of the same type // virtual Animal* giveBirth() = 0; // Pay no attention private: int _age; // Animal's age in months Gender _gender; // Male or female };