Function Overloading
What is function overloading?
Function overloading lets you give several functions the same name, as long as their parameter lists differ — a different number of parameters, or different types. C++ figures out which one you meant from the arguments you pass.
Why overload?
Without overloading you would need clumsy names for the same idea: addInts, addDoubles, addThree. Overloading lets one clear name — add — serve them all, so callers do not have to remember variants.
A working example
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
int main() {
cout << add(2, 3) << "\n"; // int version
cout << add(2.5, 3.5) << "\n"; // double version
cout << add(1, 2, 3) << "\n"; // three-arg version
return 0;
}5
6
6
How the compiler chooses
The compiler matches your call to the version whose parameters best fit the argument types and count — a process called overload resolution. In the example, add(2, 3) matches the two-int version, add(2.5, 3.5) matches the two-double version, and so on.
What counts as different
| Difference | Valid overload? |
|---|---|
| Different number of parameters | Yes |
| Different parameter types | Yes |
| Different order of types | Yes (e.g. (int,double) vs (double,int)) |
| Only different return type | No — not allowed |
| Only different parameter names | No — names do not count |
Common mistakes
- Trying to overload by return type only — not allowed.
- Writing two versions that are ambiguous, so no single best match exists.
- Thinking different parameter names make an overload — only types/count matter.
- Confusing overloading (compile-time) with overriding (run-time, via virtual functions).
Write an overloaded area function: one for a square (one side), one for a rectangle (length and width), and one for a circle (radius, returning a double). Call each from main.
Summary
- Overloading = several functions with the same name, different parameters.
- The compiler picks the version that best matches your arguments.
- Overloads must differ in parameter count, types or order.
- You cannot overload by return type or parameter names alone.
- Overloading is compile-time; overriding is a separate run-time concept.
Function overloading क्या है?
Function overloading आपको कई functions को एक ही नाम देने देता है, बशर्ते उनकी parameter सूचियाँ भिन्न हों — अलग संख्या में parameters, या अलग types। C++ आपके भेजे arguments से पता कर लेता है कि आपका मतलब कौन-सा था।
Overload क्यों करें?
Overloading के बिना आपको एक ही विचार के लिए भद्दे नाम चाहिए: addInts, addDoubles, addThree। Overloading एक साफ़ नाम — add — को इन सबकी सेवा करने देता है, तो callers को variants याद नहीं रखने पड़ते।
एक चलता उदाहरण
#include <iostream>
using namespace std;
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
int main() {
cout << add(2, 3) << "\n"; // int version
cout << add(2.5, 3.5) << "\n"; // double version
cout << add(1, 2, 3) << "\n"; // teen-arg version
return 0;
}5
6
6
Compiler कैसे चुनता है
Compiler आपके call को उस version से मिलाता है जिसके parameters argument types और संख्या से सबसे अच्छे फिट हों — इस प्रक्रिया को overload resolution कहते हैं। उदाहरण में, add(2, 3) दो-int version से मेल खाता है, add(2.5, 3.5) दो-double version से, और ऐसे ही।
अलग क्या माना जाता है
| अंतर | मान्य overload? |
|---|---|
| Parameters की अलग संख्या | हाँ |
| अलग parameter types | हाँ |
| Types का अलग क्रम | हाँ (जैसे (int,double) बनाम (double,int)) |
| केवल अलग return type | नहीं — अनुमति नहीं |
| केवल अलग parameter नाम | नहीं — नाम मायने नहीं रखते |
आम गलतियाँ
- केवल return type से overload करने की कोशिश — अनुमति नहीं।
- दो ऐसे versions लिखना जो ambiguous हों, तो कोई एकल सर्वोत्तम मेल न हो।
- यह सोचना कि अलग parameter नाम overload बनाते हैं — केवल types/संख्या मायने रखते हैं।
- Overloading (compile-time) को overriding (run-time, virtual functions से) से भ्रमित करना।
एक overloaded area function लिखें: एक square के लिए (एक भुजा), एक rectangle के लिए (लंबाई और चौड़ाई), और एक circle के लिए (त्रिज्या, double लौटाते हुए)। हर एक को main से call करें।
सारांश
- Overloading = एक ही नाम वाले कई functions, अलग parameters।
- Compiler वह version चुनता है जो आपके arguments से सबसे अच्छा मेल खाए।
- Overloads को parameter संख्या, types या क्रम में भिन्न होना चाहिए।
- आप केवल return type या parameter नामों से overload नहीं कर सकते।
- Overloading compile-time है; overriding एक अलग run-time अवधारणा है।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में function overloading क्या है?
Function overloading क्यों उपयोगी है?
addInt, addDouble और addThree जैसे नाम गढ़ने के बजाय एक सहज नाम साझा करने देता है। इससे code साफ़ और पढ़ने में आसान होता है, क्योंकि add(...) काम करता है चाहे आप दो ints, दो doubles या तीन numbers भेजें।