Home Segments Index Top Previous Next

513: Mainline

Recall that the box_car and box class definitions, as developed so far, are as follows:

class box { 
  public: double height, width, length; 
    // Default constructor: 
    box ( ) { } 
    // Argument-bearing constructor: 
    box (double h, double w, double l) { 
       height = h; width = w; length = l; 
    } 
    // Volume member function: 
    double volume ( ) { 
      return height * width * length; 
    } 
}; 
class box_car : public railroad_car, public box { 
  public: 
    // Default constructor: 
    box_car ( ) : box (10.5, 9.5, 40.0) { } 
    // Displayers: 
    virtual void display_short_name ( ) { 
      cout << "box"; 
    } 
    virtual void display_capacity ( ) { 
      cout << volume ( ); 
    } 
}; 

Although these class definitions work, you can improve them in a variety of ways using the C++ protection mechanisms described in this chapter.