⚡C++IntermediateLesson 29 of 50CamboFreelanceJune 19, 2026
C++ Abstraction
Learn C++ abstraction with abstract classes and pure virtual functions. Build flexible interfaces that separate what from how.
Intermediate7 min readLesson 29 of 50
What is Abstraction?
Abstraction hides complex implementation details and exposes only what is necessary. In C++, abstract classes with pure virtual functions define an interface.
MySQL: Connected
MySQL executing: SELECT * FROM users
MySQL: Disconnected
Key Rules: A class with at least one pure virtual function (= 0) is abstract. You cannot instantiate an abstract class directly — only its concrete subclasses.
Exercise
Create an abstract class Notification with a pure virtual method send(string message). Implement EmailNotification and SMSNotification subclasses.
Show Solution
#include <iostream>
using namespace std;
class Notification {
public:
virtual void send(string msg) = 0;
virtual ~Notification() { }
};
class EmailNotification : public Notification {
public:
void send(string msg) override {
cout << "Email: " << msg << endl;
}
};
class SMSNotification : public Notification {
public:
void send(string msg) override {
cout << "SMS: " << msg << endl;
}
};
int main() {
Notification* n1 = new EmailNotification();
Notification* n2 = new SMSNotification();
n1->send("Welcome!");
n2->send("Your code is 1234");
delete n1; delete n2;
return 0;
}
Quiz
How is a pure virtual function declared?
A) virtual void f() {}
B) abstract void f();
C) virtual void f() = 0;
D) void f() = pure;
Answer
C) virtual void f() = 0;
Summary
Abstract classes define an interface with pure virtual functions (= 0).
Cannot instantiate abstract classes — only concrete subclasses.
Abstraction separates "what" from "how" — a key OOP principle.