Function overriding in C++ happens when a derived class provides its own version of a virtual function declared in a base class, so calls through a base pointer or reference run the derived code. The override keyword, added in C++11, tells the compiler you intend to override, and it reports an error if no matching virtual function exists. Using the override keyword c++ offers is the simplest way to catch silent mistakes in class hierarchies.
finalA function is overridden only when the base declares it virtual and the derived class declares a function with the same name, the same parameter types, the same constness and a compatible return type.
The call goes through an Animal pointer, yet Dog::speak runs. The compiler dispatches at run time using the object's dynamic type, usually through a vtable pointer stored in the object. Once a function is virtual in a base, it stays virtual in every derived class, even without the virtual keyword.
Without override, a small mismatch quietly creates a new function instead of overriding:
If you wrote void run(int x) without const in Good, the compiler would stop with an error such as "marked override, but does not override". Make override mandatory in your code style; many compilers can warn when you forget it (for example -Wsuggest-override in GCC and -Winconsistent-missing-override in Clang).
override is a contextual keyword, so it is only special after a member function declarator and still works as an identifier elsewhere.
finalfinal blocks further overriding of a virtual function, or inheritance from a whole class.
Beyond design intent, final lets the compiler devirtualise calls when it knows the dynamic type cannot change, which can remove the indirect call.
If the base function is not virtual, a function with the same name in the derived class hides it. The static type of the pointer decides which one runs.
Name hiding also affects overloads. A derived function named run hides every run overload of the base unless you bring them back with using Base::run;.
An overrider may return a pointer or reference to a more derived class than the base function does. This is called a covariant return type.
Default arguments are resolved statically from the static type, not the dynamic one. Overriding a function and giving it a different default value leads to surprising behaviour, so repeat the same default or avoid defaults on virtual functions.
Call the base implementation with a qualified name: Animal::speak();. Other traps to watch for:
private in the base does not stop overriding. Access control and virtual dispatch are independent, which the non-virtual interface idiom relies on.No, but it is strongly recommended. Without it, a signature mismatch compiles and creates a new function that is never called through the base.
You can declare a function with the same name, but it hides the base one and is chosen by static type. It does not take part in dynamic dispatch.
No. The function is already virtual. Writing override is enough, and it makes virtual redundant.
override checks that you override something. final forbids any further override in classes derived from yours.
Overriding replaces a virtual function in a derived class so calls are resolved at run time. Put override on every overrider so the compiler checks the signature, use final when a class or function must not be extended further, and remember that non-virtual functions are hidden rather than overridden. Keep a virtual destructor in polymorphic bases.