DUPLICATED VARIABLE NAMES

If you will examine the example program named MULTINH3.CPP, you will notice that each base class has a variable with the same name.

According to the rules of inheritance, an object of the driven_truck class will have two variables with the same name, weight. This would be a problem if it weren’t for the fact that C++ has defined a method of accessing each one in a well defined way. You have probably guessed that we will use qualification to access each variable. Lines 38 and 45 illustrate the use of the variables. It may be obvious, but it should be explicitly stated, that there is no reason that the subclass itself cannot have a variable of the same name as those inherited from the parent classes. In order to access it, no qualification would be required.

It should be apparent to you that once you understand single inheritance, multiple inheritance is nothing more than an extension of the same rules. Of course, if you inherit two methods or variables of the same name, you must use qualification to allow the compiler to select the correct one.

Share
Tweet
Share
Pin