Function Overloading in C++ with Examples and Rules
Function overloading lets you write many functions with the same name but different parameters — the compiler picks the right one. Examples, rules and the return-type trap.
The receptionist analogy
Basic example — one name, three jobs
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; } // more params
double add(double a, double b) { return a + b; } // diff types
int main() {
cout << add(2, 3) << endl; // matches (int, int)
cout << add(2, 3, 4) << endl; // matches (int, int, int)
cout << add(2.5, 3.5) << endl; // matches (double, double)
return 0;
}
The decision happens at compile time: the compiler reads the arguments, finds the matching version, and hard-wires the call. That is why overloading is called compile-time (static) polymorphism.
What makes two functions "different enough"?
| Change | Valid overload? | Example |
|---|---|---|
| Number of parameters | ✅ Yes | add(a,b) vs add(a,b,c) |
| Type of parameters | ✅ Yes | add(int,int) vs add(double,double) |
| Order of types | ✅ Yes | f(int,char) vs f(char,int) |
| Only return type | ❌ NO — compile error | int f(int) vs double f(int) |
| Only parameter names | ❌ NO — same function | f(int a) vs f(int b) |
f(5); (result ignored) gives no clue which return type was wanted. Ambiguity = forbidden. This "why" is the follow-up interviewers love.The ambiguity error you will definitely meet
void show(int x) { cout << "int\n"; }
void show(float x) { cout << "float\n"; }
show(5); // OK: exactly int
show(5.5); // ERROR on many compilers: 5.5 is a DOUBLE literal -
// converting to int or to float are equally good matches!
show(5.5f); // OK: the f suffix makes it exactly float
Fix: use exact-type literals (5.5f) or add a double overload. Understanding this error message once saves hours later.
Real-world use: constructors and print functions
class Student {
public:
Student() { } // no info yet
Student(string n) { } // name only
Student(string n, int marks) { } // full info
}; // constructor overloading: create objects in whichever way suits
void print(int n) { cout << "Number: " << n; }
void print(string s) { cout << "Text: " << s; }
void print(bool b) { cout << (b ? "Yes" : "No"); }
This is why cout itself can print an int, a string, or a double with the same << — the entire iostream library is built on overloading.
Receptionist वाली analogy
Basic example — एक नाम, तीन काम
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; } // ज़्यादा params
double add(double a, double b) { return a + b; } // अलग types
int main() {
cout << add(2, 3) << endl; // (int, int) से match
cout << add(2, 3, 4) << endl; // (int, int, int) से match
cout << add(2.5, 3.5) << endl; // (double, double) से match
return 0;
}
Decision compile time पर होता है: compiler arguments पढ़ता है, matching version ढूंढता है, और call fix कर देता है. इसीलिए overloading को compile-time (static) polymorphism कहते हैं.
दो functions "काफी अलग" कब माने जाते हैं?
| बदलाव | Valid overload? | Example |
|---|---|---|
| Parameters की संख्या | ✅ हां | add(a,b) vs add(a,b,c) |
| Parameters के types | ✅ हां | add(int,int) vs add(double,double) |
| Types का order | ✅ हां | f(int,char) vs f(char,int) |
| सिर्फ return type | ❌ नहीं — compile error | int f(int) vs double f(int) |
| सिर्फ parameter names | ❌ नहीं — same function | f(int a) vs f(int b) |
f(5); जैसी call (result ignore) से कोई clue नहीं मिलता कि कौन-सा return type चाहिए था. Ambiguity = मना. यही "क्यों" वाला follow-up interviewers को पसंद है.वह ambiguity error जो आपको ज़रूर मिलेगा
void show(int x) { cout << "int\n"; }
void show(float x) { cout << "float\n"; }
show(5); // OK: exactly int
show(5.5); // कई compilers पर ERROR: 5.5 DOUBLE literal है -
// int में बदलना या float में - दोनों बराबर अच्छे matches!
show(5.5f); // OK: f suffix इसे exactly float बना देता है
Fix: exact-type literals use करें (5.5f) या double overload जोड़ें. यह error एक बार समझ ली तो बाद में घंटों बचेंगे.
Real-world use: constructors और print functions
class Student {
public:
Student() { } // अभी कोई info नहीं
Student(string n) { } // सिर्फ name
Student(string n, int marks) { } // पूरी info
}; // constructor overloading: जिस तरह सुविधाजनक हो object बनाओ
void print(int n) { cout << "Number: " << n; }
void print(string s) { cout << "Text: " << s; }
void print(bool b) { cout << (b ? "Yes" : "No"); }
इसीलिए cout खुद int, string या double को same << से print कर पाता है — पूरी iostream library overloading पर ही बनी है.
Frequently Asked Questions
What is function overloading in C++?
Function overloading lets multiple functions share the same name with different parameter lists; the compiler selects the correct version at compile time based on the arguments — this is compile-time polymorphism.
Can functions be overloaded by return type only?
No, it is a compile error — the compiler resolves calls from arguments alone, and a call that ignores the result gives no clue about which return type was intended.
What is the difference between function overloading and overriding?
Overloading is same name with different parameters in the same scope, resolved at compile time; overriding is a derived class redefining a virtual function with the same signature, resolved at runtime.