Module 5 · Classes and Objects — topic 5 of 5
Abstraction
Abstraction shows what an object does and hides how it does it. Learn abstract interfaces with pure virtual functions, header/source separation, and designing to an interface.
Learning objectives
- Design a class interface that hides implementation
- Declare abstract classes with pure virtual functions
- Write code that depends on an interface, not a concrete class
Definition
Abstraction is the practice of exposing only the essential behaviour of a component through a simplified interface while hiding implementation details; in C++ it is expressed with clean class interfaces and abstract base classes containing pure virtual functions.
Introduction
You drive a car through a steering wheel and pedals without knowing how the engine works. Abstraction gives software the same property: callers use storage.save(record) without caring whether it writes to a file, a database or memory.
Why it matters
Abstraction lets you swap implementations — for testing or new requirements — without touching calling code.
Abstract classes are the C++ way to define interfaces, used throughout frameworks and the projects in this course.
Detailed explanation
Encapsulation hides data; abstraction hides complexity. They work together: a good class has a small, meaningful public interface.
A pure virtual function is declared virtual void draw() const = 0;. A class with at least one is abstract and cannot be instantiated.
Derived classes implement the pure virtual functions; code holds a reference or pointer to the abstract base and calls through it (runtime polymorphism, covered in the next module).
An interface class should have a virtual destructor so objects deleted through a base pointer are destroyed correctly.
Syntax
= 0marks a pure virtual function.overrideasks the compiler to check the signature.- You can't create a
Shapedirectly.
Code examples
Using an abstract interface
Hello, Asha Namaste, Asha
Abstract classes cannot be instantiated
More examples
Swapping storage implementations
164 put maths put physics get maths get physics 164
Step-by-step explanation
Storagedeclares what a store can do, not how.MemoryStorageimplements it with a map;LoggingStoragewraps any other storage (the decorator pattern).score_totalworks with both because it only knows the interface.- Adding a file-backed storage later would not change
score_total.
Expected output
Running “Swapping storage implementations” prints exactly:
Practical applications
- Database drivers implement a common interface so applications can switch databases.
- Unit tests replace real services with fake implementations of the same interface.
- GUI frameworks define abstract widgets with draw and handle-event functions.
Best practices
- Program to interfaces, not implementations.
- Keep interfaces small and cohesive.
- Give polymorphic base classes virtual destructors.
Common mistakes
Missing virtual destructor in a base class
Avoid
Prefer
Deleting a derived object through a base pointer without a virtual destructor is undefined behaviour.
Leaking implementation details in the interface
Avoid
Prefer
Exposing the map ties every caller to one implementation.
Important notes
- C++ has no
interfacekeyword; an abstract class with only pure virtual functions plays that role. - C++20 concepts provide compile-time abstraction for templates without virtual functions.
Key takeaways
- Abstraction exposes what, hides how.
- Pure virtual functions make a class abstract.
- Depend on abstract interfaces to swap implementations easily.
Try it yourself
- 1Define an abstract
PaymentMethodwithpay(amount)and implementUpiPaymentandCardPayment. - 2Write a
FileStorageimplementingStoragewith a text file and use it withscore_total.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
3 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat makes a C++ class abstract?
- BeginnerGiven `class Shape { public: virtual double area() const = 0; };`, what happens with `Shape s;`?
- ModerateWhy should code depend on an abstract `Storage&` rather than on `FileStorage&`?