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.

FOCUS:

  • 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.

//Abstract base class 
class AbstractBase         
{
 public:
 virtual void Display() = 0; //Pure Virtual Function declaration
};

//Derived class that will inherit the abstract base class
// and implement pure virtual function.
class Derived:public AbstractBase
{
 public:
 void Display()
 {
	 cout << "pure virtual function implementation"; }
 };
 
//-------------- TEST-Abstract Class ---------------

 int main() {

 AbstractBase *basePointer = new Derived(); basePointer->Display();
 
 // OR
 AbstractBase * bPtr;
 Derived dObj;
 bPtr = &dObj;
 bPtr->Display();

 //Abstract base class object can't be created.
//AbstractBase obj; // Compiler will flash an error.

}

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.

/*-------------------------------------------------------
*  C++ Example - abstract class
* 
*/

#include <iostream>
using namespace std;

class Beverages{

public:

      //This is common behaviour to all derived classes.
      void addHotItems(){

            addHotWater();
            addMilk();
            addSugar();
      }

      //Defer / force derived classes to implement it.

      virtual void addSachets () = 0;//abstract function

private:

      // internal private functions.
      void addHotWater(){          

            cout<<"\tAdd Hot Water "<< endl;

      }

      void addMilk(){

            cout<<"\tAdd Milk "<< endl;        

      }

      void addSugar(){

            cout<<"\tAdd Sugar "<< endl;      

      }

};

//Derived class tea
class Tea:public Beverages
{

public:
      void addSachets ()// implement specific behavior
      {
            cout<<"\tAdd Tea "<< endl;
      }

      Tea(){
            cout<<"Preparing Tea... "<< endl;
      }

};

class Coffee :public Beverages
{

public:
      void addSachets (){
            cout<<"\tAdd Coffee "<< endl;
      }

      Coffee(){
            cout<<"Preparing Coffee... "<< endl;
      }
};


//Test vinding machine

int main(){
      //create pointer of abstract base class
      //and assign an object of derived class

      Beverages*tea = new Tea();

      tea ->addHotItems();
      tea ->addSachets();


      //Prepare Coffee
      Beverages*coffee = new Coffee();

      coffee ->addHotItems();
      coffee ->addSachets();

      return 0;

}

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

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

Related Posts