Expert C Programming

(Jeff_L) #1

The Key Idea: Inheritance


Deriving one class from another such that all of the other's characteristics are automatically
available. Being able to declare types which share some or all of the characteristics of
previously-declared types. Being able to share some characteristics from more than one
parent type.


Inheritance usually provides increasing specialization as you go from a simple base class (e.g. vehicle)
to a more specific derived class (e.g. passenger car, fire truck, or delivery van). It could equally subset
or extend the available operations, though. The shape class seems to be the popular example of
choice in the C++ literature. From a basic shape, more specialized configurations of circle,
square, and pentagon can be derived. We think it makes more sense if we first consider a real-
world example of "class inheritance" in the Linnaean taxonomy of the animal kingdom (see Figure 11-1),
and a similar example showing how it relates to the existing C type model.


Figure 11-1. Two Real-World Examples of an Inheritance Hierarchy

In the example above:



  • The phylum chordata contains every creature that has a notochord (roughly, a spinal cord),
    and only those creatures. There are 32 phyla in the animal kingdom in all.

  • All mammals have a spinal cord. They inherit it by virtue of being "derived" from the
    chordata phylum. Mammals also have specialized characteristics: they feed their young milk,
    they have only one bone in the lower jaw, they have hair, a certain bone configuration in the
    inner ear, two generations of teeth, and so on.

Free download pdf