Home Segments Index Top Previous Next

503: Mainline

You could, of course, define a display_capacity member function as follows, in the box_car class, relying on values for the height, width, and length member variables inherited from the box class:

virtual void display_capacity ( ) {cout << height * width * length;} 

The blatant defect of this approach is that you already have a perfectly good function for computing box volumes defined as a box class member function. You should make use of that function, rather than duplicating its innards.

box <-- Volume function   railroad_car <-- Virtual display_capacity 
 ^      defined here         ^             function announces 
 |                           |             presence of virtual 
 |  *------------------------*             functions lower in the 
 |  |                                      hierarchy 
 |  |            
box_car <-- Virtual display_capacity function computes volume