Home Segments Index Top Previous Next

237: Mainline

Having defined the container, railroad_car, box, and cylinder classes, you can now define the classes at the bottom of the class hierarchy:

class box_car : public railroad_car, public box { 
  public: box_car ( ) {height = 10.5; width = 9.2; length = 40.0;} 
}; 
class tank_car : public railroad_car, public cylinder { 
  public: tank_car ( ) {radius = 3.5; length = 40.0;} 
}; 
class engine : public railroad_car { 
  public: engine ( ) { } 
}; 
class caboose : public railroad_car { 
  public: caboose ( ) { } 
}; 

Note that none of the class definitions just shown includes any member variables. All box_car, tank_car, engine, and caboose objects have member variables, however, because of the subclass–superclass relations that link the box_car, tank_car, engine, and caboose classes to various superclasses.

Note also that two of the four definitions specify two base classes; such definitions enable multiple inheritance.

Finally, note that the definitions of the box_car and tank_car classes include default constructors that initialize inherited member variables. These classes are sufficiently specific to have sensible default member-variable values. Later, in the hardcopy version of this book, you learn about another way to initialize inherited member variables using constructors that explicitly call other constructors.