Abstract Class

A class might declare a virtual function without implementing it. In this case the virtual function is said to be pure and the class is said to be abstract. No object can belong to an abstract class. Abstract classes can only be used as base classes. The syntax used to declare a pure virtual functions is for example:
virtual void virtualFunction() = 0;
In the example below, an abstract class is defined:
class BaseClass
{
public:
     virtual void virtualFunction() = 0;
};
virtualFunction() can be overridden (i.e. redefined) within derived classes but cannot be called from a BaseClass object, as it is not implemented in such a class. Hence, the statement:
BaseClass baseObject;
would cause a compilation error. Abstract classes can be used in all cases in which the base class does not correspond to any entity that actually needs to be created, but represents more of a common denominator for several types of entities. For example, a class called Professional might be created, whose activity() virtual function is pure. Then each and every type of specific professional figure might be derived from Professional and might implement the activity() performed according to the professional's actual competence.

Example


#include <iostream>
#include <string>
using namespace std;

class BaseClass
{
public:
     virtual void virtualFunction() = 0;
};

class DerivedClass1: public BaseClass
{
public:
     void virtualFunction()
     {
           cout << "DerivedClass1 virtualFunction()\n";
     }
};

class DerivedClass2: public DerivedClass1 
{
public:
     void virtualFunction()
     {
           cout << "DerivedClass2 virtualFunction()\n";
     }
};

int main()
{
     // BaseClass baseObject; // if included, this
     //statement prevents compilation
     DerivedClass1 object1;
     DerivedClass2 object2;
     BaseClass *basePointer;
     basePointer = &object1;
     basePointer->virtualFunction();
     basePointer = &object2;
     basePointer->virtualFunction();
}

Output

DerivedClass1 virtualFunction()
DerivedClass2 virtualFunction()