0Pricing
C++ Academy · Lesson

Abstract Classes and Pure Virtual Methods

Define interfaces with pure virtual methods and enforce derived-class contracts.

Abstract Classes and Pure Virtual Methods is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is an Abstract Class?

An abstract class has at least one pure virtual function — declared with = 0. You cannot instantiate it directly.

class Animal {
public:
    virtual void speak() const = 0;   // pure virtual
    virtual ~Animal() = default;
};

// Animal a;     // ERROR: cannot instantiate

Pure Virtual Functions

The = 0 marks a function as having no default implementation. Derived classes must override it before the class can be instantiated.

Implementing the Interface

Derived classes provide concrete bodies for every pure virtual.

class Dog : public Animal {
public:
    void speak() const override {
        std::cout << "Woof!\n";
    }
};

Dog d;
d.speak();      // Woof!

Abstract Classes as Interfaces

An abstract class with only pure virtual methods (and no data members) acts like an interface in other languages.

class Drawable {
public:
    virtual void draw() const = 0;
    virtual ~Drawable() = default;
};

Combining Implementation and Interface

Abstract classes can have both pure virtual methods and concrete methods. Pure virtuals define the contract; concrete methods give shared behavior.

class Logger {
public:
    void logInfo(const std::string& msg) { log("INFO", msg); }
    void logError(const std::string& msg) { log("ERROR", msg); }
protected:
    virtual void log(const std::string& level, const std::string& msg) = 0;
};

Pure Virtual with a Default Body

A pure virtual can have a body — useful as a default that derived classes can call.

class Base {
public:
    virtual void f() = 0;
};
void Base::f() {
    std::cout << "default behavior\n";
}

class Derived : public Base {
public:
    void f() override {
        Base::f();          // call the default
        std::cout << "extra\n";
    }
};

Use Through Pointers and References

Polymorphic code holds abstract objects through pointers or references to the base type.

void perform(const Animal& a) {
    a.speak();
}

Storing in Containers

Vectors cannot hold abstract types by value. Use pointers — typically smart pointers.

std::vector<std::unique_ptr<Animal>> zoo;
zoo.push_back(std::make_unique<Dog>());
zoo.push_back(std::make_unique<Cat>());
for (auto& a : zoo) a->speak();

When to Use Abstract Classes

Use them when:

  • You want to define a common interface for multiple unrelated implementations
  • You need a base type for polymorphic containers
  • You want to inject behavior via subclasses (Template Method pattern)

Alternatives: Concepts and Templates

For compile-time polymorphism, C++20 concepts and templates often replace abstract classes — zero runtime cost. Use virtual when polymorphism is needed at runtime.

Pitfall: Slicing

Pass abstract classes by reference or pointer. Pass-by-value is impossible anyway (cannot instantiate abstract type) — but derived classes pass-by-value would slice.

Quick Check

What makes a class abstract in C++?

Recap

Abstract classes — those with at least one pure virtual method — define interfaces for polymorphic code. Use them with pointers or references (often smart pointers) to dispatch behavior at runtime.

Frequently asked questions

Is the “Abstract Classes and Pure Virtual Methods” lesson free?

Yes — the full text of “Abstract Classes and Pure Virtual Methods” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Abstract Classes and Pure Virtual Methods”?

Define interfaces with pure virtual methods and enforce derived-class contracts. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Abstract Classes and Pure Virtual Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Single and Multiple Inheritance
  2. Virtual Functions and the vtable
  3. Abstract Classes and Pure Virtual Methods
  4. Inheritance vs Composition Trade-offs
← Back to C++ Academy