共用方式為


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 the following figure.

Simple Multiple-Inheritance Graph

Basic multiple-inheritance

The diagram in the figure 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 the above figure, you can form noncollectible strings and collectibles that are not strings. This flexibility is not possible using single inheritance.

See Also

Reference

Multiple Base Classes

Overview of Derived Classes

Multiple Inheritance