C++ में Function Overloading
In this page:
return_type function_name(type1 param);
return_type function_name(type2 param);
return_type function_name(type1 param1, type2 param2);
Parameter Type से Overloading
C++ कई functions को exact same नाम share करने देता है जब तक उनके parameter types अलग हों, इसलिए void print(int x) और void print(double x) साथ exist कर सकते हैं — compiler call site पर आपके pass किए argument type के आधार पर automatically सही एक चुनता है।
इसे function overloading कहते हैं।
उदाहरण: Overloading by Parameter Type
// Include std::cout and std::cin
#include <iostream>
void print(int x) { std::cout << "int: " << x << std::endl; }
void print(double x) { std::cout << "double: " << x << std::endl; }
// Program execution starts in main()
int main() {
print(5);
print(5.5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameters की संख्या से Overloading
Functions को इस आधार पर भी overload किया जा सकता है कि वे कितने parameters लेते हैं, इसलिए void log(string msg) और void log(string msg, string level) दोनों same नाम के तहत exist कर सकते हैं — compiler उन्हें purely हर call पर supplied arguments count करके अलग करता है।
उदाहरण: Overloading by Number of Parameters
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void log(std::string msg) { std::cout << msg << std::endl; }
void log(std::string msg, std::string level) { std::cout << "[" << level << "] " << msg << std::endl; }
// Program execution starts in main()
int main() {
log("Started");
log("Started", "INFO");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameters के Order से Overloading
यहाँ तक कि जब दो functions parameter types का same set share करें, उन types को reorder करना — जैसे void combine(int a, double b) बनाम void combine(double a, int b) — दो genuinely distinct, valid overloads बनाता है, क्योंकि compiler फिर भी उन्हें अलग parameter signatures की तरह देखता है।
उदाहरण: Overloading by Order of Parameters
// Include std::cout and std::cin
#include <iostream>
void combine(int a, double b) { std::cout << "int,double: " << a << "," << b << std::endl; }
void combine(double a, int b) { std::cout << "double,int: " << a << "," << b << std::endl; }
// Program execution starts in main()
int main() {
combine(1, 2.5);
combine(2.5, 1);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overloading में Best Practices
Same नाम वाले overloaded functions को genuinely related, conceptually similar tasks perform करने चाहिए — उदाहरण के लिए, अलग shapes के लिए कई area() overloads — बस एक convenient नाम share करने वाले unrelated operations होने की बजाय।
Unrelated behavior के लिए एक नाम reuse करना code को एक नज़र में समझना मुश्किल बनाता है।
उदाहरण: Best Practices in Overloading
// Include std::cout and std::cin
#include <iostream>
double area(double side) { return side * side; }
double area(double length, double width) { return length * width; }
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << area(4.0) << std::endl;
// Print to the console with cout
std::cout << area(3.0, 5.0) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Overload Ambiguity Resolve करना
Compiler कभी-कभी decide नहीं कर पाता कि आपका मतलब कौन सा overload था, आमतौर पर जब एक argument का type दो candidates के बीच ambiguous हो — एक literal 0 pass करना जहाँ एक int और एक pointer overload दोनों exist करें एक classic example है।
जब ऐसा हो, आपको एक चुपचाप गलत choice की बजाय एक compile-time 'ambiguous call' error मिलेगी, जो कम से कम problem को तुरंत surface करती है।
उदाहरण: Resolving Overload Ambiguity
#include <iostream>
void show(int x) { std::cout << "int: " << x << std::endl; }
void show(double x) { std::cout << "double: " << x << std::endl; }
int main() {
show(5); // clearly int
show(5.0); // clearly double
// show('a'); // ambiguous-prone: char can convert to either
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- सिर्फ return type से overload करना, जो allowed नहीं है।
- एक overloaded function को एक argument से call करना जो दो versions से equally match करे, एक ambiguity error cause करते हुए।
- यह उम्मीद करना कि
intऔरconst intparameters अलग overloads हैं, जब top-level const ignore हो जाता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions