Polymorphism in C++ - W3 Coding Club

    Polymorphism in C++

    polymorphism शब्द का अर्थ है अनेक forms(रूपों) का होना। सरल शब्दों में, हम polymorphism को एक message(संदेश) की एक से अधिक forms(रूपों) में displayed करने की ability(क्षमता) के रूप में परिभाषित कर सकते हैं। polymorphism का एक real-life example हैं कि एक ही समय में एक व्यक्ति की अलग-अलग विशेषताएं हो सकती हैं। एक आदमी की तरह एक ही समय में एक पिता, एक पति, एक कर्मचारी है। तो एक ही व्यक्ति का अलग-अलग परिस्थितियों में अलग-अलग व्यवहार होता है। इसे polymorphism कहते हैं। polymorphism को Object Oriented Programming(ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग) की महत्वपूर्ण विशेषताओं(important features) में से एक माना जाता है।

     C++ में पॉलिमॉफिज्म दो प्रकार से प्रयोग में लिया जा सकता है

    1. Function Overloading(फंक्शन ओवरलोडिंग) में, तथा
    2. ऑपरेटर ओवरलोडिंग में

    Function Overloading(फंक्शन ओवरलोडिंग) में एक ही नाम के एक से अधिक फंक्शन हो सकते हैं। एक ही नाम के एक से अधिक फंक्शन बनाने के पीछे उद्देश्य यह होता है कि वे समान प्रकार का कार्य अलग-अलग तरीकों से करें।

    Operator Overloading(ऑपरेटर ओवरलोडिंग) के माध्यम से पहले से मौजूद ऑपरेटर्स को किसी क्लास के संदर्भ में नया अर्थ प्रदान करने से है। सरल शब्दों में कहा जा सकता है कि ऑपरेटर ओवरलोडिंग के माध्यम से यह तय किया जा सकता है कि किसी क्लास के ऑब्जेक्ट के लिए कोई ऑपरेटर कैसे कार्य करेगा।

    C++ में polymorphism को मुख्य रूप से दो प्रकारों में बांटा गया है:

    1. Compile time Polymorphism
    2. Runtime Polymorphism


    1. Compile time polymorphism

    इस प्रकार का polymorphism Function Overloading(फ़ंक्शन ओवरलोडिंग) या operator overloading(ऑपरेटर ओवरलोडिंग) द्वारा प्राप्त किया जाता है

    ● Function Overloading(फंक्शन ओवरलोडिंग) : जब एक ही नाम के कई फंक्शन होते हैं लेकिन अलग-अलग parameters (पैरामीटर) होते हैं तो इन फंक्शन्स को overloaded(ओवरलोडेड) कहा जाता है । arguments की संख्या में परिवर्तन या/और arguments के प्रकार में परिवर्तन द्वारा Function को overloaded किया जा सकता।

    Example:-


        // C++ program for function overloading
        #include <stdio.h>
        #include <iostream>

        using namespace std;
        class w3club
        {
        public:
            // function with 1 int parameter
            void func(int x)
            {
                cout << "value of x is " << x << endl;
            }

            // function with same name but 1 double parameter
            void func(double x)
            {
                cout << "value of x is " << x << endl;
            }
            // function with same name and 2 int parameters
            void func(int x, int y)
            {
                cout << "value of x and y is " << x << ", " << y << endl;
            }
        };

        int main()
        {

            w3club obj1;

            // Which function is called will depend on the parameters passed
            // The first 'func' is called
            obj1.func(7);

            // The second 'func' is called
            obj1.func(9.132);

            // The third 'func' is called
            obj1.func(85, 64);
            return 0;
        }

    Output:-

    value of x is 7
    value of x is 9.132       
    value of x and y is 85, 64

    Explain Example:-

    उपरोक्त उदाहरण में, func नामक एक single function तीन अलग-अलग स्थितियों में अलग-अलग कार्य करता है जो polymorphism की property है।

    ● Operator Overloading(ऑपरेटर ओवरलोडिंग) : C++ Operators को overload करने का option भी provide करता है। उदाहरण के लिए, हम दो Strings को जोड़ने के लिए String Class के लिए Operator ('+') बना सकते हैं। हम जानते हैं कि यह addition operator है जिसका कार्य दो operands(ऑपरेंड) को जोड़ना है। तो एक single operator '+' जब string operands के बीच रखा जाता है, तो उन्हें concatenates है और जब integer operands के बीच रखा जाता है, तो उन्हें Add(जोड़ता) है।

    Example:-


        // CPP program to illustrate
        // Operator Overloading
        #include <stdio.h>
        #include <iostream>
        using namespace std;

        class Complex
        {
        private:
            int real, imag;

        public:
            Complex(int r = 0, int i = 0)
            {
                real = r;
                imag = i;
            }

            // This is automatically called when '+' is used with
            // between two Complex objects
            Complex operator+(Complex const &obj)
            {
                Complex res;
                res.real = real + obj.real;
                res.imag = imag + obj.imag;
                return res;
            }
            void print() { cout << real << " + i" << imag << endl; }
        };

        int main()
        {
            Complex c1(10, 5), c2(2, 4);
            Complex c3 = c1 + c2; // An example call to "operator+"
            c3.print();
        }

    Output:-

    12 + 19

    Explain Example:-

    उपरोक्त उदाहरण में operator '+' overloaded है। operator '+' एक addition operator है और दो Numbers (integers या floating point) को जोड़ सकता है लेकिन यहां operator को दो complex या imaginary Numbers को जोड़ने के लिए बनाया गया है।



    2. Runtime polymorphism

    इस प्रकार का polymorphism Function overriding(फ़ंक्शन ओवरराइडिंग) द्वारा प्राप्त किया जाता है।

    ● Function overriding(फ़ंक्शन ओवरराइडिंग):- Function overriding(फ़ंक्शन ओवरराइडिंग) तब होती है जब एक derived class में base class के member functions में से एक की definition होती है। उस base function को overridden कहा जाता है ।


        // C++ program for function overriding
        #include<iostream>
        #include<stdio.h>
        using namespace std;
        class base
        {
        public:
            virtual void print()
            {
                cout << "print base class" << endl;
            }
            void show()
            {
                cout << "show base class" << endl;
            }
        };
        class derived : public base
        {
        public:
            void print() // print () is already virtual function in derived class, we could also declared as virtual void print () explicitly
            {
                cout << "print derived class" << endl;
            }
            void show()
            {
                cout << "show derived class" << endl;
            }
        };

        // main function
        int main()
        {
            base *bptr;
            derived d;
            bptr = &d;

            // virtual function, binded at runtime (Runtime polymorphism)
            bptr->print();

            // Non-virtual function, binded at compile time
            bptr->show();

            return 0;
        }

    Output:-

    print derived class
    show base class




    This article is contributed by Nirma Kanwar If you like W3 Coding club and would like to contribute, you can also write an article and mail on w3codingclub@gmail.com. See your article appearing on the W3 Coding Club main page.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    Post a Comment

    1 Comments