Overriding, override & final in C++
Overriding replaces an inherited virtual function with a derived version of matching signature. The override specifier makes the intent compiler-checked: a typo'd name or mismatched const silently creates a new hiding function without it, but with override the compiler errors instead of misbehaving.
final closes the door from the other side — a final class can't be inherited, a final virtual can't be overridden further. Together they document design intent (this hierarchy ends here) and occasionally help the optimizer devirtualize calls.
Match the signature exactly
Same name, same parameters, same constness — any deviation hides the base function instead of overriding it.
Always write override
void speak() override; asks the compiler to verify a matching base virtual exists — signature drift becomes a loud error.
final seals hierarchies
class Engine final blocks all inheritance; void run() final blocks further overriding — use sparingly, where extension is genuinely wrong.
Covariant returns allowed
An override may return a derived-class pointer/reference where the base returns a base one — the one legal signature difference.
#include <iostream> class Base {public: virtual void run() { std::cout << "base" << std::endl; } virtual ~Base() = default;}; class Child : public Base {public: void run() override { std::cout << "child" << std::endl; }}; int main() { Base* p = new Child(); p->run(); delete p; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.