Pure Virtual Functions Abstract Base Classes And Interface C

Pure virtual functions

Functions that have no body at all. When we add a pure virtual function to our class, we are effectively saying, “it is up to the derived classes to implement this function”.

Example

class Base
{
public:
    const char* SayHi() { return "Hi"; } // a normal non-virtual function    
 
    virtual const char* GetName() { return "Base"; } // a normal virtual function
 
    virtual int GetValue() = 0; // a pure virtual function
};

Consequences
1. Any class with one or more pure virtual functions becomes an abstract base class, which means that it can not be instantiated!
2. Any derived class must define a body for this function, or that derived class will be considered an abstract base class as well.

Interface classes

An interface class is a class that has no members variables, and where all of the functions are pure virtual!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.