Home Segments Index Top Previous Next

524: Mainline

On the other hand, you cannot access the height member variable of a refrigerator_car or box_car object other than in a member function. For example, if you declare a box_car or refrigerator_car variable, you cannot read the height member variable using the class-member operator, because access to that member variable is limited as though the member variable were in the protected part of a class definition:

box_car box_car_variable; 
cout << box_car_variable.height;                 // DEFECTIVE! 
refrigerator_car refrigerator_car_variable; 
cout << refrigerator_car_variable.height;        // DEFECTIVE! 

Note, however, that you can still read the height member variable of a box using the class-member operator:

box box_variable; 
cout << box_variable.height;                     // OK 

Thus, access via box_car objects to the height member variable declared in the box base class definition is limited by the protected nature of the derivation, but for only those class objects belonging to the derived box_car class and its subclasses.