The purpose of this assignment is to gain experience with class heirarchies, inheritance, and derived classes. You should define a collection of small classes (details below). To focus on the relationships between classes, we will do a few artificial things, including putting several classes in a single source file, splitting the functions and data rather arbitrarily between base and derived classes, and using cout directly instead of writing output functions that work with arbitrary streams.
Create a collection of classes to represent information about different kinds of animals. Each animal has two member functions. Function describe should write a short description of the animal to the standard output stream cout (without any embedded newline '\n' characters). Function speak should write a sound that the animal produces to cout. For example, if c is a variable of class Cat, then execution of c.describe() might write A cat is a small furry creature on the screen. Execution of c.speak() should write meow or hiss!!! or some other appropriate utterance.
To experiment with inheritance, the collection of animal classes should be structured as follows. All animal classes should be derived from a single base class Critter. Functions describe() and speak() should be defined as public members of Critter so they are part of the interface of all animal classes. Critter should contain a private array of characters to store the description of each animal. This string must be initialized when an animal object is created, and should be written to cout by Critter::describe().
Each different kind of animal should be represented by a class derived from Critter. Each derived class should contain a definition of speak() that writes an appropriate noise to cout (this need only contain a simple statement like cout<<"woof!"; nothing more). The constructor for each derived class should contain an initializer for the Critter part of the animal object to give an appropriate initial value to the description string.
Use your imagination to pick (or invent) animals, descriptions, and sounds that they make. The only requirement is that you must have at least two distinct animal classes derived from Critter.
If you are feeling ambitious, feel free to experiment with more complex class hierarchies. For example, you might want to derive class Cat from Critter, then derive classes Lion, Tiger, and Siamese from Cat. But before you get carried away, be sure that you can get the basic assignment done first.
Class Critter should be an abstract class containing the following data and function members.
Constructor | Critter(char * d) |
Operations | describe(), speak() |
Private data | char description[60] |
Constructor Critter(d) should copy string d to the member variable description. To simplify things, the description can be stored in a static-sized array containing at least 60 characters. Since this class does not need pointers to dynamic data, we can omit the copy constructor, destructor, and assignment operator (operator=). A null constructor (with no parameters) can also be omitted for this assignment, because it doesn't make sense to create a Critter without giving an initial value to description.
Function speak() should be a pure virtual function because there is no generic speak() operation that is appropriate for every Critter. This means that Critter is an abstract class. Individual classes derived from Critter should override speak() as needed.
Each derived class for a specific animal A should contain the following.
Constructor | A() |
Operations | speak() |
The constructor should initialize the description with an appropriate message, using appropriate syntax to pass the message to a Critter constructor.
For this homework assignment, you should put all of the class declarations in a single file named classes.h and all of the implementations in a single file named classes.cpp. Normally, each class declaration would be in a separate .h file and each implementation in a separate .cpp file. That would be overkill here and would greatly complicate the turnin procedures.
You should include a main program and an additional function write_info to test your Critter classes. These functions should be in a file named main.cpp The main program should contain variable declarations to create an instance of each of your classes, then call function write_info for each variable. For example,
int main( ) { Cat c; Dog d; write_info(c); write_info(d); return 0; }
Function write_info should have one parameter, a reference to a Critter (type Critter &). It should use the describe() and speak() methods of the Critter parameter and additional output statements to produce a nicely formatted description of the animal on cout. For this example, the output might be something like
A cat is a small four-legged creature. It says: purrr A dog is a large four-legged creature that chases cats. It says: woof!
We're sure you can come up with something more interesting.
When you have finished, go back to the main homework 5 page and follow the directions for turning in your homework.