|
|
|
|
|
|
|
It is the taxonomist's perspective that interests us at the moment (sorry, Mom). To a taxonomist, a dog is a canine. A canine is a kind of mammal. A mammal is a kind of animal, and so forth. Taxonomists divide the world of living things into kingdom, phyla, class, order, family, genus, and species. You can remember this by remembering that King Philip Came Over From Great Spain. I'm not sure why this is easier, who King Philip was, why he came over, or where exactly Great Spain is; but that's what my ninth-grade teacher told me, and I've remembered it ever since. |
|
|
|
|
|
|
|
|
The taxonomist's hierarchy establishes an is-a relationship. A dog is a canine. We see the is-a relationships everywhere: a Toyota is a kind of car, which is a kind of vehicle. A sundae is a kind of dessert, which is a kind of food. |
|
|
|
|
|
|
|
|
What exactly do we mean when we say something is a kind of something else? We mean that it is a specialization of that thing. That is, a car is a special kind of vehicle. Cars and buses are both vehicles. They are distinguished by their specific characteristics of car-ness or bus-ness, but to the extent that they are vehicles they are identical. Their vehicle-ness is shared. |
|
|
|
|
|
|
|
|
Inheritance and Derivation. |
|
|
|
|
|
|
|
|
The concept dog inherits (automatically gets) all the features of a mammal. Because it is a mammal, we know that it moves and that it breathes airall mammals move and breathe air by definition. The concept of a dog adds the idea of barking, wagging its tail, and so forth to that definition. The dog-ness is specialized, but the mammal-ness is universal to all mammals. |
|
|
|
|
|
|
|
|
We can further divide dogs into hunting dogs and terriers; we can divide terriers into Yorkshire Terriers, Dandie Dinmont Terriers, and so forth. |
|
|
|
|
|
|
|
|
A Yorkshire Terrier is a kind of terrier; therefore, it is a kind of dog; therefore, a kind of mammal; therefore, a kind of animal; and, therefore, a kind of living thing. This hierarchy is represented in Figure 16.1. |
|
|
|
|
|
|
|
|
C++ attempts to represent these relationships by defining classes that derive from one another. Derivation is a way of expressing the is-a relationship. You derive a new class, Dog, from the class Mammal. You don't have to state explicitly that dogs move because they inherit that from Mammal. Because it derives from Mammal, Dog automatically moves. |
|
|
|
|
|
|
|
|
A class that adds new functionality to an existing class is said to derive from that original class. The original class is said to be the new class's base class. |
|
|
|
|
|
|
|
|
If the Dog class derives from the Mammal class, then Mammal is a base class of Dog. Derived classes are supersets of their base classes. Just as dog adds certain features to the idea of a mammal, the Dog class will add certain methods or data to the Mammal class. |
|
|
|
|
|