Jump to content

Pure virtual function

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Cheesy UK (talk | contribs) at 12:47, 21 March 2004 (New page.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

A pure virtual method is one which has a declaration (a 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 methods only make sense (and is only legal) in an inheritance tree.

In C++, classes which have one or more pure virtual methods cannot be instantiated. This is because if the user of the class tried to call a pure virtual method, the compiler would not have any code to execute!

Thus in C++, pure virtual methods force deriving classes to implement a piece of functionality to fulfil the pure virtual method.

A real world example may be a class of type account. The designer may decide it to be 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'.