Polymorphism
Polymorphism-
Poly means many (कई सारे) होता है, यह OOPS (object-oriented programming) का एक concept है जिसमें किसी function या operator को कई तरीकों से एक ही name के साथ access किया जा सकता है ।
Polymorphism are two types.
Compile time Polymorphism-
Program को compile करते time एक ही name के function की अलग definition compile कर दी जाती है।
Compile time polymorphism दो प्रकार का होता है।
(i) Function Overloading- एक ही name के function परंतु अलग अलग parameter use हो तो function overloading कहलाती है।
Compile time polymorphism दो प्रकार का होता है।
(i) Function Overloading- एक ही name के function परंतु अलग अलग parameter use हो तो function overloading कहलाती है।
Example-
void area (int a, int b)
void area (int c)
void area (int d, int e, int f)
int area (int a)
{
return (a*a);
}
int area (int b, int c)
{
return (b*c);
}
Cout<<area(5);
// output=25(area of square)
Cout<<area(5,10);
// output=50(area of rectangle)
(ii) Operator Overloading- एक single operator को दो या दो से अधिक कार्यो के लिए प्रयुक्त किया जाये तो operator overloading कहलाती है।
Example-
5+2=7
इसमें '+' operator द्वारा addition किया गया है एक new (diffrent) output प्राप्त हुआ।
ganesh+gururani=ganeshgururani
इस example में उसी '+' operator को दो string को catenate(add) करने के लिए प्रयोग किया गया है इससे जो output प्राप्त हुआ है वह input से मिलता जुलता (समान) है।
Run Time Polymorphism- Program के run होने पर एक ही name के दो या दो से अधिक function में से किसे execute होना है यह decide किया जाये तो वह run time है।
★Run time polymorphism को virtual function के जरिये प्राप्त किया जा सकता है।