Function Overloading (Polymorphism)
Using the same function name to do related things in different ways.
Function Overloading (Polymorphism)
Function Overloading C++ की एक important feature है जिसमें एक ही class में एक ही नाम के multiple functions define किए जा सकते हैं, लेकिन उनके parameters अलग होने चाहिए। Function के parameters की संख्या या उनके data types अलग हो सकते हैं।
Function Overloading Polymorphism का एक उदाहरण है। इसे विशेष रूप से Compile-Time Polymorphism या Static Polymorphism कहा जाता है क्योंकि compiler यह निर्धारित करता है कि function call के आधार पर कौन-सा function execute किया जाएगा।
What is Function Overloading?
जब एक ही नाम के दो या दो से अधिक functions को अलग-अलग parameter lists के साथ define किया जाता है, तो इसे Function Overloading कहते हैं।
उदाहरण के लिए, हम add() नाम के functions बना सकते हैं जो अलग-अलग प्रकार के data पर काम करें।
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
float add(float a, float b)
{
return a + b;
}
};
int main()
{
Calculator c;
cout << c.add(10, 20) << endl;
cout << c.add(2.5f, 3.5f);
return 0;
}
30
6
यहाँ दोनों functions का नाम add() है, लेकिन पहले function में int parameters और दूसरे में float parameters हैं। Compiler arguments के आधार पर सही function को select करता है।
Why is Function Overloading called Polymorphism?
Polymorphism का अर्थ है many forms अर्थात एक ही नाम या interface का अलग-अलग forms में काम करना। Function Overloading में एक ही function name अलग-अलग parameter lists के साथ अलग situations में काम करता है। इसलिए यह polymorphism का एक form है।
Compiler function call को देखकर निर्धारित करता है कि कौन-सा overloaded function execute होगा।
Rules of Function Overloading
Function Overloading के लिए overloaded functions की parameter list में difference होना आवश्यक है। Difference निम्न प्रकार से हो सकता है:
- Parameters की संख्या अलग हो सकती है।
- Parameters के data types अलग हो सकते हैं।
- Parameters का order अलग हो सकता है, यदि उनके data types अलग हों।
Overloading by Number of Parameters
जब functions के parameters की संख्या अलग होती है, तब function overloading किया जा सकता है।
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
};
int main()
{
Calculator c;
cout << c.add(10, 20) << endl;
cout << c.add(10, 20, 30);
return 0;
}
30
60
यहाँ पहले add() function में दो parameters हैं जबकि दूसरे add() function में तीन parameters हैं।
Overloading by Data Type
Functions में parameters की संख्या समान हो सकती है, लेकिन उनके data types अलग होने पर भी function overloading किया जा सकता है।
#include <iostream>
using namespace std;
class Display
{
public:
void show(int x)
{
cout << "Integer: " << x << endl;
}
void show(double x)
{
cout << "Double: " << x;
}
};
int main()
{
Display d;
d.show(10);
d.show(5.5);
return 0;
}
Integer: 10
Double: 5.5
यहाँ दोनों functions में एक parameter है, लेकिन पहले का parameter int और दूसरे का double है।
Overloading by Order of Parameters
यदि parameters के data types अलग हैं, तो उनके order को बदलकर भी functions overload किए जा सकते हैं।
#include <iostream>
using namespace std;
class Test
{
public:
void display(int a, double b)
{
cout << "int, double" << endl;
}
void display(double a, int b)
{
cout << "double, int";
}
};
int main()
{
Test t;
t.display(10, 5.5);
t.display(5.5, 10);
return 0;
}
int, double
double, int
यहाँ दोनों functions में दो parameters हैं, लेकिन parameters का order अलग है।
Function Overloading with Different Operations
Function Overloading का उपयोग एक ही नाम के function से अलग-अलग प्रकार के operations करने के लिए भी किया जा सकता है।
#include <iostream>
using namespace std;
class Area
{
public:
int calculate(int side)
{
return side * side;
}
int calculate(int length, int breadth)
{
return length * breadth;
}
};
int main()
{
Area a;
cout << "Square Area: "
<< a.calculate(5) << endl;
cout << "Rectangle Area: "
<< a.calculate(10, 5);
return 0;
}
Square Area: 25
Rectangle Area: 50
यहाँ calculate() function एक parameter मिलने पर square का area और दो parameters मिलने पर rectangle का area calculate करता है।
Function Overloading and Return Type
केवल return type को बदलकर functions को overload नहीं किया जा सकता। यदि functions के parameters बिल्कुल समान हैं और केवल return type अलग है, तो यह valid function overloading नहीं है।
उदाहरण:
int add(int a, int b)
{
return a + b;
}
float add(int a, int b)
{
return a + b;
}
Function Signature
Function Overloading को समझने के लिए function signature की concept महत्वपूर्ण है। Function signature में मुख्य रूप से function का नाम और parameter list शामिल होती है। Overloaded functions की parameter lists अलग होनी चाहिए।
| Function | Parameter List | Overloading |
|---|---|---|
| add(int, int) | दो int parameters | Valid |
| add(int, int, int) | तीन int parameters | Valid |
| add(double, double) | दो double parameters | Valid |
| add(int, int) with different return type | समान parameter list | Invalid |
How Compiler Resolves an Overloaded Function
जब overloaded function को call किया जाता है, तो compiler दिए गए arguments को देखता है और matching parameter list वाले function को select करता है।
add(10, 20);
add(10, 20, 30);
add(2.5, 3.5);
Compiler प्रत्येक function call के arguments की संख्या और data types को देखकर appropriate overloaded function को select करता है।
Function Overloading vs Function Overriding
Function Overloading और Function Overriding अलग concepts हैं। Function Overloading में सामान्यतः एक ही class में same name के functions की parameter lists अलग होती हैं, जबकि overriding inheritance के context में होती है जहाँ derived class base class के member function का अपना implementation प्रदान करती है।
| Function Overloading | Function Overriding |
|---|---|
| Same function name के multiple functions होते हैं। | Derived class base class के function का नया implementation देती है। |
| Parameter list अलग होती है। | Overriding में function signature सामान्यतः compatible/same होता है। |
| Compile-time polymorphism का उदाहरण है। | Runtime polymorphism से संबंधित है। |
| Inheritance आवश्यक नहीं है। | Inheritance की आवश्यकता होती है। |
Advantages of Function Overloading
- एक ही प्रकार के कार्य के लिए meaningful एक ही function name रखा जा सकता है।
- Program की readability बढ़ती है।
- Code को समझना और maintain करना आसान होता है।
- Different data types या different number of arguments के साथ same operation किया जा सकता है।
- Compile-time polymorphism को implement करने में सहायता मिलती है।
- Function names की अनावश्यक संख्या कम होती है।
Limitations of Function Overloading
- केवल return type बदलने से function overload नहीं किया जा सकता।
- Overloaded functions की parameter list में meaningful difference होना चाहिए।
- कुछ situations में implicit type conversion के कारण compiler को function selection में ambiguity हो सकती है।
Example: Function Overloading with Multiple Data Types
#include <iostream>
using namespace std;
class Calculator
{
public:
int multiply(int a, int b)
{
return a * b;
}
float multiply(float a, float b)
{
return a * b;
}
int multiply(int a, int b, int c)
{
return a * b * c;
}
};
int main()
{
Calculator c;
cout << c.multiply(4, 5) << endl;
cout << c.multiply(2.5f, 4.0f) << endl;
cout << c.multiply(2, 3, 4);
return 0;
}
20
10
24
यहाँ multiply() function के तीन overloaded versions हैं। Compiler arguments के आधार पर appropriate version को select करता है।
Important Points
- Function Overloading में same name के multiple functions define किए जाते हैं।
- Overloaded functions की parameter lists अलग होनी चाहिए।
- Parameters की संख्या अलग होने पर function overload किया जा सकता है।
- Parameters के data types अलग होने पर भी function overload किया जा सकता है।
- Parameters का order अलग होने पर भी function overload किया जा सकता है, यदि types अलग हों।
- केवल return type बदलने से function overloading नहीं होता।
- Function Overloading compile-time polymorphism का उदाहरण है।
- Compiler function call के arguments के आधार पर appropriate function select करता है।
- Function Overloading के लिए inheritance आवश्यक नहीं है।
Board Focus
Function Overloading → Same name + Different parameter list
Polymorphism → Many forms
Function Overloading → Compile-Time Polymorphism
Parameters → Number, Type या Order में difference हो सकता है
Return Type → केवल return type बदलने से overloading नहीं होती
Compiler → Arguments के आधार पर function select करता है
Board Important Questions
Very Short Answer Questions
Q1. Function Overloading क्या है?
Answer: एक ही नाम के multiple functions को अलग-अलग parameter lists के साथ define करना Function Overloading कहलाता है।
Q2. Function Overloading किस प्रकार के polymorphism का उदाहरण है?
Answer: Function Overloading compile-time polymorphism का उदाहरण है।
Q3. क्या केवल return type के आधार पर function overload किया जा सकता है?
Answer: नहीं, केवल return type बदलने से function overload नहीं किया जा सकता।
Q4. Function Overloading में parameters की संख्या अलग हो सकती है?
Answer: हाँ, parameters की संख्या अलग होने पर functions overload किए जा सकते हैं।
Q5. क्या parameters के data types अलग होने पर function overload किया जा सकता है?
Answer: हाँ, parameters के data types अलग होने पर function overload किया जा सकता है।
Q6. Polymorphism का क्या अर्थ है?
Answer: Polymorphism का अर्थ many forms है, अर्थात एक interface या function का अलग-अलग forms में काम करना।
Short Answer Questions
Q7. Function Overloading को उदाहरण सहित समझाइए।
Answer: जब एक ही नाम के multiple functions अलग-अलग parameter lists के साथ define किए जाते हैं, तो इसे Function Overloading कहते हैं। उदाहरण:
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
यहाँ दोनों functions का नाम add() है, लेकिन parameters की संख्या अलग है।
Q8. Function Overloading में किन आधारों पर functions को overload किया जा सकता है?
Answer: Functions को parameters की संख्या, parameters के data types तथा parameters के order के आधार पर overload किया जा सकता है, बशर्ते parameter lists अलग हों।
Q9. केवल return type के आधार पर Function Overloading क्यों नहीं की जा सकती?
Answer: Function call में compiler function selection के लिए return type का उपयोग नहीं करता। यदि parameter list समान हो और केवल return type अलग हो, तो compiler यह निर्धारित नहीं कर सकता कि कौन-सा function call करना है। इसलिए केवल return type के आधार पर overloading valid नहीं है।
Q10. Compile-Time Polymorphism क्या है?
Answer: जब compiler program compile करते समय यह निर्धारित करता है कि किसी operation या function call का कौन-सा form execute होगा, तो इसे compile-time polymorphism कहते हैं। Function Overloading इसका प्रमुख उदाहरण है।
Q11. Function Overloading के दो advantages लिखिए।
Answer: Function Overloading से program की readability बढ़ती है और एक ही प्रकार के operation के लिए अलग-अलग function names रखने की आवश्यकता कम होती है। यह compile-time polymorphism को भी support करता है।
Long Answer Questions
Q12. Function Overloading को C++ program की सहायता से समझाइए।
Answer: Function Overloading C++ की सुविधा है जिसमें same name के multiple functions को different parameter lists के साथ define किया जाता है। Compiler function call के arguments के आधार पर appropriate function को select करता है। उदाहरण:
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
};
int main()
{
Calculator c;
cout << c.add(10, 20) << endl;
cout << c.add(10, 20, 30);
return 0;
}
30
60
यहाँ add() function दो बार define किया गया है। पहला version दो arguments लेता है और दूसरा तीन arguments लेता है। इसलिए compiler function call के अनुसार appropriate version को execute करता है।
Q13. Function Overloading और Function Overriding में अंतर बताइए।
| Function Overloading | Function Overriding |
|---|---|
| Same name के multiple functions होते हैं। | Derived class base class के function को redefine करती है। |
| Parameter list अलग होती है। | Function signature compatible/same होता है। |
| Compile-time polymorphism से संबंधित है। | Runtime polymorphism से संबंधित है। |
| Inheritance आवश्यक नहीं है। | Inheritance आवश्यक है। |
Q14. निम्नलिखित program का output लिखिए:
#include <iostream>
using namespace std;
class Test
{
public:
void show(int x)
{
cout << "Integer";
}
void show(double x)
{
cout << "Double";
}
};
int main()
{
Test t;
t.show(10);
cout << endl;
t.show(5.5);
return 0;
}
Integer
Double
Q15. Function Overloading को polymorphism क्यों कहा जाता है?
Answer: Function Overloading में एक ही function name अलग-अलग parameter lists के साथ multiple forms में काम करता है। इसलिए यह polymorphism अर्थात many forms का उदाहरण है। इसका selection compile time पर होता है, इसलिए इसे compile-time polymorphism कहा जाता है।
Quick Revision
- Function Overloading: Same name के multiple functions
- Parameter List: Overloaded functions में अलग होनी चाहिए
- Number of Parameters: अलग हो सकता है
- Data Type: अलग हो सकता है
- Parameter Order: अलग हो सकता है
- Return Type: केवल return type बदलने से overloading नहीं होती
- Polymorphism: Many forms
- Function Overloading: Compile-Time Polymorphism
Practice Questions
- Function Overloading क्या है?
- Function Overloading किस प्रकार के polymorphism का उदाहरण है?
- Function Overloading के लिए आवश्यक conditions लिखिए।
- Parameters की संख्या के आधार पर function overloading का उदाहरण दीजिए।
- Data type के आधार पर function overloading का उदाहरण दीजिए।
- Parameter order के आधार पर function overloading समझाइए।
- क्या केवल return type बदलकर functions को overload किया जा सकता है? कारण बताइए।
- Compile-time polymorphism क्या है?
- Function Overloading और Function Overriding में अंतर बताइए।
- Function Overloading के advantages लिखिए।
- दो overloaded functions का उपयोग करके addition का C++ program लिखिए।
- एक C++ program लिखिए जिसमें same function name का उपयोग करके square और rectangle का area calculate किया जाए।
- Function Overloading का उपयोग करके integer और floating-point numbers को multiply करने का program लिखिए।
- Function Overloading को demonstrate करने वाला C++ program लिखिए और उसका output बताइए।
- समझाइए कि Function Overloading को polymorphism क्यों कहा जाता है।