Home Segments Index Top Previous Next

462: Mainline

Given that the display_short_name virtual function defined in the railroad_car class should be shadowed in every situation, you do not expect that version ever to be executed. You can signal this expectation by replacing the existing virtual function by what is called a pure virtual function. Such functions signal an error whenever called.

Syntactically, pure virtual functions are like ordinary virtual functions, except that the body is replaced, by convention, with = 0;. Hence, the railroad_car class could be defined this way:

class railroad_car { 
  public: railroad_car ( ) { } 
          virtual void display_short_name ( ) = 0; 
}