What Is an Abstract Base Class in C++
In C++, an abstract base class serves as a blueprint for other classes by defining an interface that derived classes must implement. This article explains the exact criteria that turn a standard class into an abstract base class, how to declare pure virtual functions, and the practical rules governing instantiation and inheritance in object-oriented C++ programming.
The Core Requirement: Pure Virtual Functions
A class in C++ becomes an abstract base class (ABC) if and only if it
declares or inherits at least one pure virtual
function. A pure virtual function is a virtual member function
assigned = 0 in its declaration.
class Shape {
public:
// Pure virtual function
virtual void draw() const = 0;
// Regular virtual destructor
virtual ~Shape() = default;
};The = 0 syntax specifies that the function has no
default implementation within that class (though C++ allows you to
provide an out-of-line body if needed) and signals to the compiler that
this class is incomplete on its own.
Key Characteristics of an Abstract Base Class
1. Inability to Instantiate
You cannot create an instance (object) of an abstract base class directly. Attempting to allocate an object of an abstract type results in a compile-time error:
Shape s; // Compiler error: cannot declare variable 's' to be of abstract type 'Shape'However, you can declare pointers and references to an abstract base class. This allows dynamic polymorphism at runtime:
Shape* shapePtr = nullptr; // Valid2. Mandatory Implementation in Derived Classes
Any class derived from an abstract base class must provide a concrete implementation for every pure virtual function it inherits. If a derived class fails to override even one pure virtual function, that derived class remains abstract and cannot be instantiated either.
class Circle : public Shape {
public:
void draw() const override {
// Concrete implementation
}
};
Circle c; // Valid: all pure virtual functions are implemented3. Ability to Contain Data and Normal Functions
An abstract base class is not restricted to pure virtual declarations. It can contain:
- Member variables
- Non-virtual functions
- Normal virtual functions with default implementations
- Constructors and destructors
Pure Virtual Destructors
A class can also be made abstract by declaring its destructor as pure virtual. This is useful when you have a base class with no other meaningful methods to designate as pure virtual, but you still want to prevent instantiation.
class Base {
public:
virtual ~Base() = 0;
};
// A definition must still be provided for a pure virtual destructor
Base::~Base() {}Unlike ordinary pure virtual functions, a pure virtual destructor must have a function body defined outside the class declaration because derived class destructors always invoke the base class destructor during cleanup.
Summary
A class in C++ is made an abstract base class solely through the
presence of at least one pure virtual function
(virtual ReturnType funcName() = 0;). This mechanism
enforces an explicit contract for all derived classes, preventing
incomplete base objects from being instantiated while enabling
polymorphic design.