Module 6 · Inheritance and Polymorphism — topic 3 of 5
Function Overriding
A derived class can replace a base-class virtual function. Learn override, final, calling the base version, and the difference between overriding, overloading and hiding.
Learning objectives
- Override virtual functions using the override keyword
- Call the base implementation from an override
- Distinguish overriding, overloading and name hiding
Definition
Function overriding occurs when a derived class declares a function with the same name, parameter list and cv/ref qualifiers as a virtual function in its base; the derived version replaces the base version in dynamic dispatch.
Introduction
Overriding is how a derived class customises behaviour: SavingsAccount::monthly_fee() returns 0 while Account::monthly_fee() returns 50. The signatures must match exactly, otherwise you accidentally create a new, unrelated function.
Why it matters
Subtle signature mismatches (a missing const) are a classic bug; override makes the compiler catch them.
Interviews frequently ask the difference between overloading and overriding.
Detailed explanation
Mark overrides with override. If no base virtual function matches, compilation fails — protecting against typos and signature drift.
final on a function prevents further overriding; on a class it prevents further derivation.
Call the base version explicitly: Base::describe(); — useful for extending rather than replacing behaviour.
Overloading: same name, different parameters, same scope. Overriding: same signature, virtual, base/derived. Hiding: a derived function with the same name but different parameters hides all base overloads of that name (bring them back with using Base::name;).
Syntax
- The
virtualkeyword is optional in the derived class;overrideis recommended. - Return types must match (or be covariant pointers/references).
finalstops further overriding.
Code examples
override and calling the base
[base] logging message sending email
override catches a signature mistake
More examples
Name hiding and using-declarations
Hider int 2 double 2.5 Keeper int 7
Step-by-step explanation
- Declaring
print(int)in Hider hides everyprintfrom the base. h.print(2.5)therefore converts 2.5 to 2 and calls Hider's version.using Printer::print;re-exposes the base overloads in Keeper.- Keeper's own override still handles the int case.
Expected output
Running “Name hiding and using-declarations” prints exactly:
Practical applications
- Framework classes expose virtual hooks (onClick, onStart) that you override.
- Exception classes override
what(). - Logging decorators call the base implementation then add behaviour.
Best practices
- Always write
overrideon overrides. - Use
finalwhen a class is not designed for further derivation. - Add
using Base::name;when you override one of several overloads.
Common mistakes
Accidental overload instead of override
Avoid
Prefer
A different parameter type creates a new function; dynamic dispatch never calls it. override reveals the mistake.
Calling a virtual function from a constructor
Avoid
Prefer
During base construction the object is still a Base, so the derived override is not called.
Important notes
- Static (non-virtual) functions with the same signature in a derived class hide the base version; they don't override it.
- Covariant return types allow a derived override to return
Derived*where the base returnsBase*.
Key takeaways
- Overriding replaces a virtual function with the same signature.
overridemakes the compiler check it.- Declaring a name in a derived class hides all base overloads of it.
Try it yourself
- 1Create
Employee::pay()and override it inContractorandIntern. - 2Demonstrate name hiding with a base class that has
log(int)andlog(const std::string&).
Finished reading? Mark it complete to update your dashboard.
Suggested practice
4 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat does the `override` specifier do?
- ModerateBase declares `virtual void show() const;` and Derived writes `void show() override;` (no const). What happens?
- ModerateWhat is printed?