What is abstract class in C++

(Last Updated On: February 25, 2019)

Abstract class in C++  programming is a class that contains at least one pure virtual function and act as a base class. An abstract class may also contain non-virtual functions and member variables.

Pure virtual function in C++ is a function that has no implementation. All derived class who inherit the abstract base class must implement all pure virtual functions.

Main purpose of abstract class is to have all common functions that can be in derived classes also in the base class itself and have some pure virtual function  to defer implementation of it to derived classes.

Important point of an abstract class is that we cannot create an object of it, but we create a pointer. And, in this pointer, we assign the object of derived classes.


  • Abstract class have at least on pure virtual function and act as a base class.
  • An object of this class cannot be created but a pointer can be.
  • It forces all derived classes to implement all pure virtual functions.


Example of abstract class in C++

Below is an abstract class that contains a pure virtual function. Derived classes will override the method and implement it. We will create a pointer of abstract base class and will assign an object of derived class and invoke the function of derived class.


Use of an Abstract class in C++ with simple example

We know that we use an abstract class in cpp to have some common behaviors (functions) and defer some functions to derived classes to implement it.

Below is a pseudo real time of implementation of an abstract class for a Vending Machine that will deliver Tea and Coffee. Common methods milk, hot water and sugar will be defined and provided by abstract class and an abstract method addSachets () will be  introduced in the abstract class itself that is unimplemented and will be implemented by derived classes e.g. Tea and coffee etc. So, they can add tea or coffee depends upon their choice.


Preparing Tea…
Add Hot Water
Add Milk
Add Sugar
Add Tea

Preparing Coffee…
Add Hot Water
Add Milk
Add Sugar
Add Coffee