MULTIPLE INHERITANCE

A major recent addition to the C++ language is the ability to inherit methods and variables from two or more parent classes when building a new class. This is called multiple inheritance, and is purported by many people to be a major requirement for an object oriented programming language. Some writers, however, have expressed doubts as to the utility of multiple inheritance. To illustrate the validity of this, it was not easy to think up a good example of the use of multiple inheritance as an illustration for this chapter. In fact, the resulting example is sort of a forced example that really does nothing useful. It does however, illustrate the mechanics of the use of multiple inheritance with C++, and that is our primary concern at this time.

The biggest problem with multiple inheritance involves the inheritance of variables or methods from two or more parent classes with the same name. Which variable or method should be chosen as the inherited variable or method if two or more have the same name? This will be illustrated in the next few example programs.

SIMPLE MULTIPLE INHERITANCE

An examination of the file named MULTINH1.CPP will reveal the definition of two very simple classes in lines 4 through 27 named moving_van and driver.

In order to keep the program as simple as possible, all of the member methods are defined as inline functions. This puts the code for the methods where it is easy to find and study. You will also notice that all variables in both classes are declared to be protected so they will be readily available for use in any class which inherits them. The code for each class is kept very simple so that we can concentrate on studying the interface to the methods rather than spending time trying to understand complex methods. As mentioned previously, chapter 12 will illustrate the use of non- trivial methods.

In line 30, we define another class named driven_truck which inherits all of the data and all of the methods from both of the previously defined classes. In the last two chapters, we studied how to inherit a single class into another class, and to inherit two or more classes, the same technique is used except that we use a list of inherited classes separated by commas as illustrated in line 30. The observant student will notice that we use the keyword public prior to the name of each inherited class in order to be able to freely use the methods within the subclass. In this case, we didn’t define any new variables, but we did introduce two new methods into the subclass in lines 32 through 39.

We declared an object named chuck_ford which presumably refers to someone named Chuck who is driving a Ford moving van. The object named chuck_ford is composed of four variables, three from the moving_van class, and one from the driver class. Any of these four variables can be manipulated in any of the methods defined within the driven_truck class in the same way as in a singly inherited situation. A few examples are given in lines 47 through 56 of the main program and the diligent student should be able to add additional output messages to this program if he understands the principles involved.

All of the rules for private or protected variables and public or private method inheritance as used with single inheritance extends to multiple inheritance.

Share
Tweet
Share
Pin