Polymorphism in Hindi
Polymorphism in C Explained with Types and Benefits in Hindi
Polymorphism in Hindi
What is Polymorphism in C in Hindi
Polymorphism शब्द दो शब्दों से मिलकर बना है:
- Poly → जिसका मतलब है "अनेक" (Many)
- Morphism → जिसका मतलब है "रूप" (Forms)
इस प्रकार Polymorphism का पूरा अर्थ होता है — "अनेक रूप" या "एक चीज़ के कई रूप"।
Polymorphism एक ऐसा concept है जिसमें एक ही नाम या एक ही entity अलग-अलग परिस्थितियों में अलग-अलग रूप में दिखाई देती है या अलग व्यवहार करती है।
Types of Polymorphism in C in Hindi
Polymorphism मुख्यतः दो प्रकार का होता है:
- Compile Time Polymorphism (Static Polymorphism)
- Run Time Polymorphism (Dynamic Polymorphism)
Compile Time Polymorphism in Hindi
C language में direct polymorphism (जैसे C++ में होता है) available नहीं होता, इसलिए हम इसे C++ के program के द्वारा समझेंगे।।
Compile Time Polymorphism वह concept है जिसमें function कौन सा execute होगा, यह compile होने के समय ही decide हो जाता है। इसे Static Polymorphism भी कहा जाता है।
मुख्य तरीके
- Function Overloading
- Operator Overloading
1. Function Overloading Example
#include
using namespace std;
class Math {
public:
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
};
int main() {
Math obj;
cout << obj.add(2, 3) << endl; // int version
cout << obj.add(2.5f, 3.5f) << endl; // float version
cout << obj.add(2, 3, 4) << endl; // 3 parameter version
return 0;
}
- एक ही function नाम add() को कई बार define किया गया है
- लेकिन parameters अलग-अलग हैं
- Compiler पहले से decide करता है कि कौन सा function call होगा
2. Operator Overloading Example
#include
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r, int i) {
real = r;
imag = i;
}
Complex operator + (Complex obj) {
return Complex(real + obj.real, imag + obj.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
c3.display();
return 0;
}
- Decision compile time पर होता है
- Execution fast होता है
- Function Overloading सबसे common तरीका है
- Operator Overloading से operators को customize कर सकते हैं
Run Time Polymorphism in Hindi
C language में direct polymorphism (जैसे C++ में होता है) available नहीं होता, इसलिए हम इसे C++ के program के द्वारा समझेंगे।
Run Time Polymorphism वह concept है जिसमें function कौन सा call होगा, यह program run होने के समय decide होता है। इसे Dynamic Polymorphism भी कहा जाता है।
यह कैसे achieve होता है?
- Function Overriding
- Virtual Function
Example (Virtual Function + Overriding)
#include
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks" << endl;
}
};
int main() {
Animal* obj; // base class pointer
Dog d;
obj = &d;
obj->sound(); // Run time पर decide होगा
return 0;
}
- Animal base class है
- Dog derived class है
- sound() function override किया गया है
- virtual keyword के कारण run time पर function call decide होता है
- Base class pointer, derived class object को point कर रहा है
Output
Dog barks
- Decision run time पर होता है
- Virtual function जरूरी है
- Inheritance का use होता है
- Flexibility और reusability बढ़ती है
Polymorphism Using Function Pointer in C
C language में Function Pointers का उपयोग करके हम कुछ हद तक Polymorphism जैसा behavior achieve कर सकते हैं।
Example of Function Pointer in C:
#include
void add(int a, int b) {
printf("Addition: %d\\n", a + b);
}
void subtract(int a, int b) {
printf("Subtraction: %d\\n", a - b);
}
int main() {
void (*operation)(int, int);
operation = &add;
operation(10, 5); // Addition
operation = &subtract;
operation(10, 5); // Subtraction
return 0;
}
यहां पर operation एक function pointer है, जो दो अलग-अलग functions को call कर सकता है, जिससे यह Polymorphism जैसा व्यवहार करता है।
Benefits of Using Polymorphism in Hindi
Polymorphism के प्रयोग से हम flexible और reusable code बना सकते हैं। इसके कई महत्वपूर्ण लाभ होते हैं:
- Code Reusability: एक ही नाम के function का उपयोग करके हम अलग-अलग डेटा types के साथ काम कर सकते हैं, जिससे बार-बार नया function लिखने की ज़रूरत नहीं होती।
- Readability: Polymorphism से code ज़्यादा readable और आसान बनता है, जिससे इसे समझना और debug करना सरल हो जाता है।
- Extensibility: अगर हमें भविष्य में नए प्रकार के objects या operations जोड़ने हैं, तो Polymorphism की मदद से हम आसानी से नए functions implement कर सकते हैं।
- Runtime Flexibility: Run Time Polymorphism से program run होते समय behavior बदल सकता है, जिससे अधिक dynamic और responsive program बनाया जा सकता है।
- Maintainability: एक ही logic को reuse करने से code छोटा होता है, जिससे उसे maintain करना आसान हो जाता है।
Difference Between Compile Time and Run Time Polymorphism
| Aspect | Compile Time Polymorphism | Run Time Polymorphism |
|---|---|---|
| Identification | Compile Time पर तय होता है | Run Time पर तय होता है |
| Achieved by | Function Overloading, Operator Overloading | Virtual Functions, Function Pointers |
| Speed | तेज़ (Fast) | धीमा (Slow) |
| Flexibility | कम | अधिक |
| Use | Simple Programs में | Complex Systems में |
Summary in Simple Terms
- Polymorphism एक ही नाम के functions का अलग-अलग व्यवहार है।
- Compile Time Polymorphism compile होते समय तय होता है, और यह तेज़ होता है।
- Run Time Polymorphism रन टाइम पर तय होता है और अधिक flexible होता है।
- C language में direct Polymorphism नहीं है लेकिन function pointers से achieve किया जा सकता है।
- Polymorphism से code साफ, छोटा, reusable और maintainable बनता है।