Use type inheritance to share protocol

Use public base classes or public derivation whenever a collection of classes shares protocol in common. The only reason for a base class to be public is so that a pointer or reference to the derived class can be converted into a pointer or reference to the base class. The most important reason to perform these conversions is to allow for polymorphism (for example, a function that expects a Base& is handed a Derived& instead). Polymorphism occurs when a collection of related classes can be used via the protocol they all have in common. This allows one component of a system to deal with other components at the highest level of abstraction possible (the base class) and ignore any irrelevant details (of the derived class).

Type inheritance is also known as subtyping; deriving a class from a public base class states that the new class is a subtype. This statement places important constraints on the base class and the derived class. If these constraints are not met, serious errors can occur later. Compilers can't catch many of these errors.

If you want to share only portions of a base class' protocol, make the class private and reexport the members you want to make public (or factor that partial protocol out into a separate base). Use public base classes only when you need polymorphism.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker