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:

    “The whole body of what is now called moral or ethical truth existed in the golden age as abstract science. Or, if we prefer, we may say that the laws of Nature are the purest morality.”
    —Henry David Thoreau (1817–1862)

    “The want of education and moral training is the only real barrier that exists between the different classes of men. Nature, reason, and Christianity recognize no other. Pride may say Nay; but Pride was always a liar, and a great hater of the truth.”
    —Susanna Moodie (1803–1885)

    “What sought they thus afar?
    Bright jewels of the mine?
    The wealthy of seas, the spoils of war?—
    They sought a faith’s pure shrine!

    Ay, call it holy ground,
    The soil where first they trod;
    They have left unstained what there they found,—
    Freedom to worship God.”
    —Felicia Dorothea Hemans (1783–1835)

    “Neither dead nor alive, the hostage is suspended by an incalculable outcome. It is not his destiny that awaits for him, nor his own death, but anonymous chance, which can only seem to him something absolutely arbitrary.... He is in a state of radical emergency, of virtual extermination.”
    —Jean Baudrillard (b. 1929)

    “Those things which now most engage the attention of men, as politics and the daily routine, are, it is true, vital functions of human society, but should be unconsciously performed, like the corresponding functions of the physical body.”
    —Henry David Thoreau (1817–1862)