Pure Virtual Function and Abstract Class
Definition
Abstract class
- In a base class, we don't provide the implementation of certain functions. Such a class is called abstract class.
- An abstract class cannot have an instance of itself created.
Pure Virtual Function
- a virtual function for which we don’t have implementation, we only declare it.
declared by assigning 0 in declaration.
// An abstract class class Test { // Data members of class public: // Pure Virtual Function virtual void show() = 0; /* Other members */ };
When
- create a pure virtual function when it doesn’t make sense to provide a definition for a virtual function in the base class itself, within the context of inheritance
- For example: for a base class "Figure", it does not make sense to provide the implementation draw() function in the abstract class.