Polymorphism in C++
#include <iostream>
#include <format>
#include <vector>
#include <memory>
#include <string>
#include <numbers>
#include <cmath>1 Abstract base class (interface)
These OOP constructs model interfaces, reuse, and dynamic behavior.
Use this when types share contracts or behavior across hierarchies.
It organizes abstractions and enables polymorphic extension.
Define clear base contracts and override behavior intentionally in derived types.
Pure virtual functions (= 0) make this class abstract. You cannot instantiate Shape directly.
(virtual ~Base() = 0;), you still MUST provide a definition for it (e.g., Base::~Base() = default;) because the derived class destructor implicitly calls it. A missing definition will produce a linker error, not a compiler error, which can be confusing.
Watch out: If you declare a pure virtual destructor
class Shape {
public:
virtual ~Shape() = default;
// Pure virtual: every derived class MUST implement these
[[nodiscard]] virtual double area() const = 0;
[[nodiscard]] virtual double perimeter() const = 0;
[[nodiscard]] virtual std::string name() const = 0;
// Non-virtual: shared behavior using virtual methods
void describe() const {
std::cout << std::format("{}: area={:.2f}, perimeter={:.2f}\n",
name(), area(), perimeter());
}
};Virtual Dispatch (vtable / vptr) Deep Dive
class Circle : public Shape {
double radius_;
public:
explicit Circle(double r) : radius_(r) {}
[[nodiscard]] double area() const override {
return std::numbers::pi * radius_ * radius_;
}
[[nodiscard]] double perimeter() const override {
return 2.0 * std::numbers::pi * radius_;
}
[[nodiscard]] std::string name() const override {
return "Circle";
}
// Circle-specific method (not in base)
[[nodiscard]] double diameter() const { return 2.0 * radius_; }
};
class Rectangle : public Shape {
double width_, height_;
public:
Rectangle(double w, double h) : width_(w), height_(h) {}
[[nodiscard]] double area() const override {
return width_ * height_;
}
[[nodiscard]] double perimeter() const override {
return 2.0 * (width_ + height_);
}
[[nodiscard]] std::string name() const override {
return "Rectangle";
}
};
class Triangle : public Shape {
double a_, b_, c_; // Side lengths
public:
Triangle(double a, double b, double c) : a_(a), b_(b), c_(c) {}
[[nodiscard]] double area() const override {
// Heron's formula
double s = (a_ + b_ + c_) / 2.0;
return std::sqrt(s * (s - a_) * (s - b_) * (s - c_));
}
[[nodiscard]] double perimeter() const override {
return a_ + b_ + c_;
}
[[nodiscard]] std::string name() const override {
return "Triangle";
}
};3 Runtime polymorphism in action
The derived part is "sliced off" and only the base sub-object is copied, silently losing overridden behaviour.
Use this when it cleanly solves the problem in front of you.
A function that works with ANY Shape.
Use it in code like: double total_area(const std::vector<std::unique_ptr<Shape>>& shapes) {.
A function that works with ANY Shape.
by value in a base-typed variable or container (e.g., std::vector<Shape>). The derived part is "sliced off" and only the base sub-object is copied, silently losing overridden behaviour. Always use pointers or references (typically std::unique_ptr<Base>) to preserve polymorphism.
Watch out: Object slicing occurs when you store a derived object
Object Slicing Deep Dive
A derived object's memory layout is:
Circle object:
+---------------------------+
| vptr (-> Circle's vtable) | \
| [Shape base members] | } Shape sub-object (first N bytes)
+---------------------------+ /
| radius_ | <- Circle-specific data
+---------------------------+
When you copy a Circle into a Shape-typed variable:
Shape s = my_circle; // copy constructor of Shape is called
only the first N bytes (the Shape sub-object) are copied. The
radius_ field is literally chopped off — it does not exist in s.
Crucially, the vptr in the copy is set to Shape's vtable (by
Shape's copy constructor), so virtual dispatch on s calls Shape's
methods, not Circle's. The derived behaviour is permanently lost. double total_area(const std::vector<std::unique_ptr<Shape>>& shapes) {
double sum = 0.0;
for (const auto& s : shapes) {
sum += s->area(); // Calls the correct override via vtable
}
return sum;
}4 CRTP - Static (compile-time) polymorphism
Static (compile-time) polymorphism.
} If targeting C++23 or later, prefer deducing this over CRTP.
No virtual function overhead.
Use it in code like: template <typename Derived>.
No virtual function overhead. The base class "knows" the derived type at compile time.
"deducing this" (explicit object parameters) provides the same compile-time dispatch without the template boilerplate: void print(this const auto& self) { ... } If targeting C++23 or later, prefer deducing this over CRTP.
Watch out: CRTP can be tricky to read and maintain. In C++23,
CRTP (Curiously Recurring Template Pattern) Deep Dive
When you write class Coordinate : public Printable<Coordinate>, the compiler instantiates Printable<Coordinate>. Inside Printable<Coordinate>::print(), the static_cast<const Coordinate&>(*this) is always valid because *this IS a Coordinate (Coordinate inherits from Printable<Coordinate>, so every Printable<Coordinate> object is actually the Coordinate sub-object of a Coordinate instance). Because Derived is a template parameter known at compile time, the compiler resolves Derived::to_string() statically — there is no vtable, no vptr, and no pointer indirection at runtime. In optimized builds (-O2 and above), the call to to_string() is typically inlined directly into print(), yielding zero overhead. Reference: reference/en/cpp/language/crtp
template <typename Derived>
class Printable {
public:
void print() const {
// Calls derived class's to_string() -- resolved at compile time
const auto& self = static_cast<const Derived&>(*this);
std::cout << self.to_string() << '\n';
}
};
class Coordinate : public Printable<Coordinate> {
int x_, y_;
public:
Coordinate(int x, int y) : x_(x), y_(y) {}
[[nodiscard]] std::string to_string() const {
return std::format("({}, {})", x_, y_);
}
};
class Color : public Printable<Color> {
int r_, g_, b_;
public:
Color(int r, int g, int b) : r_(r), g_(g), b_(b) {}
[[nodiscard]] std::string to_string() const {
return std::format("rgb({}, {}, {})", r_, g_, b_);
}
};int main() {
// ---- Runtime polymorphism ----
std::cout << "--- Runtime Polymorphism ---\n";
// Store different shapes through base class pointers
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Circle>(5.0));
shapes.push_back(std::make_unique<Rectangle>(4.0, 6.0));
shapes.push_back(std::make_unique<Triangle>(3.0, 4.0, 5.0));
// Polymorphic dispatch: each shape calls its own describe()
for (const auto& s : shapes) {
s->describe();
}
std::cout << std::format("Total area: {:.2f}\n", total_area(shapes));
// ---- dynamic_cast for safe downcasting ----
// Watch out: dynamic_cast relies on RTTI (Run-Time Type Information).
// Some codebases compile with -fno-rtti to reduce binary size and
// improve performance, which disables dynamic_cast entirely.
//dynamic_cast Deep Dive
The compiler stores RTTI (Run-Time Type Information) alongside
each class's vtable. RTTI includes the class name and a list of
base classes. When you write dynamic_cast<Circle*>(raw), the
runtime:
1. Follows raw's vptr to find the vtable and its RTTI record.
2. Traverses the RTTI inheritance graph to determine whether
the actual (most-derived) type is, or derives from, Circle.
3a. For pointer casts: returns a correctly adjusted pointer on
success, or nullptr on failure (no exception thrown).
3b. For reference casts (dynamic_cast<Circle&>(ref)): throws
std::bad_cast on failure, because a reference cannot be null.
Because it reads the vtable, dynamic_cast requires the source
type to be polymorphic (i.e., have at least one virtual function).
Attempting dynamic_cast on a non-polymorphic type is a compile error.
Reference: reference/en/cpp/language/dynamic_cast std::cout << "\n--- dynamic_cast ---\n";
Shape* raw = shapes[0].get();
// Try to cast to Circle (should succeed for shapes[0])
if (auto* circle = dynamic_cast<Circle*>(raw)) {
std::cout << std::format("Diameter: {:.2f}\n", circle->diameter());
}
// Try to cast to Rectangle (should fail for shapes[0])
if (auto* rect = dynamic_cast<Rectangle*>(raw)) {
std::cout << "This is a rectangle\n";
} else {
std::cout << "Not a rectangle -- dynamic_cast returned nullptr\n";
}
// ---- CRTP: static polymorphism ----
std::cout << "\n--- CRTP (Static Polymorphism) ---\n";
Coordinate coord(10, 20);
Color color(255, 128, 0);
coord.print(); // Calls Coordinate::to_string at compile time
color.print(); // Calls Color::to_string at compile time
return 0;
}