Escolar Documentos
Profissional Documentos
Cultura Documentos
A pure virtual function is a function which has no definition in the base class. Its
definition lies only in the derived class ie it is compulsory for the derived class to provide
definition of a pure virtual function. Since there is no definition in the base class, these
functions can be equated to zero.
Consider following example of base class Shape and classes derived from it viz Circle,
Rectangle, Triangle etc.
class Shape
{
int x, y;
public:
virtual void draw() = 0;
};
The base class with pure virtual function can't be instantiated since there is no definition
of the function in the base class. It is necessary for the derived class to override pure
virtual function. This type of class with one or more pure virtual function is called
abstract class which can't be instantiated, it can only be inherited.
class shape
{
public: virtual void draw() = 0;
};
C++ solves this issue by introducing a virtual base class. When a class is made virtual,
necessary care is taken so that the duplication is avoided regardless of the number of
paths that exist to the child class.