┌─────────────────────────────────┐
│ IStorage │
├─────────────────────────────────┤
│ + saveRecord(data): bool = 0 │ ◄── Pure Virtual Contract
│ + readRecord(id): string = 0 │
└────────────────┬────────────────┘
│
┌─────────┴─────────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ SqlStorage │ │ RedisStorage │
└──────────────┘ └──────────────┘ Topic 81 of 91 11. Additional C++ Topics Added Interactive Tutorial
Pure Virtual Functions in C++: Creating Abstract Interfaces
On This Page0 sectionsQuick jump to subtopic
Direct Answer & Core Concept
A pure virtual function is a virtual function declared with = 0 (virtual void render() = 0;) that must be overridden by concrete derived classes. A class with at least one pure virtual function is an Abstract Class and cannot be instantiated. In C++, pure virtual functions can optionally have a default implementation body outside the class definition.
Detailed Explanation & Mechanics
1. Abstract Interface Contract:
Architecture Diagram
2. Pure Virtual with Body:
Useful for providing default cleanup logic in a pure virtual base destructor (virtual ~Base() = 0;).
Runnable Code Example: Pure Virtual Functions in C++
C++20 / C++23main.cpp
#include <iostream>
#include <memory>
#include <vector>
// Abstract Base Class / Interface
class IPaymentGateway {
public:
virtual ~IPaymentGateway() = default;
// Pure Virtual Function
virtual bool processPayment(double amount) = 0;
};
class StripeGateway : public IPaymentGateway {
public:
bool processPayment(double amount) override {
std::cout << "[Stripe API] Successfully charged $" << amount << "\n";
return true;
}
};
class PayPalGateway : public IPaymentGateway {
public:
bool processPayment(double amount) override {
std::cout << "[PayPal API] Successfully routed $" << amount << "\n";
return true;
}
};
int main() {
std::vector<std::unique_ptr<IPaymentGateway>> gateways;
gateways.push_back(std::make_unique<StripeGateway>());
gateways.push_back(std::make_unique<PayPalGateway>());
for (const auto& gw : gateways) {
gw->processPayment(49.99);
}
return 0;
}Common C++ Technical Interview Questions & Answers
Interview PrepQ
Q: Can you instantiate a derived class if it fails to override one pure virtual function from its base?
A: No. If a derived class does not override all pure virtual functions, it remains an abstract class itself and cannot be instantiated.
Q
Q: Why would you ever define a body for a pure virtual function in C++?
A: To provide common reusable default behavior that derived classes can explicitly call (e.g. `Base::process()`) or to make a class abstract by declaring its destructor pure virtual (`virtual ~Base() = 0;`).
Key Takeaways, Modern Best Practices & Idioms
C++ Best Practices & Production Rules
• Define pure virtual destructors (
• Keep interfaces focused and granular according to the Interface Segregation Principle (ISP).
• Always mark overrides in derived classes with
virtual ~Interface() = default;) on all interface classes.• Keep interfaces focused and granular according to the Interface Segregation Principle (ISP).
• Always mark overrides in derived classes with
override to ensure exact signature matching.