Abstraction in C++: Hiding Implementation Details Behind Interfaces
What is Abstraction in C++: Hiding Implementation Details Behind Interfaces? Abstraction is achieved in C++ by creating abstract classes that contain at least one pure virtual function (virtual void func() = 0;). Abstract classes define...
How is abstraction achieved in C++ using abstract classes, pure virtual functions, and interfaces?
Explanation & In-Depth Details
An abstract class cannot be directly instantiated. It acts as a contractual interface:
```text
┌───────────────────────────────┐
│ <<Interface>> 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:
```cpp
virtual void execute() = 0; // Pure virtual: must be overridden in concrete child
```
Code Example
#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;
}Interview Question & Tips
A: A class is abstract if it contains at least one pure virtual function (declared with `= 0`). It cannot be instantiated directly.
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.
Best Practices
• 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.
This study reference for Abstraction in C++: Hiding Implementation Details Behind Interfaces is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: