Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Abstract classes and pure virtual functions

Reference for C++ abstract classes: pure virtual functions, interface classes, pure virtual destructors, definitions for pure virtuals and why you cannot instantiate them.

An abstract class is a class that has at least one pure virtual function, declared with = 0. You cannot create objects of an abstract class, only use it through pointers and references to derived classes. It defines an interface: a set of operations every concrete derived class must provide.

A class with only pure virtual functions (and no data) is the C++ equivalent of an interface in Java or C#. A derived class stays abstract until it overrides every pure virtual function it inherited.

C++
struct Logger {
    virtual void write(const std::string& line) = 0;
    virtual ~Logger() = default;
};

Try it

C++ An interface and two implementations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Template method, defined pure virtuals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
virtual ReturnType name(params) = 0;          // pure virtual function
virtual ~Base() = 0;                          // pure virtual destructor (must be defined)
ReturnType Base::name(params) { ... }         // optional out-of-class definition
Part Description
= 0 Marks the function pure. Only valid on virtual functions, written in the declaration.
Abstract class Any class with at least one pure virtual function, declared or inherited and not overridden.
Concrete class A class that overrides all pure virtuals; objects of it can be created.

Returns: n/a. Calling a pure virtual through the vtable of a complete derived object runs the override; an abstract class itself has no complete objects.

Errors / undefined behavior: instantiating an abstract class is a compile error. Calling a pure virtual function directly (not via qualified call) from a constructor or destructor of the abstract class is undefined behavior and typically aborts with "pure virtual method called".

Usage notes

  • Pure does not mean "no body". A pure virtual function may be defined; derived overrides can call it with Base::name(). This provides default behavior that must still be opted into.
  • Pure virtual destructor. To make a class abstract when it has no other virtual function, declare virtual ~Base() = 0; and always define it, because destructors of base classes are called by every derived destructor.
  • Interfaces should be small, have a virtual destructor, no data members and usually no implementation. Use override in implementations so signature mistakes are caught.
  • Pointers and references are allowed. You can write Logger&, Logger*, std::unique_ptr<Logger>, but not Logger by value or std::vector<Logger>.
  • Still abstract? If a derived class forgets one override, the compiler tells you at the point you try to create it, not at the class definition.
  • Template Method pattern. A non-virtual public function calls protected virtual steps, as in the second example, so the algorithm stays fixed while steps vary.
  • Cost. Each object of a polymorphic class carries a hidden vtable pointer, and virtual calls are indirect. For performance-critical, closed hierarchies consider std::variant or templates.
  • Non-virtual interface (NVI). Make virtuals private or protected and expose public non-virtual wrappers; this lets the base add checks or logging in one place.
  • Constructors of abstract classes still run, as part of constructing the derived object, and can be protected.