C++ Pure Virtual Functions
In this page:
What is a Pure Virtual Function?
A pure virtual function is declared in a base class with no default implementation at all, using the syntax 'virtual void functionName() = 0;'. It acts as a contract, forcing every concrete subclass to supply its own implementation before it can be instantiated.
Example: What is a Pure Virtual Function?
#include <iostream>
class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() override { std::cout << "Circle" << std::endl; }
};
int main() {
Circle c;
c.draw();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Implementing Pure Virtual Functions
Any concrete (non-abstract) child class is required to implement every pure virtual function it inherits; if it fails to override even one of them, that child class itself becomes abstract too and inherits the same restriction against being instantiated directly.
Example: Implementing Pure Virtual Functions
#include <iostream>
class Shape {
public:
virtual void draw() = 0;
};
class Square : public Shape {
public:
void draw() override { std::cout << "Square" << std::endl; }
};
int main() {
Square s;
s.draw();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Partial Implementation
If an intermediate class in a hierarchy inherits from an abstract class but doesn't override its pure virtual function, that intermediate class remains abstract as well — only a class further down the chain that finally provides the implementation becomes concrete and instantiable.
Example: Partial Implementation
#include <iostream>
class Shape {
public:
virtual void draw() = 0;
virtual void resize() = 0;
};
class PartialShape : public Shape {
public:
void draw() override { std::cout << "Drawing" << std::endl; }
// resize() not overridden: PartialShape is still abstract
};
class FullShape : public PartialShape {
public:
void resize() override { std::cout << "Resizing" << std::endl; }
};
int main() {
FullShape s;
s.draw();
s.resize();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pure Virtual with Body
Somewhat surprisingly, a pure virtual function is allowed to have a function body in C++, even though it must still be overridden to make the class concrete. That body must be defined outside the class definition, and derived classes can optionally call it explicitly as shared default logic.
Example: Pure Virtual with Body
#include <iostream>
class Shape {
public:
virtual void draw() = 0;
};
void Shape::draw() {
std::cout << "Default shape drawing logic" << std::endl;
}
class Circle : public Shape {
public:
void draw() override {
Shape::draw();
std::cout << "Circle" << std::endl;
}
};
int main() {
Circle c;
c.draw();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pure Virtual Destructors
You can declare a destructor as pure virtual, which forces the class to remain abstract, but you must still provide a body for it, since every destructor in the inheritance chain — including this one — will eventually be called when a derived object is destroyed.
Example: Pure Virtual Destructors
#include <iostream>
class Shape {
public:
virtual ~Shape() = 0;
};
Shape::~Shape() {
std::cout << "Shape destructor body" << std::endl;
}
class Circle : public Shape {
public:
~Circle() { std::cout << "Circle destructor" << std::endl; }
};
int main() {
Shape *s = new Circle();
delete s;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: