< previous page page_251 next page >

Page 251
LISTING 16.1 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
32:
33:      // Constructors
34:      Dog();
35:      ~Dog();
36:
37:      // Accessors
38:      BREED GetBreed() const;
39:      void SetBreed(BREED);
40:
41:      // Other methods
42:      // WagTail()const;
43:      // BegForFood()const;
44:
45:   protected:
46:      BREED itsBreed;
47:   };

This program has no output because it is only a set of class declarations without their implementations. Nonetheless, there is much to see here.
Analysis: On lines 627, the Mammal class is declared. Note that in this example, Mammal does not derive from any other class. In the real world, mammals do derivethat is, mammals are kinds of animals. In a C++ program you can represent only a fraction of the information you have about any given object. Reality is far too complex for a program to capture all of it, so every C++ hierarchy is an arbitrary representation of the data available. The trick of a good design is to represent the areas that you care about in a way that maps back to reality in a reasonably faithful way.
The hierarchy has to begin somewhere; this program begins with Mammal. Because of this decision, some member variables that might properly belong in a higher base class are now represented here. For example, certainly all animals have an age and weight; so if Mammal derived from Animal, we might expect it to inherit those attributes. As it is, the attributes appear in the Mammal class.
To keep the program reasonably simple and manageable, only six methods have been put in the Mammal classfour accessor methods, Speak(), and Sleep().
The Dog class inherits from Mammal, as indicated on line 29. Every Dog object will have three member variables: itsAge,itsWeight, and itsBreed. Note that the class declaration for Dog does not include the member variables itsAge and itsWeight. Dog objects inherit these variables from the Mammal class, along with all Mammal's methods except the copy operator, the constructors, and the destructor.

 
< previous page page_251 next page >

If you like this book, buy it!