Home Segments Index Top Previous Next

461: Mainline

One virtual function can shadow another, just as an ordinary member function can shadow another. Thus, the display_short_name virtual functions in the engine, box_car, tank_car, and caboose classes shadow the display_short_name virtual member function in the railroad_car class.

Assuming that you never intend to create any individual class objects at the railroad_car level, the only reason to define the display_short_name function in the railroad_car class definition is to inform C++ that lower-level display_short_name virtual functions are defined.

On the other hand, if you fail to define a display_short_name function, either deliberately or accidentally, in, say, the engine class definition, then there will be nothing to shadow the display_short_name function defined in the railroad_car class definition when you attempt to display the short name of an engine. Accordingly, your program will display rrc.