Expert C Programming

(Jeff_L) #1

  • Primates inherit all the characteristics of mammals (including the quality of having a spinal
    cord, which mammals inherited from chordates). Primates are further distinguished by
    forward-facing eyes, a large braincase, and a particular pattern of incisor teeth.

  • The hominidae family inherits everything from primates and more distant ancestors. It adds to
    the class the unique specialization of a number of skeletal modifications suitable for walking
    upright on two feet. The homo sapiens species is now the only species alive within this family.
    All other species have become extinct.


To be a little more abstract, the hierarchy of types in C can be similarly analyzed:



  • All types in C are either composite (types like arrays or structs, which are composed of
    smaller elements) or scalar. Scalar types have the property that each value is atomic (it is not
    composed of other types).

  • The numeric types inherit all the properties of scalar types, and they have the additional
    quality that they record arithmetic quantities.

  • The integer types inherit all the properties of numeric types, and they have the additional
    characteristic that they only operate on whole numbers (no fractional quantities).


The type char is a smaller range within the values in the integer family.


Although we can amuse ourselves by showing how inheritance can theoretically be applied to the
familiar C types, we note that this model is of no practical use to a C programmer. C does not allow
the programmer to create first class new data types, much less data types that inherit attributes from
other data types. So a programmer cannot use the type hierarchy in real programs. An important part
of OOP is figuring out the hierarchies of the abstract data types in your application. The major novelty
that C++ provides, which cannot easily be accomplished by disciplined use of C, is inheritance.
Inheritance allows the programmer to make the type hierarchies explicit, and to use the relationships
to control code.


Let's invent a class Apple that has every characteristic of fruit, and two specialized operations of its
own. The two things that are done with apples that are not generally done with other kinds of fruit, are:



  • bobbing for apples. You can't bob for pears, for example, as they are denser than apples and
    sink. Apple bobbing is implemented by the method bob_for().

  • making candy apples ("toffee apples" to the British). People don't make caramel-covered
    grapes, not even in California. Making candy apples is implemented by the method
    make_candy_apple().


So we make Apple a derived class that inherits all the Fruit class operations and adds these two
specializations of its own. Don't get hung up on how these methods might be implemented. Obviously,
they are far removed from usual computing. Remember, we're concentrating on the new concepts,
without getting caught up in specific algorithms.


Software Dogma

Free download pdf