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:
“One of the fundamental reasons why so many doctors become cynical and disillusioned is precisely because, when the abstract idealism has worn thin, they are uncertain about the value of the actual lives of the patients they are treating. This is not because they are callous or personally inhuman: it is because they live in and accept a society which is incapable of knowing what a human life is worth.”
—John Berger (b. 1926)
“When we of the so-called better classes are scared as men were never scared in history at material ugliness and hardship; when we put off marriage until our house can be artistic, and quake at the thought of having a child without a bank-account and doomed to manual labor, it is time for thinking men to protest against so unmanly and irreligious a state of opinion.”
—William James (18421910)
“So the soul, that drop, that ray
Of the clear fountain of eternal day,
Could it within the human flower be seen,
Remembering still its former height,
Shuns the sweet leaves and blossoms green;
And, recollecting its own light,
Does, in its pure and circling thoughts, express
The greater heaven in an heaven less.”
—Andrew Marvell (16211678)
“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 Fortunewhat 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 (18951985)
“When Western people train the mind, the focus is generally on the left hemisphere of the cortex, which is the portion of the brain that is concerned with words and numbers. We enhance the logical, bounded, linear functions of the mind. In the East, exercises of this sort are for the purpose of getting in tune with the unconsciousto get rid of boundaries, not to create them.”
—Edward T. Hall (b. 1914)