Abstract Base Class in C++

What is Abstract Base Class ? (ABC) ?

Abstract Class is a conceptual class, which contains at least one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

Syntax: Abstract Base Class

class Base //Abstract base class
{
public:
     virtual void show() = 0; //Pure Virtual Function
};

What is Pure Virtual Function in c++ ?

Pure virtual Functions are virtual functions with no definition or no body part. They start with virtual keyword and ends with = 0.

Syntax: Pure Virtual Function

void function_nor()   //normal function
{ }
virtual void function_vir() = 0;  //pure virtual function

Characteristics of Abstract Class

  1. Abstract class cannot be instantiated, but pointers and references of Abstract class type can be created.
  2. Abstract class can have normal functions and variables along with a pure virtual function.
  3. Abstract classes are mainly used for Up-casting, so that its derived classes can use its interface.
  4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.

Why can’t we create Object of Abstract Class ?

When we create a pure virtual function in Abstract class, we reserve a slot for a function in the VTABLE, but doesn’t put any address in that slot. Hence the VTABLE will be incomplete.
As the VTABLE for Abstract class is incomplete, hence the compiler will not let the creation of object for such class and will display an error message whenever you try to do so.

Deriving Abstract Base Class

When a class is derived from an abstract class, it inherits all the methods the base class contains, particularly the pure virtual methods. If all of these pure virtual methods are implemented in the derived class, you can then create an object of the derived class type.

This means you need to implement all the pure virtual methods of base class into derived class to complete the inheritance process. If you do not do so, derived class becomes abstract too. In this case derived class is called concrete class.

Example:

class Base    //Abstract base class
{
public:
     virtual void show() = 0; //Pure Virtual Function
};
class Derived:public Base
{
public:
void show()
{ 
   cout << "Implement of Virtual Function in Derived class"; 
}
};
int main()
{
   Base obj;    //Compile Time Error
   Base *b;
   Derived d;
   b = &d;
   b->show();
}

In the above example Base class is abstract, with pure virtual show() function, hence we cannot create object of base class.

Here, Class Derived is deriving Base class, which is abstract base class because it contains a pure virtual function virtual void show()=0 in it.

Derived class has to complete the definition of virtual function in its scope. If you will not complete the definition in Derived class, it will become abstract too. You can’t create an object of Derived class also.

Example:

class Base    //Abstract base class
{
public:
     virtual void show() = 0; //Pure Virtual Function
};
class Derived:public Base
{
public:
void display()
{ 
   cout << "Implement of Virtual Function in Derived class"; 
}
};
int main()
{
   Base obj;  //Compile Time Error
   Base *b;
   Derived d;
  //Error
   b = &d;
   b->show();
}

Output:

Error: You can not create an object of abstract class.

Related posts