Skip to content

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.

Beginner 12 min read·Lesson 24 of 40 in C++ Programming

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

C++
class Account {
public:
virtual ~Account() = default;
virtual double monthly_fee() const { return 50; }
virtual void describe() const;
};
class Savings final : public Account {
public:
double monthly_fee() const override { return 0; }
void describe() const override { Account::describe(); /* then extra */ }
};
  • The virtual keyword is optional in the derived class; override is recommended.
  • Return types must match (or be covariant pointers/references).
  • final stops further overriding.

Code examples

override and calling the base

C++
#include <iostream>
class Notification {
public:
virtual ~Notification() = default;
virtual void send() const { std::cout << "[base] logging message\n"; }
};
class Email : public Notification {
public:
void send() const override {
Notification::send();
std::cout << "sending email\n";
}
};
int main() {
Email e;
const Notification& n = e;
n.send();
return 0;
}
Output
[base] logging message
sending email

override catches a signature mistake

C++
struct Base {
virtual void show() const;
};
struct Derived : Base {
void show() override; // error: missing const, so it overrides nothing
};

More examples

Name hiding and using-declarations

C++
#include <iostream>
struct Printer {
virtual ~Printer() = default;
virtual void print(int x) const { std::cout << "int " << x << '\n'; }
virtual void print(double x) const { std::cout << "double " << x << '\n'; }
};
struct Hider : Printer {
void print(int x) const override { std::cout << "Hider int " << x << '\n'; }
};
struct Keeper : Printer {
using Printer::print; // keep the double overload visible
void print(int x) const override { std::cout << "Keeper int " << x << '\n'; }
};
int main() {
Hider h;
h.print(2.5); // only print(int) is visible: 2.5 converts to 2
Keeper k;
k.print(2.5); // base print(double) is visible again
k.print(7);
return 0;
}
Output
Hider int 2
double 2.5
Keeper int 7

Step-by-step explanation

  • Declaring print(int) in Hider hides every print from 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:

TextOutput
Hider int 2
double 2.5
Keeper int 7

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 override on overrides.
  • Use final when 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

struct D : B { void update(float dt); }; // base has update(double)

Prefer

struct D : B { void update(double dt) override; };

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

Base() { init(); } // virtual init()

Prefer

Base() { /* non-virtual setup */ } // call init() after construction

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 returns Base*.

Key takeaways

  • Overriding replaces a virtual function with the same signature.
  • override makes the compiler check it.
  • Declaring a name in a derived class hides all base overloads of it.

Try it yourself

  1. 1
    Create Employee::pay() and override it in Contractor and Intern.
  2. 2
    Demonstrate name hiding with a base class that has log(int) and log(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?
Practise this topic