← Back to C++ Course | Chapter 5: Functions | Lesson 7 of 13

C++ में Function Overloading

Function overloading का मतलब है कई functions को same नाम लेकिन अलग inputs देना, जैसे draw word एक circle या एक square के लिए काम करना। C++ आपके दिए के आधार पर सही एक चुनता है।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. सिर्फ return type से overload करना, जो allowed नहीं है।
  2. एक overloaded function को एक argument से call करना जो दो versions से equally match करे, एक ambiguity error cause करते हुए।
  3. यह उम्मीद करना कि int और const int parameters अलग overloads हैं, जब top-level const ignore हो जाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.