Theory
Two words designed to confuse you
Overloading. Overriding. Two words that share nine letters, live in the same unit, and appear together in one exam question almost every year: "Differentiate function overloading and function overriding."
Mix them up in a viva and the follow-up questions get uncomfortable fast.
Here is the good news: you have already used both without naming them. The till's two bill() versions? Overloading. Student rewriting Person's discount()? Overriding.
Theory
Same counter vs family recipe
Overloading is one counter offering chai, chai(large), chai(large, extra sugar): one name, and the cashier picks the recipe by looking at what you asked for.
Overriding is the family's franchise branch taking the parent's standard thali recipe and replacing it with its own version, same dish name, same order slip, different kitchen. Which one you get depends on which branch serves you.
Theory
The two, formally
Function overloading: two or more functions in the same scope share a name but differ in their parameter list (number or types of parameters). The compiler selects by matching arguments.
double bill(double amt);
double bill(double amt, double disc);
Function overriding: a derived class redefines a base-class function using the same name AND same signature. The child's version replaces the parent's for that child.
class Person { virtual double discount(); };
class Student : public Person { double discount(); };
At a glance
Overloading vs overriding, the exam table
| Aspect | Overloading | Overriding |
|---|---|---|
| Signature | Must DIFFER (params) | Must MATCH exactly |
| Where | Same scope / class | Base class vs derived class |
| Inheritance needed | No | Yes |
| Resolved | Compile time | Run time (with virtual) |
| Polymorphism family | Compile-time (static) | Run-time (dynamic) |
Quiz
In one class, a student writes: int calc(int a); and double calc(int a); expecting an overload. What happens?
- Compile error: a different return type ALONE cannot overload a function
- It works: return types make the signatures different
- It works, but only the int version is ever called
- Runtime error when calc is first called
Show the answer
Compile error: a different return type ALONE cannot overload a function
Overload resolution looks only at the parameter list. Both functions take (int), so to the compiler they are the same function declared twice with clashing return types: an error. This is the most-tested overloading fact in existence. To overload, change the parameters: calc(int) and calc(double) would be fine.
Think first
Classify these four
Label each as overloading or overriding before tapping:
1. Item() and Item(char n[], double p) in one class
2. Staff redefining Person's show() with the same signature
3. print(int) and print(char) in one file
4. Student redefining discount() from Person
Show the answer
1. Overloading (constructors with different parameter lists, the everyday application of overloading).
2. Overriding (base vs derived, same signature).
3. Overloading (same scope, different parameter types).
4. Overriding (the card family's whole point).
Quick test to reuse: different params in one place = overloading; same signature across a parent-child pair = overriding.
Watch out
The three mark-losers
1. Claiming overloading needs inheritance: it does not; it lives inside one scope.
2. Claiming return type alone can overload: it cannot; parameters must differ.
3. Forgetting virtual: overriding without it still compiles, but a base-class pointer will call the base version (early binding). Overriding only becomes run-time polymorphism when the base function is declared virtual.
Theory
Where each earns its keep
Overloading in the wild: constructors (default + parameterized + copy, one name, three forms) and operator overloading (total + item making sense for your own types).
Overriding in the wild: every framework you will ever use hands you a base class and expects you to override its hooks: Android's onCreate(), a game engine's update(). The card family's discount() is the same pattern in miniature.
Summary
Key takeaways
- Overloading: same name, different parameter list, same scope, compile-time choice.
- Overriding: same name, same signature, base vs derived, run-time choice when virtual.
- Return type alone can NEVER overload a function.
- Overloading needs no inheritance; overriding cannot exist without it.
- Applications: overloaded constructors and operators; overridden hooks like discount().
- Memory hook: one counter, many recipes vs franchise rewrites the family dish.