DEV Community

Cover image for # πŸš€ C++ Abstraction Cheat Sheet: 10-Minute Interview Revision Guide
Muhammad Hassan Obaid
Muhammad Hassan Obaid

Posted on

# πŸš€ C++ Abstraction Cheat Sheet: 10-Minute Interview Revision Guide

If you have an interview in the next few hours and need to quickly revise Abstraction in C++, this guide is for you.

No long theory.

No unnecessary examples.

Only the concepts interviewers expect you to know.


πŸ“Œ What is Abstraction?

Definition

Abstraction is the process of exposing only the essential behavior of an object while hiding unnecessary implementation details.

Remember

WHAT

↓

Hide HOW
Enter fullscreen mode Exit fullscreen mode

The user knows what an object can do, but not how it performs the work.


❓ Why Do We Need Abstraction?

Without abstraction:

  • Every developer needs to understand internal implementation.
  • Client code becomes tightly coupled.
  • Maintenance becomes difficult.

With abstraction:

  • Developers interact with a simple interface.
  • Internal implementation can change without affecting users.
  • Systems become easier to extend and maintain.

Benefits

  • βœ… Reduces complexity
  • βœ… Promotes loose coupling
  • βœ… Improves maintainability
  • βœ… Supports extensibility
  • βœ… Enables cleaner architecture

βš™οΈ How Does C++ Achieve Abstraction?

C++ primarily achieves abstraction using:

Abstract Class

+

Pure Virtual Functions

+

Runtime Polymorphism
Enter fullscreen mode Exit fullscreen mode

πŸ—οΈ What is an Abstract Class?

An abstract class is a class that contains at least one pure virtual function.

It represents a:

  • βœ… Contract
  • βœ… Blueprint
  • βœ… Common capability

Because it is incomplete, it cannot be instantiated.


🎯 What is a Pure Virtual Function?

Syntax

virtual ReturnType functionName() = 0;
Enter fullscreen mode Exit fullscreen mode

Meaning

It tells the compiler:

Every concrete derived class must implement this function.

= 0 does NOT mean "return zero."

It simply marks the function as pure virtual.


🧠 Mental Model

Think of it like this:

Job Description

↓

Employee
Enter fullscreen mode Exit fullscreen mode

The job description defines responsibilities.

Each employee fulfills those responsibilities differently.

Or:

Blueprint

↓

House
Enter fullscreen mode Exit fullscreen mode

You don't live inside a blueprint.

You build a house from it.

Similarly, you don't create objects of an abstract classβ€”you create objects of concrete derived classes.


🏭 Practical Software Example

Imagine an e-commerce application supporting multiple payment providers.

Every payment provider should support:

  • Process Payment
  • Refund Payment

The checkout service shouldn't care whether the payment is processed by Stripe, PayPal, or JazzCash.

class PaymentGateway {
public:
    virtual void processPayment() = 0;
    virtual void refund() = 0;

    virtual ~PaymentGateway() = default;
};
Enter fullscreen mode Exit fullscreen mode

Concrete implementations:

  • Stripe
  • PayPal
  • JazzCash

Each provides its own implementation.

The checkout service simply uses:

paymentGateway->processPayment();
Enter fullscreen mode Exit fullscreen mode

It depends on the contract, not the implementation.

This is abstraction in production software.


πŸ“¦ What Can an Abstract Class Have?

An abstract class can contain:

  • βœ… Constructors
  • βœ… Data members
  • βœ… Concrete (implemented) methods
  • βœ… Static members
  • βœ… Virtual destructor

Only one pure virtual function is enough to make the class abstract.


🚫 Why Can't an Abstract Class Be Instantiated?

Because it is an incomplete type.

Some required behavior is still missing.

Abstract Class

↓

Incomplete Type

↓

Missing Implementation

↓

Compiler Prevents Object Creation
Enter fullscreen mode Exit fullscreen mode

A concrete derived class becomes instantiable only after implementing all inherited pure virtual functions.


πŸ”„ Abstract Class vs Pure Abstract Class

Abstract Class Pure Abstract Class (Interface Style)
Can contain shared implementation Contains only pure virtual functions (typically plus a virtual destructor)
Can contain variables Usually contains no state
Can contain concrete methods Only declares behavior
Can provide common functionality Primarily defines a contract

Note: C++ has no interface keyword. Developers usually refer to a class containing only pure virtual functions as an interface-style or pure abstract class.


πŸ†š Abstraction vs Encapsulation

Abstraction Encapsulation
Hides complexity Protects data
Focuses on behavior Focuses on object state
Defines WHAT an object can do Controls HOW data is accessed

Easy Memory Trick

Abstraction

↓

WHAT

Encapsulation

↓

Protect DATA
Enter fullscreen mode Exit fullscreen mode

πŸ” Connection with Runtime Polymorphism

Abstraction

↓

Defines Contract

↓

Runtime Polymorphism

↓

Chooses Correct Implementation
Enter fullscreen mode Exit fullscreen mode

Remember:

Abstraction

↓

WHAT

Runtime Polymorphism

↓

WHO
Enter fullscreen mode Exit fullscreen mode

🧠 Memory Model

When using runtime polymorphism, think of the flow like this:

Stack

↓

Base Pointer

↓

Heap

↓

Derived Object

↓

vptr

↓

vtable

↓

Correct Function Executes
Enter fullscreen mode Exit fullscreen mode

This is how C++ dynamically dispatches virtual function calls.


🏭 Real-World Uses

Abstraction is commonly used in:

  • βœ… Payment Gateways
  • βœ… Inventory Providers
  • βœ… Notification Services
  • βœ… Shipping Providers
  • βœ… Database Drivers
  • βœ… Cloud Storage SDKs

πŸ’‘ Production Best Practices

  • βœ… Program to abstractions, not concrete implementations.
  • βœ… Always use override when overriding virtual functions.
  • βœ… Give polymorphic base classes a virtual destructor.
  • βœ… Keep abstractions focused on one business capability.
  • βœ… Introduce abstraction when multiple implementations or future extensibility justify it.
  • ❌ Don't over-abstract simple systems.

🚨 Common Interview Traps

❌ "= 0 means return zero."

β†’ No. It marks a function as pure virtual.


❌ "Abstract classes cannot have constructors."

β†’ They absolutely can.


❌ "Abstract classes cannot have variables."

β†’ They can.


❌ "Abstract classes cannot have implemented methods."

β†’ They can.


❌ "Interface and abstract class are identical."

β†’ In C++, an interface is generally a design style (a pure abstract class), not a separate language feature.


❌ "Virtual destructor isn't necessary."

β†’ If the class is intended to be used polymorphically, a virtual destructor is the recommended practice.


πŸ‘¨β€πŸ’Ό Think Like a Staff Engineer

Don't design abstractions around technologies.

❌ Bad

MySQLWarehouse
Enter fullscreen mode Exit fullscreen mode

βœ… Better

InventoryProvider
Enter fullscreen mode Exit fullscreen mode

Model business capabilities, not implementation details.

Business concepts remain stable.

Technologies change.


🎯 One-Line Summary

Abstraction

↓

Expose WHAT

↓

Hide HOW

↓

Abstract Class

↓

Defines Contract

↓

Pure Virtual Function

↓

Mandatory Behavior

↓

Derived Class

↓

Provides Implementation

↓

Runtime Polymorphism

↓

Chooses Correct Implementation

↓

Loose Coupling

↓

Extensible Software
Enter fullscreen mode Exit fullscreen mode

🧠 30-Second Memory Map

Problem

↓

Complex Software

↓

Need Simplicity

↓

Abstraction

↓

Abstract Class

↓

Pure Virtual Function

↓

Cannot Instantiate

↓

Derived Class Implements

↓

Runtime Polymorphism

↓

Correct Function Executes

↓

Flexible Architecture
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Final Takeaway

Don't memorize this:

virtual void function() = 0;
Enter fullscreen mode Exit fullscreen mode

Instead, remember this:

Abstraction is about designing software around stable business capabilities while hiding implementation details.

The syntax is just C++'s way of expressing that idea.


If this cheat sheet helped you, consider bookmarking it for your next interview and share it with someone preparing for C++ or Object-Oriented Programming interviews. πŸš€

Top comments (0)