Abstract Classes and Pure Virtual Methods
Define interfaces with pure virtual methods and enforce derived-class contracts.
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 instantiatePure Virtual Functions
The = 0 marks a function as having no default implementation. Derived classes must override it before the class can be instantiated.
All lessons in this course
- Single and Multiple Inheritance
- Virtual Functions and the vtable
- Abstract Classes and Pure Virtual Methods
- Inheritance vs Composition Trade-offs