← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 4 of 11

C++ Pure Virtual Functions

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?

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.