Difficulty: Beginner
What is a pure virtual function and an abstract class? Can an abstract class have a constructor or a pure virtual function with a body?
Abstract classes are how C++ expresses 'this is a contract, not a concrete thing', and they are the backbone of interface-driven design. Let's walk through them.
A pure virtual function is a virtual function declared with '= 0', such as 'virtual double area() const = 0;'. It says: every concrete class derived from me must provide this function. A class with at least one pure virtual function is called abstract, and you cannot create objects of it. Trying 'Shape s;' is a compile error. But you can absolutely have pointers and references to an abstract type, which is exactly how polymorphism is used: 'Shape* s = new Circle(2);' or 'std::unique_ptr<Shape>'.
An analogy helps: think of a job description. 'Vehicle must have a drive() method' is the contract, but you cannot ride a 'Vehicle'; you ride a Car or a Bike. The abstract class describes what, and derived classes decide how.
A derived class that does not override all pure virtual functions is itself abstract. It becomes concrete only when every pure virtual has an implementation. This enforces at compile time that nobody forgets to implement a required operation, which is a big advantage over documentation or runtime checks.
The questions that follow are usually the tricky parts. Can an abstract class have constructors? Yes. Its constructor runs as part of building any derived object; it just cannot be used to make a standalone instance. It can also have data members and non-virtual functions. Can a pure virtual function have a body? Yes: you can define it outside the class, and derived classes may call it explicitly as Base::f(). This is legal and sometimes used to provide default behaviour that still forces derived classes to make a conscious choice. The most common real-world case is a pure virtual destructor: 'virtual ~Base() = 0;' makes the class abstract even if there are no other virtual functions, but you must still provide a definition 'Base::~Base() {}', because destructors of base classes are always called.
C++ has no interface keyword. By convention, an interface is an abstract class with only pure virtual functions and a virtual destructor, and no data. Classes can implement multiple interfaces through multiple inheritance, which is the safe, common use of it.
Keep in mind that abstract classes are a runtime polymorphism tool. If you know the types at compile time, templates and concepts can provide the same contract without the vtable cost.
#include <iostream>
#include <memory>
#include <vector>
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0; // pure virtual
virtual void describe() const = 0; // pure virtual WITH a body below
};
void Shape::describe() const { std::cout << "a shape"; }
class Rect : public Shape {
double w_, h_;
public:
Rect(double w, double h) : w_(w), h_(h) {}
double area() const override { return w_ * h_; }
void describe() const override { Shape::describe(); std::cout << " (rectangle)"; }
};
int main() {
// Shape s; // error: cannot instantiate abstract class
std::vector<std::unique_ptr<Shape>> v;
v.push_back(std::make_unique<Rect>(3, 4));
v.push_back(std::make_unique<Rect>(2, 5));
for (const auto& s : v) {
s->describe();
std::cout << " area=" << s->area() << "\n";
}
}
Shape cannot be instantiated, but Shape pointers hold concrete Rects. describe() is pure virtual yet has a body which the override calls explicitly.
pure virtual, abstract class, interface, polymorphism