C++ virtual function

A virtual function is a member function in the base class that is overridden by a derived class. It allows the function to be redefined in the derived class so that the most appropriate function is called for the object.

  1. Declare the base class with a virtual function.
  2. Define the virtual function in the base class.
  3. Declare the derived class that overrides the virtual function.
  4. Define the overridden virtual function in the derived class.