01 / 20

Override & Final
Keywords in C++

Master the override and final keywords to write safer virtual functions, catch bugs at compile time, and control class inheritance.

C++ Object-Oriented Programming

Quick Recap: Virtual Functions

In C++, a virtual function in a base class can be overridden in a derived class. When you call it through a base-class pointer, the derived version runs. This is runtime polymorphism.

class Shape { public: virtual double area() const { return 0.0; } }; class Circle : public Shape { public: double area() const { return 3.14159 * r * r; } };

The Silent Bug Problem

Without override, a small typo means you create a new function instead of overriding. The compiler says nothing. The bug hides until runtime.

class Shape { public: virtual double area() const; }; class Circle : public Shape { public: // Oops! Missing 'const': this is NOT an override double area() { return 3.14 * r * r; } };

Enter the override Keyword

C++11 introduced override: a specifier you place after the function declaration. It tells the compiler: "I intend to override a base-class virtual function."

class Circle : public Shape { public: double area() const override; // ✓ Correct double area() override; // ✕ Compiler ERROR };

How override Works

Base Class
virtual void draw() const;
virtual int size();
Derived Class
void draw() const override;
int size() override;
Compiler Check Does the base class have a virtual function with this exact signature? ✓ Compiles OK ✕ Compile Error

The final Keyword

final does the opposite job: it prevents further overriding of a virtual function, or prevents a class from being inherited at all.

final on a Function
No derived class can override this specific function any further.
final on a Class
No class can inherit from this class at all. The hierarchy stops here.

final in Practice

// 1) final on a virtual function class Shape { public: virtual void draw() const = 0; }; class Circle : public Shape { public: void draw() const override final; // locked! }; class Dot : public Circle { public: void draw() const override; // ✕ ERROR: draw is final };
// 2) final on a class class Circle final : public Shape { // ... }; class Dot : public Circle {}; // ✕ ERROR: Circle is final

override vs final

override final
Purpose Confirm you are overriding Prevent further overriding
Applies to Virtual member functions Functions or entire classes
When it fires Signature mismatch with base Any attempt to override / inherit
Introduced in C++11 C++11
Can be combined? Yes: override final

Visualizing the Hierarchy

Shape
virtual draw() = 0
Circle
draw() override final
Rectangle
draw() override
final ✕ Cannot Override RoundedRect
draw() override
Can inherit Blocked by final

Why Use Them?

1
Catch typos instantly. A misspelled name or wrong const qualifier triggers a compile error with override.
2
Document intent. Readers immediately know a function is meant to replace a base-class version.
3
Protect invariants. Use final to ensure critical behavior (security checks, logging) cannot be bypassed by subclasses.
4
Enable optimizations. The compiler can devirtualize calls to final functions, avoiding vtable lookups.

Step-by-Step Checklist

1
Declare base function virtual. Without virtual in the base class, override is meaningless.
2
Match the signature exactly. Return type, parameters, const-ness, and ref-qualifiers must all match.
3
Add override after the declaration. Place it after the parameter list (and any const/noexcept), before the semicolon or body.
4
Add final only when needed. Use it when you want to lock a function or an entire class from further derivation.
5
Compile and verify. If you made a mistake, the compiler tells you immediately. Fix the signature or intent.

Example 1: Basic Override

class Animal { public: virtual std::string speak() const { return "..."; } virtual ~Animal() = default; }; class Dog : public Animal { public: std::string speak() const override { return "Woof!"; } };

Example 2: override + final

class Logger { public: virtual void log(const std::string& msg) = 0; virtual ~Logger() = default; }; class SecureLogger : public Logger { public: void log(const std::string& msg) override final { encrypt(msg); write(msg); } }; class HackerLogger : public SecureLogger { public: void log(const std::string& msg) override; // ✕ ERROR: log() is final in SecureLogger };

Example 3: final on a Class

class Singleton final { public: static Singleton& instance() { static Singleton s; return s; } private: Singleton() = default; }; class MySingleton : public Singleton {}; // ✕ ERROR: Singleton is marked final

Common Mistakes

✕ Missing virtual in base
Using override on a function that isn't virtual in the base: the compiler rejects it, but the real fix is adding virtual to the base.
✕ Signature mismatch
Forgetting const, using a different return type, or wrong parameter types. override catches this, but you must fix the signature.
✕ Using final too eagerly
Marking functions or classes final prematurely prevents legitimate extension. Use it only when you have a clear reason to lock things down.

Summary Cheat Sheet

Syntax Meaning Error If...
void f() override; I'm overriding a base virtual No matching virtual in base
void f() final; No subclass may override f() A subclass tries to override f()
void f() override final; I'm overriding + locking it Either condition fails
class C final { }; No class may inherit from C Any class tries to derive from C

Question 1 of 5

What happens if you write override on a function whose signature does not match any virtual function in the base class?

Question 2 of 5

Which of the following correctly uses final to prevent a class from being inherited?

Question 3 of 5

Will the code above compile? If not, why?

Question 4 of 5

If a function is marked override final in class Middle, which of the following is true?

Question 5 of 5

What is one practical performance benefit of marking a virtual function as final?