virtual
Syntax:
virtual return-type name( parameter-list );
virtual return-type name( parameter-list ) = 0;
The virtual keyword can be used to create virtual functions, which
can be overridden by derived classes.
- A virtual function indicates that a function can be overridden
in a subclass, and that the overridden function will actually be
used.
- When a base object pointer points to a derived object that
contains a virtual function, the decision about which version of
that function to call is based on the type of object pointed to by
the pointer, and this process happens at runtime.
- A base object can point to different derived objects and have
different versions of the virtual function run.
If the function is specified as a pure virtual function (denoted
by the = 0), it must be overridden by a derived class.
Example code:
Related topics: