A C++ virtual function lets a call made through a base-class pointer or reference select the final overrider for the object’s dynamic type. This is runtime polymorphism: code can work with an interface without knowing which derived implementation it receives. It is particularly useful for plugins, rendering strategies and other bounded extension points, but it adds a contract that must be designed deliberately.
A compilable virtual/override example
The base class below declares one virtual operation and a virtual destructor. Derived classes mark their implementations override, so the compiler rejects a mistaken signature instead of silently creating an unrelated overload. The call through a Shape& selects the implementation for the actual object.
#include <iostream>
#include <memory>
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
};
class Rectangle final : public Shape {
double width_, height_;
public:
Rectangle(double w, double h) : width_(w), height_(h) {}
double area() const override { return width_ * height_; }
};
void report(const Shape& s) { std::cout << s.area() << "\n"; }
int main() {
Rectangle r(3, 4);
report(r); // 12
std::unique_ptr<Shape> p = std::make_unique<Rectangle>(2, 5);
report(*p); // 10
}
c++ -std=c++17 -Wall -Wextra polymorphism.cpp -o polymorphism
./polymorphism
Why a virtual destructor matters
Deleting a derived object through a base pointer requires an appropriate virtual destructor in ordinary polymorphic class hierarchies. Without one, deletion through the base pointer can have undefined behavior. Here Shape declares virtual ~Shape(), allowing std::unique_ptr<Shape> to clean up a derived object safely when the pointer is destroyed.
Do not use manual new and delete merely to demonstrate polymorphism in production code. Value ownership and smart pointers make lifetime rules easier to audit.
Abstract classes and pure virtual functions
Declaring area() const = 0 makes Shape abstract: it cannot be instantiated as a complete concrete object. This is useful when every concrete shape must supply a meaningful area calculation. A pure virtual function can still have a separate definition in some designs, but the derived class is responsible for meeting the abstract interface before it is concrete.
Avoid object slicing
Passing a derived object by value to a function expecting a concrete base object can slice off derived state and behavior. Accept a const Base& or pointer for polymorphic operations, as report() does. Value-oriented code without inheritance is often simpler if callers do not need runtime selection.
Know what override and final do
override checks that a virtual function actually overrides a base declaration. final on a class prevents further derivation; on an overriding function it blocks additional overrides. These specifiers make an intended API contract visible to reviewers, but they do not make the class thread-safe or prevent memory-management bugs.
Runtime dispatch is not free or always necessary
Implementations commonly use a virtual table and an indirect call, but the C++ language does not require a particular table layout. The real trade-off is between flexibility, ownership complexity, testability and performance in a measured workload. Templates or std::variant may be preferable when the set of concrete types is known at compile time.
The C++ virtual-function reference details the overriding rules and the virtual-destructor requirement. Use a small compile test with warnings enabled before adopting virtual dispatch in a larger interface.
Checklist for a safe polymorphic interface
Declare a virtual destructor when callers may delete derived instances through a base pointer; use override on each overriding member; prefer references and smart pointers over raw owning pointers; document preconditions and postconditions; and test at least two derived types and base-reference dispatch. If most methods are unrelated, the interface may be too broad.