Jump to content

Pure virtual function

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by David Levy (talk | contribs) at 00:21, 15 July 2005 (Pure virtual method moved to Pure virtual function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In object-oriented programming, a pure virtual function or pure virtual method is a virtual function that has a declaration (signature), but no definition (implementation). This may be used where it does not make sense to provide a default implementation of a method.

In C++, pure virtual functions only make sense (and are only legal) in an inheritance tree. Classes that have one or more pure virtual functions cannot be instantiated. This is because the compiler would not have any code to execute if the user of the class tried to call a pure virtual function. Thus, in C++, pure virtual functions force deriving classes to implement a piece of functionality to fulfil the pure virtual function.

A real world example may be a class of type account. The designer may deem it undesirable to provide a default implementation of a "Calculate Interest" method, instead leaving the actual implementation to the designers of the derived classes "Current Account" and "Savings Account."