๐ WAYPOINT LESSON
Overriding and the override Keyword
โญโญ intermediateโณ 20 min read๐ Lesson 83 of 204
Exact signature matching, override vs overload, and how override turns silent bugs into compile errors.
A derived class may override a base member function โ but only if the
signatures match exactly, and the base must say the function may be
overridden (virtual, next lesson). Overriding is not overloading.
#include <iostream>
#include <string>
class Animal {
public:
virtual ~Animal() = default;
virtual void speak() const { std::cout << "...\n"; }
};
class Dog : public Animal {
public:
void speak() const override { std::cout << "Woof\n"; } // overrides
void speak(int times) const; // would OVERLOAD, not override
};
class Cat : public Animal {
public:
void speak() const override { std::cout << "Meow\n"; }
};
override: write it, always
override asks the compiler to verify a function really overrides something.
Without it, a typo or a const mismatch silently creates a new function:
class Bird : public Animal {
public:
void Speak() const override; // compile error: no such base function (typo caught!)
void speak() /* missing override + missing const */; // new function, silent bug
};
The matching checklist
Same name, same parameter types, same const-qualification, compatible
return type. override checks all of it for you at compile time โ the
keyword costs nothing and removes an entire class of quiet bugs.