Home Segments Index Top Previous Next

456: Mainline

Accordingly, you must somehow arrange for your compiled program to determine which display_short_name function to use, rather than having the C++ compiler decide, in advance, at compile time.

You could easily imagine many ways to convey such a message to C++. The way the designers of C++ decided to convey the message is to place a special symbol, virtual, in front of the definition of display_short_name in the railroad_car class:

class railroad_car {
  public: // Constructor:
          railroad_car ( ) { }
          // Virtual display function:               
          virtual void display_short_name ( ) { }    
}; 

Thus, to mark a member function for selection at run time, rather than at compile time, you preface its definition with virtual. Such a function is said to be a virtual member function.

Virtual member functions are called virtual because the information required to determine which function to use is not available—hence is only virtual—at compile time.