| < Day Day Up > |
|
A pure virtual function is a function with no implementation. In the Foo examples used in the previous sections, the getI() function is implemented in the Foo class. It is a virtual function and if a derived class does not provide an overriding version of getI(), the Foo version will be used.
However, in object-oriented design, base classes are used primarily to declare an interface only and the functions declared will have no implementation in the base class. The implementation of the interface, meaning, the implementation of all the interface functions declared in the base class, is left to derived classes.
A pure virtual function is declared by setting the function declaration to zero. This is done via the assignment operator as shown in the following example.
virtual void f() = 0;
Here, a pure virtual function named f() is declared. The declaration of a pure virtual function can be thought of as saying to the compiler “Don’t expect to find a definition of this function in this class. Look to derived classes for the implementation!”
Now, what is the use of a pure virtual function? With pure virtual functions you can create abstract base classes which can then be used to generalize your object-oriented designs. This topic is discussed in the next section.
| < Day Day Up > |
|