What is Function overloading in Hindi
C++ में Function overloading, कई functions के लिए एक ही नाम Use करने का तरीका है। इसका मतलब है कि आपके पास एक नाम के कई functions है, लेकिन वे आपके द्वारा दिए गए Information के आधार पर अलग अलग कार्य करते हैं।C++ में Function overloading एक शक्तिशाली features है जो एक नाम से लेकिन विभिन्न Parameters के साथ कई functions को Define करने की अनुमति देता है।
इसका मतलब यह है कि हमारे पास एक ही नाम से कई functions हो सकते हैं जो उन्हे प्राप्त होने वाले Parameter types या numbers के आधार पर अलग-अलग कार्य कर सकते हैं।
Function overloading में "function" का नाम समान होना चाहिए और उनके Parameters या Arguments अलग अलग होना चाहिए।
जब आप किसी function को overloaded नाम से Call करते है, तो Compiler दिए गए Arguments के आधार पर यह निर्धारित करता है
कि function के किस Version को Execute करना है।
Function overloading को C++ में Polymorphism का ही एक उदाहरण माना जा सकता है।
Syntax for Function overloading in C++ in Hindi
C++ में Function Overloading के तरीके
C++ में Function Overloading के कई प्रकार या तरीके है जिसमे से मुख्य प्रकार निम्न है1) Parameters या Arguments की विभिन्न संख्या के आधार पर
2)Parameters या Arguments के प्रकार के आधार पर
Parameters या Arguments की विभिन्न संख्या के आधार पर
C++ में Function overloading, आपको एक नाम तथा विभिन्न Parameters के साथ कई functions को Define करने की अनुमति देते हैं।
Function को overload करने का एक तरीका arguments की संख्या के आधार पर है।
#include<iostream>
using namespace std;
//Function to add two numbers
int add(int x, int y){
return x+y;
}
//Now overloaded function to add three numbers
int add(int x, int y,int z){
return x+y+z;
}
int main(){
//Calling function to add two numbers
cout<<"Addition of two numbers: "<<add(4, 3)<<endl;
//Now calling overloaded function to add three numbers
cout<<"Addition of three numbers: "<<add(7, 5, 9)<<endl;
return 0;
}
Output:
Addition of two numbers: 7
Addition of three numbers: 21
Explanation:
1) दो numbers को Add करने के लिए function:
int add(int x, int y) {
return x + y;
}
यह function दो Integers, x और y लेता है और उसका sum return करता है।
2) तीन numbers को Add करने के लिए Overloaded function :
int add(int x, int y, int z) {
return x + y + z;
}
यह function तीन integers x, y और z लेता है और उनका sum return करता है।
इसका एक ही नाम `add` है परंतु उनके Parameters की संख्या अलग है, ताकि Compiler उनके बीच अंतर कर सकें।
3) main में इन functions को call करना:
int main() {
cout << "Addition of two numbers: " << add(4, 3) << endl;
cout << "Addition of three numbers: " << add(7, 5, 7) << endl;
return 0;
}
`add(4, 3)` के माध्यम से `add` function को Call किया जाता है जो दो parameters लेता है
`add(7, 5, 7)` के माध्यम से overloaded `add` function को Call किया जाता है जो तीन Parameters लेता है।
Parameters या Arguments के प्रकार के आधार पर
इसमें Parameters के विभिन्न प्रकार के आधार पर Function को overloaded किया जाता है#include<iostream>
using namespace std;
int add(int x, int y) {
return x + y;
}
float add(int x, float y) {
return x + y;
}
float add(float x, float y) {
return x + y;
}
int main() {
cout << "Sum of 2 integer numbers: " << add(20, 30) << endl;
cout << "Sum of 1 Integer and 1 Real number: " << add(10, 35.54f) << endl;
cout << "Sum of 2 Real numbers: " << add(20.43f, 35.54f) << endl;
return 0;
}
Sum of 2 integer numbers: 50
Sum of 1 Integer and 1 Real number: 45.54
Sum of 2 Real numbers: 55.97
Explaination:
ऊपर का C++ program में तीन `add` functions को विभिन्न parameter के साथ Define किया गया है।
यह दो integers को Add करता है।
2) float add(int x, float y):
यह function एक integer और एक float को Add करता है।
3) `float add(float x, float y)`:
यह दो floats को Add करता है।
`main` function, में इन overloaded `add` functions को विभिन्न Argument types के साथ Call किया जाता है।
#include<iostream>
#include<string>
using namespace std;
void DisplayStr(string s) {
cout << "Displaying string: " << s <<endl;
}
void DisplayChar (char c) {
cout << "Displaying character: " << c << endl;
}
void DisplayInt(int i) {
cout << "Displaying integer: " << i <<endl;
}
int main() {
DisplayStr("Good Day");
DisplayChar ('B');
DisplayInt(57);
return 0;
}
Displaying string: Good Day
Displaying character: B
Displaying integer: 57
Advantage of function overloading in C++ in Hindi
Function overloading के निम्न फायदे है।1) Function overloading आपको विभिन्न functions के लिए एक ही नाम का उपयोग करने की अनुमति देता है जिसे Code को पढ़ना और समझना आसान हो जाता है।
2) Function overloading आपको एक ही function name और विभिन्न Parameters के साथ function बनाने की सुविधा देता है जो आपको विभिन्न Data types वाले Input को Handle करने में सक्षम बनाता है।
3) आप एक ही function name को विभिन्न कार्यो के लिए पुन: उपयोग कर सकते हैं जिससे Code का Duplication कम हो जाता है।
4) आपको समान कार्यों के लिए कई functions name को याद करने की जरुरत नहीं होती आपको केवल एक ही नाम को विभिन्न Parameters के साथ Call करना होता है।
5) अपने Code को maintain और update करना आसान होता है क्योंकि एक ही स्थान पर Function logic में बदलाव हो सकता है।
6) Function overloading, polymorphism को support करता है जो functions को विभिन्न Data के प्रकार के साथ कार्य करने की अनुमति देते है।
8) Function overloading, विभिन्न प्रकार के Input को handle करते हुए आपके Program की functionality को बढ़ाता है।
9) यह संबन्धित Functions का एक साथ समूह बनाने के द्वारा आपके Code को संगठित और व्यवस्थित रखता है।
Causes Of Function Overloading In C++ in Hindi
C++ में Function Overloading इसलिए होती है क्योंकि हम Programming को आसान और अधिक कुशल बनाना चाहते हैं। यह हमें कई functions के लिए एक ही नाम का उपयोग करने की सुविधा तब तक देता है, जब तक उनके पास विभिन्न Parameters types की list या Parameters की संख्या होती है।Function overloading के निम्न तीन मुख्य कारण है
1) Type Conversion
2) Function with default arguments
3) Function with pass-by-reference
Related Posts
> C++ क्या है? उसके इतिहास, गुण, उपयोग, फायदे और नुकसान
> Basic structure of C++ Program
> C++ में Tokens क्या है? और उसके प्रकार
> C++ Variables क्या है?, उसके प्रकार, उसे कैसे Declare, Define करते हैं
> C++ में Constants क्या है? उसके प्रकारों की संपूर्ण जानकारी
> C++ में Basic Input और Output (cin,cout,cerr) की जानकारी
> Data type in C++ की संपूर्ण जानकारी
> C+ में Operators और उसके प्रकार जानें Practical सहित
> C++ में Conditional और उसके प्रकारों को जानें Practical सहित
> C++ में Looping statements और उसके प्रकार Practical सहित
> C++ में Jump Statements और उसके प्रकारों की संपूर्ण जानकारी Practical सहित
> C++ में Array क्या है? और उसके प्रकारों की जानकारी Practical सहित
> C++ में Function क्या है उसके प्रकार, उपयोग प्रोग्राम सहित
> C++ में Structure क्या है Practical सहित
> OOPs Concepts in C++ in Hindi- C++ में OOPs के बारे में
> Oops के फायदे और नुकसान की जानकारी
> C++ में Class और Object की सम्पूर्ण जनकारी
> C++ में Array of Objects क्या है?
> C++ में Pointers, Pointer to an objects, Pointer to an Array की संपूर्ण जानकारी हिंदी में।
> C++ में Passing objects क्या है
> C++ में Reference और Type Casting की संपूर्ण जानकारी
> C++ में Access specifier की संपूर्ण जानकारी
> C++ में Static Data Members और Member Functions के बारे में Practical सहित
> C++ में Memory allocation और Memory management operators (new और delete) Practical सहित
> Friend Function in C++ in Hindi
> Friend Class in C++ in Hindi Practical सहित
> Inline function in C++ in Hindi
> Operator Overloading in C++ in Hindi Practical सहित
> C++ में Constructor क्या है और उसके प्रकारों की संपूर्ण जानकारी
> C++ में Destructor क्या है ?उसकी संपूर्ण जानकारी
> C++ मे Inheritance क्या है उसके प्रकारों को जानें प्रोग्राम सहित
> C++ में Polymorphism क्या है? और उसके प्रकारों को जानें
> C++ में Virtual function की संपूर्ण जानकारी
> C++ में File handling की संपूर्ण जानकारी
> C++ में Exception handling की संपूर्ण जानकारी
0 टिप्पणियाँ