A pure virtual function in C++ is a virtual function that has no required complete implementation in the base class and forces derived classes to provide their own implementation if they want to become concrete usable classes. It is one of the most important tools for defining abstract interfaces in C++. When a class contains at least one pure virtual function, that class becomes abstract and cannot be instantiated directly.
This topic matters because pure virtual functions take virtual dispatch one step further. A normal virtual function says, “this function can be overridden.” A pure virtual function usually says, “this function must be overridden by derived classes if they want to provide a complete object type.” That difference is essential in framework design, plugin systems, hardware abstractions, and any C++ codebase built around common interfaces.
What Is a Pure Virtual Function in C++?
A pure virtual function is a virtual function declared in a base class with the syntax = 0 at the end of its declaration. This tells the compiler that the function represents an interface requirement rather than a normal fully usable base implementation.
In practical terms, a pure virtual function expresses that the base class knows a certain operation must exist, but it does not want to define a normal complete implementation for that operation at the class interface level. Instead, concrete derived classes are expected to define the actual behavior.
A pure virtual function in C++ declares an operation that derived classes are expected to implement, and it makes the containing class abstract.
Syntax of Pure Virtual Function in C++
The syntax is simple. We declare the function with the virtual keyword and place = 0 at the end.
class Base
{
public:
virtual void show() = 0;
};
This syntax means show() is a pure virtual function. The base class is now abstract because it contains a pure virtual function.
Why Pure Virtual Functions Are Needed in C++
Sometimes a base class should define a common interface without pretending that the base class itself is a meaningful complete object. For example, a class named Shape may describe the idea of a shape, but the program may only want concrete objects such as Circle, Rectangle, and Triangle. In such cases, a pure virtual function helps say, “every real shape must implement this operation, but the generic shape type itself is not meant to be instantiated.”
- They enforce a contract on derived classes.
- They help create abstract interfaces.
- They prevent direct creation of incomplete base objects.
- They improve design clarity when the base type is only conceptual.
- They support interface-based polymorphism in C++.
These are strong design reasons, not just syntax preferences. Pure virtual functions help the class hierarchy express intent clearly.
Simple Example of Pure Virtual Function in C++
The following example shows a base class with a pure virtual function and a derived class that provides the required implementation.
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw() = 0;
};
class Circle : public Shape
{
public:
void draw() override
{
cout << "Drawing circle" << endl;
}
};
int main()
{
Circle c;
c.draw();
return 0;
}
Here, Shape cannot be instantiated directly because it is abstract. But Circle provides the missing implementation of draw(), so Circle becomes a concrete usable class.
Pure Virtual Function Makes a Class Abstract
When a class contains at least one pure virtual function, the class becomes an abstract class. That means objects of that class cannot be created directly. The class can still be used as a base class, pointer type, reference type, or interface layer, but it cannot become a normal concrete object until a derived class implements the required pure virtual functions.
This rule is what turns pure virtual functions into a design-enforcement mechanism. They do not just suggest that derived classes should implement a function. They require it for concrete instantiation.
Derived Class Must Implement the Pure Virtual Function
If a derived class does not implement all inherited pure virtual functions, then the derived class also remains abstract. This can continue across multiple inheritance levels until some final derived class provides the missing implementations.
That means abstractness can travel down the hierarchy until the interface requirements are actually fulfilled. This is very useful when a mid-level derived class is still meant to be conceptual rather than final.
class Base
{
public:
virtual void show() = 0;
};
class Middle : public Base
{
};
class Final : public Middle
{
public:
void show() override
{
}
};
Here, Middle is still abstract because it does not implement show(). Only Final becomes concrete after implementing it.
Pure Virtual Function and Run-Time Polymorphism
Pure virtual functions still participate in run-time polymorphism. A base-class pointer or reference can point to a derived object, and calling the pure virtual interface function through that base pointer or reference will execute the derived implementation at runtime. This is one of the main reasons pure virtual functions are used for interface-based design.
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw() = 0;
};
class Rectangle : public Shape
{
public:
void draw() override
{
cout << "Drawing rectangle" << endl;
}
};
int main()
{
Shape* ptr;
Rectangle r;
ptr = &r;
ptr->draw();
return 0;
}
Even though ptr is a Shape*, the program calls Rectangle::draw() because that is the concrete implementation supplied by the derived object.
Pure Virtual Function vs Virtual Function in C++
A normal virtual function may provide a complete base implementation and may optionally be overridden by derived classes. A pure virtual function marks the function as an abstract requirement. This distinction is central to class interface design.
| Point | Virtual Function | Pure Virtual Function |
|---|---|---|
| Base implementation | Usually present | Declares an abstract requirement |
| Syntax | virtual void f() | virtual void f() = 0 |
| Effect on class | Class may remain concrete | Class becomes abstract |
| Derived responsibility | May override | Must override for concrete derived class |
This comparison helps explain why pure virtual functions are often used to express interfaces, while ordinary virtual functions are used when a base class can still provide some meaningful default behavior.
Can a Pure Virtual Function Have a Definition?
This is a subtle but important point. A pure virtual function can still have a definition outside the class in some cases, even though it is declared with = 0. The declaration still keeps the class abstract, but the base class may provide a definition that derived classes can call explicitly if needed.
This is not the usual first pattern beginners use, but it is worth knowing because it shows that “pure” does not mean “definition is impossible.” It means the function is an abstract requirement for class instantiation purposes.
Pure Virtual Destructor in C++
C++ also allows a destructor to be pure virtual, which can help make a base class abstract. However, a pure virtual destructor still needs a definition because the base part of an object must still be destroyed properly when derived objects are destroyed. This is one of those advanced but important language rules that surprises many learners.
This detail shows again that abstract interface design in C++ must still respect full object lifetime rules. Even abstract classes participate in construction and destruction chains.
Interface-Only Base Classes in C++
Pure virtual functions are often used to build interface-only base classes, where the base class mainly defines required operations and leaves real behavior to derived classes. This pattern is common when the framework needs a stable contract but should not depend on one fixed implementation. It keeps high-level code flexible while still enforcing that all concrete derived types provide the required operations.
Advantages of Pure Virtual Functions in C++
- They enforce interface contracts on derived classes.
- They prevent direct creation of incomplete conceptual base objects.
- They support clean abstraction in object-oriented designs.
- They make framework-style and plugin-style designs easier to express.
- They work naturally with run-time polymorphism.
These benefits are why pure virtual functions are widely used in serious C++ libraries and architectural designs built around interfaces.
Common Mistakes with Pure Virtual Functions in C++
- Trying to instantiate a class that still has a pure virtual function.
- Forgetting to implement the pure virtual function in a derived class intended to be concrete.
- Confusing pure virtual functions with ordinary virtual functions.
- Thinking that a pure virtual function can never have a definition.
- Ignoring destructor rules when abstract base classes are used polymorphically.
One of the most common beginner mistakes is wondering why a class cannot be instantiated even though it “almost” looks complete. If one pure virtual function remains unimplemented, the class is still abstract and object creation is not allowed.
Best Practices for Pure Virtual Functions in C++
- Use pure virtual functions when a base class should express an interface contract rather than a complete concrete type.
- Keep interface functions clear and meaningful.
- Make sure concrete derived classes implement all required pure virtual functions.
- Use virtual destructors in polymorphic abstract base classes.
- Do not use abstract interfaces where simpler non-polymorphic designs would be enough.
Good interface design is about clarity. If a base class represents a concept rather than a directly usable object, pure virtual functions often express that intent better than forcing a default implementation that does not really make sense.
Why Pure Virtual Functions Matter in Real C++ Software
Pure virtual functions matter because many real systems need abstract contracts rather than default concrete behavior. Device drivers, rendering engines, serializers, communication layers, command frameworks, and plugin APIs often need a base interface that says what operations must exist without claiming to know the one correct implementation. Pure virtual functions let C++ express that design directly.
That is why pure virtual functions are one of the main tools behind abstract interfaces in C++. They transform a base class from “a general parent with optional customization” into “a required contract that concrete derived classes must fulfill.”
Continue learning C++ in order
Follow the topic sequence with the previous and next lesson.