# Abstract Classes and Pure Virtual Functions

> An abstract class is a class that cannot be instantiated directly and exists to define a shared interface that concrete subclasses must fill in. In C++, a class becomes abstract when it has at least one pure virtual function, a virtual function declared with = 0 and no implementation, which every concrete subclass must implement.

Canonical: https://devsunite.com/resources/core-subjects/oop/abstract-classes-and-pure-virtual-functions
Last updated: 2026-11-22

Subject: OOP · Category: Polymorphism · 3 min read

An abstract class is a class that can never be instantiated directly, it exists purely to define a shared structure and interface that concrete subclasses must fill in. In C++, a class becomes abstract the moment it has at least one pure virtual function, a virtual function with no implementation, declared with `= 0`.

## How it works

A pure virtual function declares that every concrete (non-abstract) subclass must provide its own implementation, the base class deliberately leaves it undefined because there's no sensible generic default, or because the whole point is to force each subclass to define its own specific behavior. Any class with even one pure virtual function it hasn't overridden is itself abstract and cannot be instantiated, this includes intermediate classes in a hierarchy that don't implement every pure virtual function they inherit.

## A worked example

```cpp
#include <iostream>
using namespace std;

class Shape {
public:
    virtual double area() = 0;       // pure virtual, no implementation here
    virtual void draw() = 0;         // pure virtual too

    void printArea() {               // a regular, non-pure method CAN have a real implementation
        cout << "Area: " << area() << endl;
    }
};

class Circle : public Shape {
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() override { return 3.14159 * radius * radius; }
    void draw() override { cout << "Drawing a circle" << endl; }
};

int main() {
    // Shape s;              // compile error: cannot instantiate an abstract class
    Circle c(5);
    c.draw();
    c.printArea();           // inherited, non-pure method works fine

    Shape* shapePtr = &c;    // allowed: a pointer/reference to the abstract type is fine
    shapePtr->draw();        // calls Circle's implementation via virtual dispatch
    return 0;
}
```

`Shape` can never be instantiated directly, attempting `Shape s;` is a compile error, but `Shape*` pointers and references are completely valid and, in fact, exactly how polymorphic code is meant to work with it: the abstract class defines the shared contract, and concrete subclasses like `Circle` fill it in.

## Where this matters in real systems

Abstract base classes are the standard way to define an interface-style contract in C++, since C++ has no separate `interface` keyword the way Java does (Java's `interface` is conceptually very close to a pure abstract class with only pure virtual-equivalent methods). Any plugin or driver architecture, an audio codec system, a database driver abstraction, a rendering backend, typically defines an abstract base class specifying exactly what every implementation must provide (`connect()`, `query()`, `close()` for a database driver, for instance), and the core application only ever talks to that abstract interface, never to a specific concrete driver directly.

## What interviewers actually ask about abstract classes and pure virtual functions

- **"What makes a class abstract in C++?"** Having at least one pure virtual function, a virtual function declared with `= 0` and no implementation.
- **"Can an abstract class have a constructor?"** Yes, and it's common, the constructor still runs when a concrete subclass is instantiated, even though the abstract class itself can never be instantiated directly.
- **"Can a pure virtual function have an implementation?"** Yes, somewhat surprisingly, C++ actually allows a pure virtual function to have a body that a subclass can explicitly call via the base class name, but the `= 0` still forces every subclass to provide its own override regardless.
- **"What's the difference between an abstract class and a regular class with all virtual methods overridden by default?"** A regular (concrete) class can be instantiated directly even if all its methods happen to be overridable, an abstract class is specifically prevented from ever being instantiated because at least one required method has no default implementation at all.

## Key takeaways

- An abstract class cannot be instantiated directly and exists to define a shared interface.
- A pure virtual function (`= 0`) has no implementation and forces every concrete subclass to define its own.
- Pointers and references to an abstract type are completely valid, that's exactly how polymorphic code uses them.
- Abstract base classes are C++'s standard way of expressing an interface-style contract, since C++ has no dedicated `interface` keyword.

## Related

- [Previous: Virtual Functions and Inline Virtual Functions](https://devsunite.com/resources/core-subjects/oop/virtual-functions-and-inline-virtual-functions)
- [More in Polymorphism: Overview of Polymorphism](https://devsunite.com/resources/core-subjects/oop/overview-of-polymorphism)
- [More in Polymorphism: Function Overloading](https://devsunite.com/resources/core-subjects/oop/function-overloading)
- [More in Polymorphism: Method Overriding](https://devsunite.com/resources/core-subjects/oop/method-overriding)
- [All 72 OOP interview questions](https://devsunite.com/resources/core-subjects/oop)
