Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
virtual is a keyword to denote that a class member function might have different behaviour in its derived classes.
Humans say hello differently. In this example there are two kinds of humans that say hello differently: SilentHuman and LoudHuman:
#include <iostream> |
Note that it is decided that a plain Human cannot say hello. This can be stated by ending the declaration of SayHello with '=0'. This also makes it impossible to create a Human (you can only create (derived) types of Human). This makes Human an abstract base class.
Avoid calling virtual member functions in constructors and destructors [2]. Avoid public virtual member functions; prefer using the Template Method Design Pattern instead [3].
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.