C++ Interfaces (via Abstract Classes) – Design Contracts for OOP
Introduction – Why Interfaces Matter in C++
In C++, interfaces are implemented using abstract classes that contain only pure virtual functions. While C++ doesn’t have a separate interface keyword like Java or C#, abstract classes serve the same purpose—providing a contract that derived classes must fulfill.
In this guide, you’ll learn:
- What an interface is in C++
- How to implement interfaces using abstract classes
- Benefits and real-world use cases
- Best practices for designing clean, modular APIs
What Is a C++ Interface?
An interface in C++ is defined as an abstract class where:
- All functions are pure virtual (
= 0) - Typically no data members
- No constructor or implementation logic (optional, but best practice)
Purpose: To enforce a structure/behavior that all derived classes must follow.
Code Examples – With Output
Example 1: Basic Interface Implementation
#include <iostream>
using namespace std;
class IPrintable {
public:
virtual void print() = 0; // Pure virtual
virtual ~IPrintable() {} // Virtual destructor
};
class Document : public IPrintable {
public:
void print() override {
cout << "Printing Document..." << endl;
}
};
int main() {
IPrintable* obj = new Document();
obj->print();
delete obj;
return 0;
}
Output:
Printing Document...
Explanation:
IPrintableacts as an interfaceDocumentmust override all methods in the interface- A virtual destructor ensures safe polymorphic cleanup
Interface Design Pattern
Interface Skeleton:
class InterfaceName {
public:
virtual void method1() = 0;
virtual void method2() = 0;
virtual ~InterfaceName() {}
};
No implementation or data members.
Followed by multiple derived classes providing specific behavior.
Interfaces vs Abstract Classes
| Feature | Interface (in C++) | Abstract Class |
|---|---|---|
| Purpose | Behavior contract only | Contract + partial implementation |
| Pure virtual methods | All | At least one |
| Data members allowed | Avoided | Allowed |
| Reuse implementation | No | Yes |
| Instantiable? | No | No |
Best Practices & Tips
Best Practice: Use I prefix (e.g., IShape, ISerializable) to clearly denote interfaces.
Tip: Use virtual destructors in interfaces to ensure proper cleanup when deleting through a base pointer.
Pitfall: Do not add data members or implementation logic to interfaces—this breaks interface separation.
Real-World Use Cases for Interfaces
Document Processing: IPrintable, ISaveable, ISerializable
Game Engines: IEntity, IController, IInteractable
Plugins/Frameworks: IPlugin, IModule, IService
Network Stacks: ISocket, IProtocolHandler for dynamic swapping of implementations
Summary – Recap & Next Steps
Key Takeaways:
- Interfaces in C++ are built using abstract classes with pure virtual functions
- Provide a clean, modular way to define behavior contracts
- Enable powerful polymorphism and decoupled architecture
Real-World Relevance:
Used in frameworks, APIs, engine architectures, and large-scale modular systems where components must follow strict behavioral contracts.
Next Steps:
- Learn about Virtual Destructors and VTable Internals
- Explore Interface Inheritance and Multiple Interface Implementation
FAQ – C++ Interfaces via Abstract Classes
Does C++ have a keyword for interface like Java?
No. Interfaces are created using abstract classes with only pure virtual functions.
Can a class implement multiple interfaces in C++?
Yes. C++ supports multiple inheritance from abstract classes (interfaces).
Can interfaces contain data members?
Not recommended. Interfaces should only declare behavior, not state.
Why use virtual destructors in interfaces?
To ensure that derived class destructors are called properly when deleting via interface pointers.
How is an interface different from a base class?
An interface has no implementation, while a base class can provide some logic.
Share Now :
