Introduction for Polymorphism
Polymorphism ये एक 'poly' और 'morph' इन दोनों ग्रीक शब्दों से बना है | इसका अर्थ होता है 'अनेक रूप होना ' |
एक ही रूप में अनेक रूप होना polymorphism होता है |
Polymorphism में inheritance का भी उपयोग किया जाता है |
Polymorphism के inheritance में base class और derived class में एक ही नाम का function होता है, लेकिन उनकी definition अलग-अलग होती है |
Polymorphism दो प्रकार के होते है |
- Compile-time Polymorphism
- Run-time Polymorphism
Compile-time Polymorphism के दो प्रकार है |
- Function Overloading
- Operator Overloading
Run-time Polymorphism का एक ही प्रकार है |
- Virtual Function


1. Function Overloading : Compile-time/Static Binding
C++ में एक से ज्यादा एक जैसे नाम के function को इस्तेमाल किया जाता है | पर C में इसका इस्तेमाल नहीं हो सकता |
एक program में एक से ज्यादा एक ही नाम के function इस्तेमाल करना 'Function Overloading' कहते है |
एक function दूसरी बार पहले जैसा इस्तेमाल नहीं किया जा सकता | For Example,
जब ऐसा होता है तो, Compiler Function cannot overloaded का error देता है |
void func(int a, int b){ } void func(int a, int b){ }
Function Overloading का इस्तेमाल करना हो तो, हर same name के function का parameter का data_type, parameters की संख्या अलग-अलग इस्तेमाल किया जाता है | For Example,
void func(){ } void func(int a){ } void func(float a){ } void func(int a, int b){ } void func(int a, int b, int c){ }
Example for Function Overloading
Output:#include <iostream.h> using namespace std; void func(); void func(int); void func(float); void func(int, float); void func(int, float, int); int main() { int x = 10; float y = 3.14; int z = 20; func(); func(x); func(y); func(x, y); func(x, y, z); return 0; } void func(){ cout<<"Hello World!"<<endl; } void func(int a){ cout<<"Value of a : "<<a<<endl; } void func(float b){ cout<<"Value of b : "<<b<<endl; } void func(int a, float b){ cout<<"Value of a : "<<a<<endl; cout<<"Value of b : "<<b<<endl; } void func(int a, float b, int c){ cout<<"Value of a : "<<a<<endl; cout<<"Value of b : "<<b<<endl; cout<<"Value of c : "<<c<<endl; }
Hello World! Value of a : 10 Value of b : 3.14 Value of a : 10 Value of b : 3.14 Value of a : 10 Value of b : 3.14 Value of c : 20
2. Operator Overloading : Compile-time/Static Binding/Early Binding
C++ Operator में Arithmetic, Logical, Conditional इत्यादी Operators के प्रकार होते है |
C++ के हर Operator का अलग-अलग काम होता है | For Example,
- + : दो numbers का addition करना |
- * : दो numbers का multiplication करना |
Operator Overloading में इन Operators को किसी दुसरे तरह से भी define किया जा सकता है | इन Operators का इस्तेमाल 'operator' keyword के साथ किसी function_name की तरह किया जाता है | अगर user + Operator को overload करता है तो, वो अपनी पहली defintion को भी same program में इस्तेमाल कर सकता है |
कौनसे Operators 'Operator Overloading' के लिए सही है ?
C++ के सभी Operators का overloading में इस्तेमाल किया जा सकता है | पर कुछ Operators ऐसे है की जीना इस्तेमाल Operator Overloading में नहीं हो सकता |
निचे दिए हुए Operators का इस्तेमाल Overloading में नहीं किया जा सकता |
Operator | Name of Operator |
---|---|
. | Accessing Members |
.* | Pointer to accessing members |
?: | Conditional |
:: | Scope Resolution |
निचे दिए हुए Operators का इस्तेमाल Overloading में किया जाता है |
Operator | Name of Operator |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
= | Assignment |
& | Bitwise AND and Address |
+= | Addition assignment |
–= | Subtraction assignment |
*= | Multiplication assignment |
/= | Division assignment |
&= | Bitwise AND assignment |
%= | Modulus assignment |
>= | Greater than or equal to |
== | Equal |
++ | Increment |
–– | Decrement |
< | Less than |
> | Greater than |
<< | Left shift |
>> | Right shift |
<<= | Left shift assignment |
>>= | Right shift assignment |
| | Bitwise inclusive OR |
|= | Bitwise inclusive OR assignment |
^ | Exclusive OR |
^= | Exclusive assignment |
~ | One's Complement |
, | Comma |
! | Logical NOT |
!= | NOT or equal |
&& | Logical AND |
|| | Logical OR |
* | Pointer |
–> | Member selection |
–>* | Pointer-to-member |
[ ] | Array subscript |
new | New |
delete | Delete |
Syntax for Operator Overloading
return_type operator operator_to_overload(parameters_list){ //statement(s); }
Operator Overloading का इस्तेमाल क्यों किया जाता है ?
C++ में हमें चाहे वैसा program 'Operator Overloading' के बिना भी कर सकते है | Operator Overloading का इस्तेमाल करना और program समझ आने में आसानी होती है | पहली बार जब user; Operator Overloading पढ़ता है , तो उसे कुछ समझ में नहीं आता और जब समझ आये तो उसे हर Operator की Overloading समझ आती है |
Normal Program में जैसे दो varaibles को add किया जाता है | C++ में दो object को add करना आसान नहीं होता, लेकिन Operator Overloading में ये मुमकिन है |
For Example.
int main(){ Number n1, n2, n3; n3 = n1 + n2; //+ is overloaded operator }
Example for '+' Operator Overloading
Output:#include <iostream.h> using namespace std; class Complex{ private: int a, b; public: void put(int x=0, int y=0){ a = x; b = y; } Complex operator+(Complex c){ Complex com; com.a = a + c.a; com.b = b + c.b; return com; } void get(){ cout<<"Real Number : "<<a<<endl; cout<<"Imaginary Number : "<<b<<endl; cout<<"Complex Number : "<<a<< " + "<<b<<"i"; } }; int main() { Complex c1, c2, c3; c1.put(5, 10); c2.put(7, 9); c3 = c1 + c2; //Use also Complex c3 = c1.operator+(c2); c3.get(); return 0; }
Real Number : 12 Imaginary Number : 19 Complex Number : 12 + 19i
उपरवाला Program Binary Operator का है |
Operator Overloading के लिए Operators के लिए दो प्रकार है |
- Binary Operators
- Unary Operators
1. Binary Operators Binary Operators के लिए दो operands दिए जाते है | for eg. +, -
2. Unary Operators Unary Operators के लिए एक operand दिया जाता है | for eg. ++, --
'-' Unary Operator Overloading
Output:#include <iostream.h> using namespace std; class Number{ private: int a; int b; public: Number(){ // Constructor is required a=5; b=-7; } void operator -(){ a = -a; b = -b; } void show(){ cout<<"Value of a : "<<a<<endl; cout<<"Value of b : "<<b<<endl; } }; int main(){ Number x; cout<<"Before Overloading"<<endl; x.show(); -x; // Unary Operator overloading cout<<"After Overloading"<<endl; x.show(); return 0; }
Before Overloading Value of a : 5 Value of b : -7 After Overloading Value of a : -5 Value of b : 7
1. Virtual Funtion : Run-time/Dynamic Binding/Late Binding
Introduction for Virtual Function
निचे दिये हुए program में एक base class और उससे नया derived class लिया है और Base Class का pointer और derived class का object बनाया है |
अगर Base class को Base class pointer से point किया जाए तो base class के member functions को access जा सकता है |
यहाँ पर base class के pointer से derived class के members को point करके access नहीं किया जा सकता |
लेकिन Virtual Function से Base class के pointer से derived class के members को point करके उनके members को access किया जा सकता है |
Without Virtual Function
Source Code :Output:#include <iostream> using namespace std; class Base{ public: void display(){ cout << "Base Class"; } }; class Derived : public Base{ public: void display(){ cout << "Derived Class"; } }; int main(){ Base *ptr; //Base class pointer Derived d; ptr = &d; ptr->display(); //Early Binding return 0; }
Base Class
Virtual Function से यहाँ पर Late Binding होता है |
With Virtual Function
Source Code :Output:#include <iostream> using namespace std; class Base{ public: virtual void display(){ // or void virtual display(){ cout << "Base Class"; } }; class Derived : public Base{ public: void display(){ cout << "Derived Class"; } }; int main(){ Base *ptr; //Base class pointer Derived d; ptr = &d; ptr->display(); //Late Binding return 0; }
Derived Class
Abstract Class and Pure Virtual Function
जिस class में एक या एक से अधिक pure virtual function होता है, तो उसे Abstract Class कहते है |
Example for Abstract Class
class A{ // Abstract Class virtual void show() = 0; // pure virtual function };
जिस virtual function की कोई definition नहीं होती, तो उसे 'Pure virtual Function' कहते है |
virtual void show() = 0;
Base Class में Virtual function की कोई definition नहीं होती, अगर derived class; base class को inherit कर रहा हो तो वहा पर virtual function की defintion दी जाती है | जब Pure virtual function के Absract class को कोई derived class; inherit कर रहा है तो, उसे भी Abstract class कहते है , चाहे उसमे Pure virtual function हो या ना हो |
Abstract Class की विशेषताए
- Abstract Class का कोई object नहीं बनाया जाता | लेकिन उसका pointer बनाया जा सकता है |
- अगर abstarct class को कोई derived class; inherit कर रहा हो और inherit हुए pure virtual function की definition नहीं लिखता तो , वो भी Abstract Class बन जाती है और उसका Object भी नहीं बन पाता |
Example for Abstract and Pure Virtual Function
Source Code :Output :#include <iostream> using namespace std; class A{ public: void virtual show() = 0; }; class B : public A{ public : void show(){ cout<<"Hello World!"; } }; int main(){ A *ptr; B b; ptr = &b; ptr->show(); return 0; }
Hello World!
Example for Pure Virtual Function and Run-time Polymorphism
Output:#include <iostream.h> using namespace std; class A{ public: void virtual show() = 0; }; class B : public A{ public : void show(){ cout<<"Hello World!"<<endl; } }; class C : public A{ public : void show(){ cout<<"Hello Friend!"<<endl; } }; int main(){ A *ptr; B b; C c; ptr = &b; ptr->show(); ptr = &c; ptr->show(); return 0; }
Hello World! Hello Friend!