Multiple Inheritance

Later versions of C++ introduced a “multiple inheritance” model for inheritance. In a multiple-inheritance graph, the derived classes may have a number of direct base classes. Consider the graph in Figure 9.3.

Figure 9.3   Simple Multiple-Inheritance Graph

The diagram in Figure 9.3 shows a class, CollectibleString. It is like a Collectible (something that can be contained in a collection), and it is like a String. Multiple inheritance is a good solution to this kind of problem (where a derived class has attributes of more than one base class) because it is easy to form a CollectibleCustomer, CollectibleWindow, and so on.

If the properties of either class are not required for a particular application, either class can be used alone or in combination with other classes. Therefore, given the hierarchy depicted in Figure 9.3 as a basis, you can form noncollectible strings and collectibles that are not strings. This flexibility is not possible using single inheritance.