Derived Classes
A derived function overrides a base virtual function only when name and type match exactly.
When Similar Names Mislead
A function in a derived class can look like it was written to replace a virtual function in a base class. But a similar-looking name is not proof that the functions are connected. To decide whether overriding occurs, compare the declared name and type exactly.
What do you think happens?
A base class has a virtual function named render with a particular type. A derived class has a function named render with a different type. Does the derived function override the base function?
Reveal answer
No. The name matches, but the type does not, so the exact-match test is not satisfied.
A difference in even one part of the type, such as a parameter type, breaks the match.
Apply the Exact Match Test
A derived function overrides a base class virtual function only when its name and type match exactly. Do not decide from how similar the declarations look at a glance. A near match may have been intended as an override, or it may be a separate function; appearance alone does not tell you which.
The first comparison passes because both the name and type match. The second fails because the type differs, even though the name is the same. The type check covers the declared function type: a difference in a parameter type is enough to make the function a near match rather than an override.
What a Near Match Means
Deciding whether two declarations match
A base class has a virtual function named update with a particular type. Consider two functions in a derived class: one is also named update and has the same type; the other is named updates and has the same type. Which one overrides?
- 1Check the first derived function
Its name is update, and its type is the same as the base virtual function’s type. Both parts of the exact-match test pass.
- 2Check the second derived function
Its type matches, but its name is updates rather than update. The name differs, so the exact-match test fails.
- 3Decide what the near match establishes
The second function does not override under this rule. It may have been intended as an override, or it may be a separate function; its appearance does not settle the programmer’s intent.
The derived function named update with the matching type overrides the base virtual function. The function named updates does not.
Make Intent Visible with override
The override keyword lets a programmer state explicitly that a derived class function is meant to override a base class virtual function. The explicit form is described as equivalent to writing the function without the keyword; its value is making the programmer’s intent visible, not changing what the function does.
This clarity is especially useful when a class hierarchy is large and contains many functions with similar names. A reader may not know whether a near match was intended to replace an inherited virtual function or to add a separate function. Stating the intent helps remove that uncertainty. The keyword does not make a mismatched name or type satisfy the exact-match test.
Mistakes to Avoid
Common mistake Assuming a similar name guarantees an override.
What it looks likeThe base function is named update, while the derived function is named updates.
Why it failsThe names are not identical, so the exact name-and-type test fails.
Do this insteadCompare the full declared name and type instead of relying on visual similarity.
Common mistake Checking the name but overlooking a type difference.
What it looks likeThe base and derived functions have the same name, but one parameter type differs.
Why it failsA difference in a single parameter type is enough to break the match.
Do this insteadCheck the type as well as the name, including the parameter types.
Common mistake Treating override as a switch that makes a function override.
Why it failsThe keyword states the programmer’s intent; it does not replace the exact name-and-type rule.
Do this insteadFirst verify the exact match, then use override to make the intended relationship clear.
Practice the Match
A base class has a virtual function named calculate with a particular type. For each derived function, decide whether it overrides: one has the name calculate and the same type; another has the name calculate and a different parameter type; a third has the name calculator and the same type. Explain which part of the test each declaration passes or fails.
Hints
- Test the name and type separately.
- A single parameter-type difference is enough to fail the type match.
- A different name fails the test even when the type matches.
Key Takeaways
- A derived function overrides a base virtual function only when its name and type match exactly.
- A near match may be intended as an override or as a separate function; appearance alone does not establish intent.
- A difference in a parameter type or in the function name breaks the exact-match test.
- The override keyword makes the programmer’s intent explicit and helps readers, especially in large class hierarchies.