C++ में Function Overloading Examples और Rules के साथ
Function overloading से same नाम लेकिन अलग parameters वाले कई functions लिख सकते हैं — compiler सही वाला चुनता है. Examples, rules और 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
C++ में function overloading क्या है?
Function overloading से कई functions अलग parameter lists के साथ same नाम share करते हैं; compiler arguments देखकर compile time पर सही version चुनता है — यही compile-time polymorphism है.
क्या सिर्फ return type से functions overload हो सकते हैं?
नहीं, यह compile error है — compiler calls को सिर्फ arguments से resolve करता है, और result ignore करने वाली call से पता ही नहीं चलता कौन-सा return type चाहिए था.
Function overloading और overriding में क्या अंतर है?
Overloading same scope में अलग parameters के साथ same नाम है, compile time पर resolve; overriding derived class द्वारा same signature से virtual function redefine करना है, runtime पर resolve.