01 / 21

Abstract Classes &
Pure Virtual Functions

Learn how C++ uses abstract classes and pure virtual functions to define interfaces, enforce contracts, and unlock the power of polymorphism.

C++

What Is a Virtual Function?

A virtual function is a member function in a base class that you can override in a derived class. When called through a base-class pointer or reference, C++ determines at runtime which version to invoke: this is called dynamic dispatch.

class Shape { public: virtual void draw() { cout << "Drawing shape"; } };

From Virtual to Pure Virtual

A pure virtual function has no body in the base class. Instead of providing a default implementation, you append = 0 to the declaration. This tells derived classes: "you must implement this."

class Shape { public: virtual void draw() = 0; // pure virtual };

What Is an Abstract Class?

Any class that contains at least one pure virtual function automatically becomes an abstract class. You cannot create objects of an abstract class directly: it exists only to be inherited from.

✕ Cannot Do
Create an instance: Shape s; produces a compiler error.
✓ Can Do
Declare pointers and references: Shape* p; is perfectly valid.

The Inheritance Hierarchy

Shape (abstract) Circle Rectangle Triangle draw() defined ✓ draw() defined ✓ draw() defined ✓ Abstract (cannot instantiate) Concrete (can instantiate)

The Core Rule

1
Declare with = 0: A pure virtual function has no body in the base class and ends with = 0.
2
Class becomes abstract: Any class with at least one pure virtual function cannot be instantiated.
3
Derived must override: A derived class must implement all inherited pure virtual functions, or it too becomes abstract.
4
Polymorphism works: Base-class pointers can call the overridden function, and the correct derived version runs at runtime.

Abstract vs Concrete Classes

Abstract Class Concrete Class
Pure virtual functions At least one None (all implemented)
Can be instantiated? No Yes
Pointers/references? Yes Yes
Purpose Define an interface Provide full behavior
Can have data members? Yes Yes

Why Use Abstract Classes?

Enforce Contracts
Guarantee every derived class implements required behavior.
Enable Polymorphism
Write code that works with any derived type through a single interface.
Reduce Coupling
Client code depends on the abstract interface, not specific implementations.

How to Apply This Pattern

1
Identify shared behavior: Determine which functions all derived classes must have (e.g., draw(), area()).
2
Create the base class: Declare those functions as virtual ... = 0; in the base class.
3
Derive concrete classes: Each subclass provides its own implementation of every pure virtual function.
4
Use base pointers: Pass objects via Base* or Base& to leverage polymorphism.
5
Add a virtual destructor: Always declare virtual ~Base() = default; to prevent resource leaks.

Example 1: Abstract Shape

class Shape { public: virtual double area() const = 0; // pure virtual virtual void draw() const = 0; // pure virtual virtual ~Shape() = default; };

Example 2: Concrete Circle

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

Example 3: Polymorphism in Action

void render(const Shape& s) { s.draw(); // calls the correct derived draw() cout << "Area: " << s.area() << "\n"; } int main() { Circle c(5.0); Rectangle r(3.0, 4.0); render(c); // "Drawing circle" Area: 78.5398 render(r); // "Drawing rectangle" Area: 12 }

What If You Don't Override?

class Blob : public Shape { public: void draw() const override { cout << "Drawing blob\n"; } // area() is NOT overridden! };

A derived class remains abstract until every inherited pure virtual function has been overridden.

Surprise: Pure Virtual with a Body

C++ allows you to provide a default implementation for a pure virtual function. The class is still abstract, but derived classes can optionally call the base version.

class Shape { public: virtual void draw() const = 0; }; void Shape::draw() const { cout << "Default shape draw\n"; }

Common Mistakes to Avoid

✕ Instantiating Abstract
Trying to create Shape s; when Shape has a pure virtual function. Use pointers or references instead.
✕ Forgetting override
Omitting the override keyword. Without it, a typo in the function signature creates a new function instead of overriding.
✕ No Virtual Destructor
Deleting a derived object through a base pointer without a virtual destructor causes undefined behavior and memory leaks.

Summary at a Glance

Concept Syntax / Rule Effect
Virtual function virtual void f(); Can be overridden; dynamic dispatch
Pure virtual virtual void f() = 0; Must be overridden; class becomes abstract
Abstract class Has ≥ 1 pure virtual Cannot be instantiated
Concrete class Overrides all pure virtuals Can be instantiated
override keyword void f() override; Compiler checks correct override

Question 1 of 5

What makes a C++ class abstract?

Question 2 of 5

Given the following code, which statement is true?

class Animal { public: virtual void speak() = 0; }; class Dog : public Animal { public: void speak() override { cout << "Woof"; } };

Question 3 of 5

If class B inherits from abstract class A but does not override all pure virtual functions, what happens?

Question 4 of 5

Which of the following is a valid use of an abstract class?

Question 5 of 5

Why should an abstract base class have a virtual destructor?