Examine the file named INHERIT2.CPP carefully and you will notice that it is a repeat of the last example program with a few minor changes.

You will notice that the derived classes named car and truck do not have the keyword public prior to the name of the base class in the first line of each. The keyword public, when included prior to the base class name, makes all of the methods defined in the base class available for use in the derived class just as if they were defined as part of the derived class. Therefore, in the previous program, we were permitted to call the methods defined as part of the base class from the main program even though we were working with an object of one of the derived classes. One example of when we did this, was when we sent a message to the sedan to get its weight in an output statement of the main program.

In the present program, without the keyword public prior to the base class name, the only methods available for objects of the car class, are those that are defined as part of the class itself, and therefore we only have the methods named initialize() and passengers() available for use with objects of class car. In this program, the only inheritance is that of variables since the two variables are inherited into objects of class car.

When we declare an object of type car, according to the definition of the C++ language, it contains three variables. It contains the

one defined as part of its class named passenger_load and the two that are part of its parent class, wheels and weight. All are available for direct use within its methods because of the use of the keyword protected in the base class. The variables are a part of an object of class car when it is declared and are stored as part of the object. We will show you the details of access to the parent class variables within derived classes shortly in this chapter. For now, we will return to the use of the subclasses in this example program.

The observant student will notice that several of the output statements have been commented out of the main program since they are no longer legal or meaningful operations. Lines 57 through 59 have been commented out because the methods named get_weight() and wheel_loading() are not inherited into the car class without the keyword public in the car class definition. You will notice that initialize() is still available but this is the one in the car class, not the method of the same name in the vehicle class.

Moving on to the use of the truck class in the main program, we find that lines 63 and 65 are commented out for the same reason as given above, but lines 66 and 67 are commented out for an entirely different reason. Even though the method named efficiency() is available and can be called as a part of the truck class, it cannot be used because we have no way to initialize the wheels or weight of the truck objects. We can get the weight of the truck objects, as we have done in line 106, but since the weight has no way to be initialized, the result is meaningless and lines 66 and 67 are commented out.

As you have surely guessed by now, there is a way around all of these problems and we will cover them shortly. In the meantime, be sure to compile and execute this example program to see that your compiler gives the same result. It would be a good exercise for you to reintroduce some of the commented out lines to see what sort of an error message your compiler issues for these errors.