Topic 46 of 91 8. Object-Oriented Programming in C++ Interactive Tutorial

Abstraction in C++: Hiding Implementation Details Behind Interfaces

9 mins read Last updated: 26 Sep 2026 20,802 views

Direct Answer & Core Concept

Abstraction is achieved in C++ by creating abstract classes that contain at least one pure virtual function (virtual void func() = 0;). Abstract classes define pure interfaces that derived classes must implement, decoupling client code from concrete implementation details.

Detailed Explanation & Mechanics

1. Abstract Class Structure:

An abstract class cannot be directly instantiated. It acts as a contractual interface:

Architecture Diagram
┌───────────────────────────────┐
                      │   IDataStorage   │
                      │  + save(data): void = 0       │
                      │  + load(): string = 0         │
                      └──────────────┬────────────────┘
                                     │
             ┌───────────────────────┴───────────────────────┐
             │                                               │
┌────────────────────────────┐                  ┌────────────────────────────┐
│      CloudDataStorage      │                  │      LocalDiskStorage      │
│ + save(data): (AWS S3)     │                  │ + save(data): (Local SSD)  │
│ + load(): string           │                  │ + load(): string           │
└────────────────────────────┘                  └────────────────────────────┘

2. Pure Virtual Syntax:

main.cpp
Run Online
virtual void execute() = 0; // Pure virtual: must be overridden in concrete child

Runnable Code Example: Abstraction in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <string>
#include <memory>

// Pure Interface / Abstract Class
class INotificationService {
public:
    virtual ~INotificationService() = default;
    virtual void sendNotification(const std::string& recipient, const std::string& message) = 0;
};

// Concrete Implementation 1: Email
class EmailNotification : public INotificationService {
public:
    void sendNotification(const std::string& recipient, const std::string& message) override {
        std::cout << "[EMAIL] Sending to " << recipient << ": " << message << "\n";
    }
};

// Concrete Implementation 2: SMS
class SmsNotification : public INotificationService {
public:
    void sendNotification(const std::string& recipient, const std::string& message) override {
        std::cout << "[SMS] Sending to " << recipient << ": " << message << "\n";
    }
};

// Consumer depends only on abstraction
void alertUser(INotificationService& service, const std::string& user, const std::string& msg) {
    service.sendNotification(user, msg);
}

int main() {
    EmailNotification emailSender;
    SmsNotification smsSender;

    alertUser(emailSender, "admin@operate.tools", "System backup finished.");
    alertUser(smsSender, "+1-555-0199", "Security Alert: New login detected.");
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What makes a class an abstract class in C++?

A: A class is abstract if it contains at least one pure virtual function (declared with `= 0`). It cannot be instantiated directly.
Q

Q: Can an abstract class have a constructor and normal member variables in C++?

A: Yes. Abstract classes can have data members, non-virtual methods, and constructors called by derived class constructors to initialize base state.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Always define a virtual destructor (virtual ~Interface() = default;) in interface classes.
• Depend on abstractions (interfaces) rather than concrete implementations (Dependency Inversion Principle).
• Prefix interface class names with I (e.g. ILogger, IRepository) for clear architectural communication.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.