Table of Contents
Previous Section Next Section

virtual

The virtual function specifier creates a virtual function. A virtual function is a member of a base class that can be overridden by a derived class. If the function is not overridden by a derived class, the base class' definition is used.

A pure virtual function is a member function that has no definition. This means that a pure virtual function must be overridden in a derived class. A pure virtual function is prototyped like this:

virtual ret-type fname(param-list) = 0;

Here, ret-type is the return type of the function, fname is the function's name, and param-list specifies any parameters. The important feature is the = 0. This tells the compiler that the virtual function has no definition relative to the base class.

Runtime polymorphism is attained when virtual functions are accessed through a base class pointer. When this is done, the type of object pointed to determines which version of the virtual function is called.


Table of Contents
Previous Section Next Section