🏛️Go deeper — read the bookObject-Oriented Programming in C++— runnable code & full walkthrough →🪤Go deeper — read the bookC++ Gotchas & Trick Questions— runnable code & full walkthrough →

oops · high

Overloading vs Overriding vs Hiding

Three easily-confused mechanisms. Overloading is several same-named functions with different parameter lists in one scope, resolved by the compiler from the argument types at compile time — nothing to do with inheritance. Overriding is a derived class redefining a base's virtual function with the identical signature (mark it override); a base pointer to a derived object then dispatches to the derived version at runtime through the vtable, which is dynamic polymorphism, and only virtual functions can be overridden. Hiding is the trap: if a derived class declares any member with a given name, it hides every base overload of that name because name lookup stops at the first scope that contains it — so a derived h(int) hides Base::h(double), and a call with a double argument silently converts to the derived h(int); it applies to virtual and non-virtual alike and is fixed by bringing the base names back with a using-declaration such as 'using Base::h;'.

🔑 Key line

Overloading = same name, different params, resolved at COMPILE time. Overriding = redefine a base VIRTUAL with the same signature -> RUNTIME dispatch via the vtable. Hiding = any derived member of a name hides ALL base overloads of that name (lookup stops at the first scope); bring them back with 'using Base::name;'.

The code

struct Base {
virtual void f(int); // virtual -> can be OVERRIDDEN
void g(int); // non-virtual
void h(int); void h(double); // two OVERLOADS (same name)
};
struct Der : Base {
void f(int) override; // OVERRIDE: same signature, runtime dispatch
void h(int); // HIDES both Base::h overloads
};
Base* p = new Der;
p->f(0); // -> Der::f (virtual: chosen at RUNTIME)
Der d; d.h(2.0); // -> Der::h(int)! Base::h(double) is HIDDEN
// fix: 'using Base::h;' in Der brings the base overloads back

What this lesson walks through

  1. 01Overloading — same name, different parameters (compile time)
  2. 02Overriding — redefine a virtual with the same signature (runtime)
  3. 03Hiding — a derived name hides ALL base overloads of that name

Overloading is several functions that share a name but differ in their parameter lists, in the SAME scope. Base has h(int) and h(double); the compiler picks which one to call from the argument types, entirely at COMPILE time. Overloading has nothing to do with virtual or inheritance — it's just name + signature resolution.

See it animated — step by step, at your own pace

Unlock the full interactive walkthrough of Overloading vs Overriding vs Hiding and 100+ animated C++ interview lessons.

← Previous
Abstract Classes, Pure Virtual & Interfaces
Next →
Virtual Destructor