Systems CourseC/C++ · Linux · Memory
C++ CoreLesson 15 of 441 min read7 questions

Interfaces / Abstract Classes

Pure virtuals make a class abstract; all-pure = an interface.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. A class with ≥1 pure virtual (virtual void f() = 0;) is abstract → cannot be instantiated. All functions pure + no data = an interface (convention).
  2. You CAN use base pointers/refs to an abstract class — that's the point: Base* p = new Derived(); works if Derived overrides every pure virtual.
  3. An abstract base can still provide implemented (normal virtual) methods → shared behavior; pure virtuals force each derived to supply its own.
  4. An interface needs a virtual destructor.
Watch out

Easily confused

abstract class vs interface
Abstract = has ≥1 pure virtual (may still have data/implemented methods). Interface = ALL pure virtual, no data (a pure contract) — convention, not a keyword.
pure virtual vs virtual
= 0 (pure) forces derived classes to implement it and makes the class abstract. A plain virtual has a body and can be overridden optionally.

Test yourself

7 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.