C/C++ Arena

Inheritance and virtual functions in C++

Base and derived classes, virtual functions and override, abstract classes, and why virtual destructors matter.

A derived class inherits from a base class and can override its virtual functions. Calling a virtual function through a base pointer or reference runs the version for the object's real type: that's runtime polymorphism.

Example

#include <iostream>
#include <memory>
#include <vector>

struct Shape {
    virtual double area() const = 0;
    virtual ~Shape() = default;
};
struct Rect : Shape {
    double w, h;
    Rect(double w_, double h_) : w(w_), h(h_) {}
    double area() const override { return w * h; }
};
struct Circle : Shape {
    double r;
    explicit Circle(double r_) : r(r_) {}
    double area() const override { return 3.14 * r * r; }
};

int main() {
    std::vector<std::unique_ptr<Shape>> shapes;
    shapes.push_back(std::make_unique<Rect>(2, 3));
    shapes.push_back(std::make_unique<Circle>(1));
    double total = 0;
    for (const auto &s : shapes) total += s->area();
    std::cout << total << "\n";
    return 0;
}

Output:

9.14

Watch it run: Virtual functions pick the real type

Practice it