Virtual Function - Abstract Classes and Pure Virtual Functions

Abstract Classes and Pure Virtual Functions

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class, if that class is not abstract. Classes containing pure virtual methods are termed "abstract"; they cannot be instantiated directly. A subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class. Pure virtual methods typically have a declaration (signature) and no definition (implementation).

As an example, an abstract base class MathSymbol may provide a pure virtual function doOperation, and derived classes Plus and Minus implement doOperation to provide concrete implementations. Implementing doOperation would not make sense in the MathSymbol class, as MathSymbol is an abstract concept whose behaviour is defined solely for each given kind (subclass) of MathSymbol. Similarly, a given subclass of MathSymbol would not be complete without an implementation of doOperation.

Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in C++ are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to, if appropriate.

Pure virtual functions can also be used where the method declarations are being used to define an interface - similar to what the interface keyword in Java explicitly specifies. In such a use, derived classes will supply all implementations. In such a design pattern, the abstract class which serves as an interface will contain only pure virtual functions, but no data members or ordinary methods. In C++, using such purely abstract classes as interfaces works because C++ supports multiple inheritance. However, because many OO languages do not support multiple inheritance, they often provide a separate interface mechanism. An example is the Java programming language.

Read more about this topic:  Virtual Function

Famous quotes containing the words abstract, classes, pure, virtual and/or functions:

    A work of art is an abstract or epitome of the world. It is the result or expression of nature, in miniature. For, although the works of nature are innumerable and all different, the result or the expression of them all is similar and single.
    Ralph Waldo Emerson (1803–1882)

    My plan of instruction is extremely simple and limited. They learn, on week-days, such coarse works as may fit them for servants. I allow of no writing for the poor. My object is not to make fanatics, but to train up the lower classes in habits of industry and piety.
    Hannah More (1745–1833)

    Who knows whither the clouds have fled?
    In the unscarred heaven they leave no wake,
    And the eyes forget the tears they have shed,
    The heart forgets its sorrow and ache;
    The soul partakes the season’s youth,
    And the sulphurous rifts of passion and woe
    Lie deep ‘neath a silence pure and smooth,
    Like burnt-out craters healed with snow.
    James Russell Lowell (1819–1891)

    Tragedy dramatizes human life as potentiality and fulfillment. Its virtual future, or Destiny, is therefore quite different from that created in comedy. Comic Destiny is Fortune—what the world will bring, and the man will take or miss, encounter or escape; tragic Destiny is what the man brings, and the world will demand of him. That is his Fate.
    Susanne K. Langer (1895–1985)

    Let us stop being afraid. Of our own thoughts, our own minds. Of madness, our own or others’. Stop being afraid of the mind itself, its astonishing functions and fandangos, its complications and simplifications, the wonderful operation of its machinery—more wonderful because it is not machinery at all or predictable.
    Kate Millett (b. 1934)