๐งฉ C++ Pure Virtual Functions โ Abstract Classes & Interfaces Explained
๐งฒ Introduction โ Why Use Pure Virtual Functions in C++
In C++ object-oriented programming, pure virtual functions define abstract behaviorโforcing derived classes to provide their own implementation. This enables creation of interfaces and enforces consistent method structures in derived classes.
๐ฏ In this guide, you’ll learn:
- What pure virtual functions are
- How to declare abstract classes and interfaces
- Syntax and real-world examples
- Best practices for polymorphic design
๐ What Is a Pure Virtual Function?
A pure virtual function is a virtual function that has no implementation in the base class. It is declared by assigning = 0
in its declaration.
๐ A class with at least one pure virtual function becomes an abstract class, which:
- Cannot be instantiated
- Must be inherited and overridden
๐ป Code Examples โ With Output
โ Example 1: Basic Pure Virtual Function
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};
int main() {
// Shape s; // โ Error: Cannot instantiate abstract class
Circle c;
c.draw(); // โ
Valid
return 0;
}
๐ข Output:
Drawing Circle
๐ Explanation:
Shape
is an abstract class due todraw() = 0
Circle
must overridedraw()
or remain abstract
โ Example 2: Interface-style Class with Multiple Pure Virtual Functions
class IPrinter {
public:
virtual void print() = 0;
virtual void scan() = 0;
};
class LaserPrinter : public IPrinter {
public:
void print() override {
cout << "Laser printing..." << endl;
}
void scan() override {
cout << "Scanning document..." << endl;
}
};
๐ Note: You can use abstract classes to define interfaces (just like in Java or C#).
๐งฑ Abstract Class Structure
class AbstractBase {
public:
virtual void doWork() = 0; // Pure virtual
void utilityMethod() { // Concrete method
cout << "Helper logic" << endl;
}
};
โ Abstract classes can also have:
- Constructors
- Data members
- Non-pure (concrete) methods
๐ Abstract Classes vs Interfaces
Feature | Abstract Class | Interface (in C++ style) |
---|---|---|
Purpose | Partial implementation + abstraction | Full abstraction only |
Members | Can include non-pure functions | All functions are pure virtual |
Variables | Can have member variables | Typically avoids data members |
Instantiation | โ No | โ No |
๐ก Best Practices & Tips
๐ Best Practice: Use abstract classes when some shared logic is needed; use pure interface classes for behavior contracts.
๐ก Tip: Use override
in derived classes to ensure correct method overriding.
โ ๏ธ Pitfall: Forgetting to implement all pure virtual functions makes the derived class abstract and uninstantiable.
๐ ๏ธ Use Cases for Pure Virtual Functions
๐งพ Document Rendering: Document โ PDF, DOCX, PPT
with render()
override
๐ฎ Game Entities: Entity โ Player, Enemy
each override update()
๐ฆ Plugin Frameworks: IPlugin
interface ensures every plugin has init()
and execute()
๐ก Device Drivers: Abstract Device
interface with read()
and write()
functions
๐ Summary โ Recap & Next Steps
๐ Key Takeaways:
- Pure virtual functions define interfaces for derived classes
- Abstract classes cannot be instantiated directly
- Ensure all pure virtual methods are overridden in concrete classes
โ๏ธ Real-World Relevance:
Used in GUI frameworks, plugin architectures, simulation engines, data pipelines, and modular APIs.
โ Next Steps:
- Learn about Interfaces in C++ using Abstract Classes
- Explore Virtual Destructors and Clean Class Hierarchies
โFAQ โ C++ Pure Virtual Functions
โWhat is the syntax of a pure virtual function?
โ
virtual void functionName() = 0;
โCan an abstract class have normal (non-virtual) functions?
โ
Yes. You can mix pure virtual and concrete methods in abstract classes.
โCan constructors be pure virtual?
โ No. Constructors canโt be virtual or pure virtual.
โCan a pure virtual function have a body?
โ
Yes, but it’s rare. You can define it outside the class, though it’s still pure.
โHow is this different from virtual functions?
๐ Virtual functions have default behavior. Pure virtual functions have no implementationโthey must be overridden.
Share Now :