Concepts / Derived Classes

Derived Classes

A derived function overrides a base virtual function only when name and type match exactly.

  • Programming

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?

  • Yes, because the names match.

  • No, because both the name and type must match exactly.

Reveal answer
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.

Base virtual functionname: render; type: Type ADerived functionname: render; type: Type ADerived functionname: render; type: Type Bname and type matchtype differs
Which declaration satisfies the exact match rule? Compare both the name and the type between the base virtual function and the derived function.

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

Worked example

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?

  1. 1
    Check 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.

  2. 2
    Check the second derived function

    Its type matches, but its name is updates rather than update. The name differs, so the exact-match test fails.

  3. 3
    Decide 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.

Result

The derived function named update with the matching type overrides the base virtual function. The function named updates does not.

Base virtual functionname: update; type: Type ADerived functionname: updates; type: Type Aname differs
What changes when one detail differs? A derived declaration that misses the exact name-and-type test is not an override under this rule and may be a separate function.

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.

Derived functionmatching name and type; nooverride keywordDerived functionmatching name and type;override stated
What does adding override communicate? The function’s name and type remain the same, while the declaration makes the programmer’s intended relationship explicit.

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 like

    The base function is named update, while the derived function is named updates.

    Why it fails

    The names are not identical, so the exact name-and-type test fails.

    Do this instead

    Compare 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 like

    The base and derived functions have the same name, but one parameter type differs.

    Why it fails

    A difference in a single parameter type is enough to break the match.

    Do this instead

    Check 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 fails

    The keyword states the programmer’s intent; it does not replace the exact name-and-type rule.

    Do this instead

    First verify the exact match, then use override to make the intended relationship clear.

Practice the Match

EASY

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.

More concepts starting with D