Home Segments Index Top Previous Next

517: Mainline

Having moved the dimension member variables to the protected part of the definition, you make their values available not only in the volume function defined in the box class definition, but also in any function you might choose to define in the box_car subclass. You could, for example, define a display_height member function in the box_car class:

class box_car : public railroad_car, public box {
  public:
    // Default constructor:
    box_car ( ) : box (10.5, 9.5, 40.0) { }
    // Displayers:
    virtual void display_height ( ) {cout << height;}  
    virtual void display_short_name ( ) {cout << "box";} 
    virtual void display_capacity ( ) {cout << volume ( );} 
}; 

Note that, once you move the dimension member variables into the protected part of the box class definition, a member function defined in the box_car class definition can write values into those member variables, as well as read from them.