marq

Dr. Charles Simonyi is the Father of Modern Microsoft Excel                                           JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.                                           The word "Biology" is firstly used by Lamarck and Treviranus                                           Hippocrates (460-370 bc) is known as father of medicine.                                           Galene, 130-200 is known as father of Experimental Physology                                           Aristotle (384-322 BC) is known as Father of Zoology because he wrote the construction and behavior of different animals in his book "Historia animalium"                                           Theophrastus(370-285 BC) is known as father of Botany because he wrote about 500 different plants in his book "Historia Plantarum".                                           John Resig is known as Father of Jquery -                                          HTML is a markup language which is use to design web pages. It was invented in 1990 by Tim Berners-Lee.                                                                The Google was founded by Larry Page and Sergey Brin.                                                                Rasmus Lerdorf was the original creator of PHP. It was first released in 1995.                                                               Facebook was founded by Mark Zuckerberg                                                               Bjarne Stroustrup, creator of C++.                                                                Dennis Ritchie creator of C                                                                                                                              James Gosling, also known as the "Father of Java"                                          At 11.44%, Bihar is India's fastest growing state                                          Father of HTML -Tim Berners Lee                                          orkut was created by Orkut Büyükkökten, a Turkish software engineer                    Photoshop: It came about after Thomas Knoll, a PhD student at the University of Michigan created a program to display grayscale images on a monochrome monitor which at the time was called 'Display'.

Polymorphism


Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.



Object Oriented Programming के तीन मुख्‍य Concepts हैं। पहला Concept है Class और दूसरा Concept है Inheritance, जिन्हें हमने पहले ही समझ लिया है। OOPS का तीसरा सबसे महत्वपूर्ण Concept है Polymorphism, जिसे C++ में Virtual Functions द्वारा Implement किया जाता है।


Polymorphism
वास्तविक जीवन में भी हम देखते हैं कि विभिन्न प्रकार की चीजों का कोई समूह किसी एक Instruction के प्रभाव में विभिन्न प्रकार के Reactions करता है। किसी एक Instruction का विभिन्न लोगों पर विभिन्न प्रभाव होता है और सभी लोग उस एक Instruction को अपनी सुविधा के अनुसार समझ कर उसके प्रति अपना Reaction देते हैं। यही प्रक्रिया C++ में Polymorphism कहलाती है। इसे एक उदाहरण द्वारा समझने की कोशिश करते हैं।


हम सभी जानते हैं कि किसी University में विभिन्न प्रकार के Students अपनी इच्छानुसार विभिन्न प्रकार के Subjects को Choose करते हैं और उसी Subject की पढाई करने के लिए University के किसी विशेष Department में Admission लेते हैं। जिसे Engineer बनना है वह Engineering के Subject के Department में Admission लेता है, जिसे Computer Science के Field में अपना Career बनाना है, वह Computer Science के Department में Admission लेता है और जिसे Doctor बनना है, वह Doctors के Department में Admission लेता है।


हर University में विभिन्न प्रकार के Students Admission लेने के लिए अपने-अपने Registration Form कब Submit कर सकते हैं, इसका निर्णय University का मुख्‍य कार्यकर्ता जिसे Dean कहते हैं, लेता है। वही ये तय करता है कि University में Admission कब Open होंगे। मानलो कि आप किसी University के Dean हैं और आप सभी Students को ये Information देना चाहते हो कि University में Admission Open हो चुके हैं। जिन Students को आपकी University में Admission लेना है, वे अपना Registration Form Fill करके University में Submit कर सकते हैं। यानी आप Students को Message देते हैं कि


“Fill out your registration form!”


आपके इस Message का विभिन्न Subjects के साथ University में Admission लेने वाले विभिन्न Students पर अलग असर होता है। जो Student Science Biology Subject में Admission लेना चाहता है, वह Science Biology के Admission Form को Fill करता है। जो Engineer बनना चाहता है, वह Engineering का Registration Form Fill करता है और जो Computer Science में Admission लेना चाहता है, वह Computer Science का Registration Form Fill करता है।


आप हर Group के Student को अलग-अलग Message नहीं देते हैं कि Doctor बनने वाले Students Doctor का Registration Form Fill करें, Engineer बनने वाले Students Engineering का Registration Form Fill करें और डै। या M. Tech. करने वाले Students Computer Science का Registration Form Fill करे। यानी Dean के एक Message को विभिन्न Student विभिन्न प्रकार से Respond करते हैं और सभी को पता होता है कि Dean के दिए गए Message का उनके लिए क्या अर्थ है और उन्हें किस Registration Form को Fill करके Submit करना है।


एक Action के Response में कई प्रकार की Reaction होना या एक ही बात का अलग-अलग स्थितियों में अलग-अलग रूप लेना ही, Polymorphism कहलाता है। Dean द्वारा दिया जाने वाला एक Single Instruction Polymorphic Message है क्योंकि विभिन्न Students के लिए ये एक अलग Instruction है। यानी Dean के इस एक ही Message में Computer Science वाले Student के लिए Computer Science Registration Form को Fill करने का Instruction है और Engineering Student के लिए Engineering Registration Form Fill करने का Instruction है।


C++ में Polymorphism की स्थिति तब उत्पन्न होती है जब विभिन्न Classes आपस में Inheritance द्वारा Relate की जाती हैं। C++ में Polymorphism का मतलब होता है कि कोई Object जिस Member Function को Invoke करता है, उसी नाम का Member Function विभिन्न Classes में होता है, लेकिन वही Member Function Invoke होता है, जिसकी Class (Type) का Object Define किया गया होता है। यानी Object समान Member Functions को ही Call करता है लेकिन Object की प्रकृति के आधार पर अलग-अलग Member Functions Execute होते हैं और समान नाम के Member Functions Object की Class के आधार पर अलग-अलग प्रकार का काम करते हैं।


Polymorphism Overloading नहीं है, बल्कि Overloading से कहीं ज्यादा Powerful व Different Mechanism है। Overloading व Polymorphism में मुख्‍य अन्तर ये है कि Overloading होने पर Compiler Compile Time में ये तय करता है कि किस Function को Execute करना है जबकि Polymorphism में किस स्थिति में कौनसा Function Execute होगा, इसका निर्णय Compiler Program के Run Time में लेता है ना कि Compile Time में। Overloading से Class User Class को सरल तरीके से अपने Program में Use करने का काम करता है जबकि Polymorphism का प्रयोग Class Creator करता है और Polymorphism Program को Class User के लिए Convenient बनाने के लिए नहीं किया जाता है बल्कि Polymorphism का प्रयोग पूरे Program के Architecture को प्रभावित करता है।
Click here to know me more.....

#include <iostream>

using namespace std;
class CPolygon
{
   protected:
     int width, height;
   public:
     void set_values (int a, int b)
       { width=a; height=b; }
};
 class CRectangle: public CPolygon {
   public:
     int area ()
       { return (width * height); }
   };
 class CTriangle: public CPolygon
{
   public:
     int area ()
       {
       return (width * height / 2);
       }
};
Int main ()
{
   CRectangle rect;
   CTriangle trgl;
   CPolygon * ppoly1 = &rect;
   CPolygon * ppoly2 = &trgl;
   ppoly1->set_values (4,5);
   ppoly2->set_values (4,5);
   cout << rect.area() << endl;
   cout << trgl.area() << endl;
   return 0;
 }

Polymorphism, Overloading and Dynamic Binding - C++ in Hindi

Polymorphism and Overloading
जैसाकि हमने पहले भी बताया कि एक Class “C” के Structure का ही Modified रूप है। यानी हम Structure प्रकार के Variable तो Declare कर सकते हैं, लेकिन जिस प्रकार से Built - In प्रकार के Data Type के दो Variables को हम आपस में जोड सकते हैं, ठीक उसी प्रकार से किसी Structure के दो Variables को हम नहीं जोड सकते। इसे समझने के लिये हम एक उदाहरण देखते हैं। माना एक Structure निम्नानुसार है-


Late Binding

view plainprint?
struct Add
{
int num1;
int num2;
};

struct Add A, B, C ;


यदि हम C = A + B ; करें, तो ये एक गलत Statement होगा। किसी Class के Objects को भी हम ठीक इसी प्रकार से नहीं जोड सकते हैं, क्योंकि Class Structure का ही Modified रूप है। इसका कारण ये है कि Compiler ये नहीं जानता है कि User द्वारा Define किए गए Variables के साथ किस प्रकार से जोड किया जाए।


जबकि Built-in Data Types में जोड करने के इस तरीके को Compiler में पहले से ही निश्चित कर दिया गया है और + Operator ये जानता है कि इन Variables को कैसे जोडा जाए। इसलिये User Defined Data Type के Variables या Object को जोडने का तरीकाOperators को बताना पडता है।


यानी + Operators को ये बताना पडता है कि किस प्रकार से User Defined Data Type के Variables या Objects को ये Operator जोडेगा। इसके लिये हमें नए प्रकार के Operations Define करने होते हैं।


इस प्रकार से Operators व Functions को अलग-अलग तरीके से इस बात पर निर्भर करते हुए कि वे किस प्रकार के Data पर Operation कर रहे हैं, Use किया जाता है और इस प्रक्रिया कोPolymorphism कहा जाता है।


किसी Existing Operator जैसे कि +, = आदि को इस लायक बनाया जाता है कि वे User Defined Data Type के विभिन्न प्रकार के Variables या Objects पर क्रिया कर सकें। विभिन्न Operators को इस लायक बनाने की क्रिया को Operators की Overloading करना कहा जाता है।


जब कई Functions के नाम समान होते हैं, लेकिन उनके Arguments या Parameters की संख्‍या या फिर Formal Arguments के Data Type में आपस में अन्तर होता है, तो इसे Functions की Overloading होना कहा जाता है। Overloading से एक Programmer के लिए Program लिखना व समझना आसान हो जाता है। यह भी Polymorphism का एक तरीका है।


Dynamic Binding
किसी Object के Reference में कौनसा Function Call होना चाहिए, जब ये बात Program के Compile Time में तय होती है, तो इसे Early Binding कहते हैं। जबकि किसी Object के Reference में किसी काम के लिए कौनसा Procedure Execute होगा, ये बात जब Program के Runtime में तय होती है, तब इसे Late Binding या Dynamic Binding कहते हैं। Polymorphism के अन्तर्गत Dynamic Binding का काम होता है। इसे समझने के लिए निम्न चित्र देखिए-






इस चित्र में हम देख सकते हैं कि Shape Class एक Base Class है, जिसे Inherit करके तीन नई Classes Circle, Box व Triangle को Create किया गया है। चूंकि ये तीनों ही Classes Shape Class से Inherited हैं, इसलिए Base Class Shape का Draw() Method तीनों ही Classes में Inherited है।


अब मानलो कि हमने तीनों Derived Classes का एक-एक Object Create किया और उस Object को Draw करने के लिए Draw() Method को Call किया। ऐसे में जब हम Circle Class के Object के लिए Draw Method Call करते हैं, तब Compiler Circle Class के Draw Method को Call करके Circle Draw करता है।


जब हम Box Object Draw करने के लिए Box Class के Object के Reference में Draw() Method को Call करते हैं, तब Compiler Box Class के Draw Method को Execute करता है।


इसी तरह से जब हम Triangle Class का Object Create करना चाहते हैं, तब Compiler Triangle Class के Draw Method को Execute करके Triangle Draw कर देता है। यानी एक ही नाम का Draw Method Create हो रहे Object की Class के आधार पर उसी Class के Draw Method को Execute करता है, जिस Class का Object Create किया जा रहा है। इस प्रक्रिया को Object के साथ Method की Binding होना कहते हैं।


चूंकि किस Object के Reference में कौनसा Draw Method Call होगा, इसका निर्णय Compiler Program के Runtime में करता है, इसलिए इस प्रक्रिया को Late Binding याDynamic Binding कहते हैं।

No comments:

Post a Comment