Function overloading vs overriding (and name hiding)

Difficulty: Beginner

Question

What is the difference between function overloading and function overriding? What is name hiding and how does the override keyword help?

Answer

Every fresher can recite 'overloading is compile time and overriding is run time', but interviewers follow up with a hiding trap that catches almost everyone. Let's cover both the definitions and the trap.

Overloading means multiple functions with the same name in the same scope but different parameter lists (types, number, or constness of the this pointer). The compiler chooses the function at compile time by matching the arguments, which is called static or early binding. The return type alone cannot distinguish overloads. Think of a receptionist who handles 'book a room', 'book a table' and 'book a cab' differently based on what you ask, even though the action is called 'book'. Overloading is a convenience for the caller and needs no inheritance.

Overriding means a derived class provides a new implementation for a virtual function declared in its base class, with the same signature (and a compatible return type). The function is chosen at run time based on the object's dynamic type, which is dynamic or late binding, using the vtable. It requires inheritance and the virtual keyword.

Now the trap: name hiding. Name lookup in C++ happens before overload resolution and stops at the first scope where the name is found. If Derived declares any function called f, all of Base's overloads of f are hidden in Derived, even if the signatures differ. So if Base has f(int) and f(double), and Derived overrides only f(int), calling d.f(1.5) on a Derived object picks Derived::f(int) and converts 1.5 to 1, instead of using Base::f(double). The fix is a using-declaration inside Derived: 'using Base::f;' brings the base overloads back into scope.

The override keyword (C++11) protects against silent mistakes. If you write void f(int) const in the derived class while the base is void f(int), you have not overridden anything; you have created a new hiding function, and calls via a base pointer go to the base version. Marking the derived function with override turns that mistake into a compile error. The final keyword prevents further overriding.

Additional details worth sharing: default arguments are resolved statically, so overriding functions can have different defaults that the base pointer will not use (a well-known puzzle), and overriding functions may return a covariant type, such as Derived* where Base* was declared. Only virtual functions are overridden; a non-virtual function redefined in a derived class merely hides the base one.

Code examples

Hiding, overriding and using

#include <iostream>

struct B {
    virtual void f(int)   { std::cout << "B::f(int)\n"; }
    void f(double)        { std::cout << "B::f(double)\n"; }
};
struct D : B {
    void f(int) override  { std::cout << "D::f(int)\n"; }   // hides B::f(double)
};
struct E : B {
    using B::f;                                                // bring back overloads
    void f(int) override  { std::cout << "E::f(int)\n"; }
};

int main() {
    D d;
    B& b = d;
    b.f(1);       // virtual dispatch -> D::f(int)
    b.f(1.0);     // B has both overloads -> B::f(double)
    d.f(1.5);     // hiding: only D::f(int) is visible, 1.5 becomes 1
    E e;
    e.f(1.5);     // using-declaration: B::f(double) is visible again
}

Overload resolution through the base reference sees both overloads. Through D, the derived f hides the base overloads; the using-declaration in E fixes that.

Key points

Common follow-up questions

Interview tips

Concepts covered

overloading, overriding, name hiding, override keyword, static vs dynamic binding

More C++ & STL questions