Multiple Inheritance in C++ -W3 Coding Club

    Multiple Inheritance in C++

    Multiple Inheritance C ++ का एक feature है जहाँ एक Class एक से अधिक classes को inherit कर सकता है।
    inherit की गई classes के constructors को उसी क्रम में बुलाया जाता है जिसमें वे inherit में मिले हैं। उदाहरण के लिए, निम्नलिखित program में, B के constructor को A के constructors से पहले call किया गया हैं।

    Example:-


        #include <iostream>
        using namespace std;

        class A
        {
        public:
            A() { cout << "A's constructor called" << endl; }
        };

        class B
        {
        public:
            B() { cout << "B's constructor called" << endl; }
        };

        class C : public B, public A // Note the order
        {
        public:
            C() { cout << "C's constructor called" << endl; }
        };

        int main()
        {
            C c;
            return 0;
        }

    Output:-

    B's constructor called
    A's constructor called
    C's constructor called

    Destructors (डिस्ट्रक्टर्स) को constructors(कंस्ट्रक्टर्स) के Reverse Order में call किया जाता है।

    The diamond problem

    की समस्या हीरे की समस्या तब होती है जब एक वर्ग के दो सुपरक्लास में एक समान आधार वर्ग होता है। उदाहरण के लिए, निम्नलिखित आरेख में, टीए वर्ग को व्यक्ति वर्ग की सभी विशेषताओं की दो प्रतियां मिलती हैं, इससे अस्पष्टता होती है।

    Multiple Inheritance in C++, W3 Coding Club, w3codingclub

    Example:-


        #include <iostream>
        using namespace std;
        class Person
        {
            // Data members of person
        public:
            Person(int x) { cout << "Person::Person(int ) called" << endl; }
        };

        class Faculty : public Person
        {
            // data members of Faculty
        public:
            Faculty(int x) : Person(x)
            {
                cout << "Faculty::Faculty(int ) called" << endl;
            }
        };

        class Student : public Person
        {
            // data members of Student
        public:
            Student(int x) : Person(x)
            {
                cout << "Student::Student(int ) called" << endl;
            }
        };

        class TA : public Faculty, public Student
        {
        public:
            TA(int x) : Student(x), Faculty(x)
            {
                cout << "TA::TA(int ) called" << endl;
            }
        };

        int main()
        {
            TA ta1(30);
        }

    Output:-

    Person::Person(int ) called
    Faculty::Faculty(int ) called
    Person::Person(int ) called
    Student::Student(int ) called
    TA::TA(int ) called

    Explain Example:-

    उपरोक्त Program में, 'Person' के constructor को दो बार call किया जाता है। Object 'ta1' के destructed होने पर 'Person ' का Destructor भी दो बार call किया जाएगा। तो Object 'ta1' में 'Person' के सभी members की दो Copies हैं, इससे ambiguities होती है। इस problem का solution 'virtual’ keyword' है । हम 'ta1' class में 'Person' की दो copies से बचने के लिए classes को 'Faculty' और 'Student' को virtual base classes बनाते हैं।

    उदाहरण के लिए, निम्नलिखित Program को consider करें।

    Example:-


        #include <iostream>
        using namespace std;
        class Person
        {
        public:
            Person(int x) { cout << "Person::Person(int ) called" << endl; }
            Person() { cout << "Person::Person() called" << endl; }
        };

        class Faculty : virtual public Person
        {
        public:
            Faculty(int x) : Person(x)
            {
                cout << "Faculty::Faculty(int ) called" << endl;
            }
        };

        class Student : virtual public Person
        {
        public:
            Student(int x) : Person(x)
            {
                cout << "Student::Student(int ) called" << endl;
            }
        };

        class TA : public Faculty, public Student
        {
        public:
            TA(int x) : Student(x), Faculty(x)
            {
                cout << "TA::TA(int ) called" << endl;
            }
        };

        int main()
        {
            TA ta1(30);
        }

    Output:-

    Person::Person() called
    Faculty::Faculty(int ) called
    Student::Student(int ) called
    TA::TA(int ) called

    Explain Example:-

    उपरोक्त program में, 'Person' के constructor को एक बार call किया जाता है। उपरोक्त Output में ध्यान देने योग्य एक important point यह है कि 'Person' के default constructor (डिफ़ॉल्ट कंस्ट्रक्टर) को जब हम ‘virtual’ keyword का use करते हैं, तो grandparent class के default constructor को default रूप से call किया जाता है, भले ही parent classes स्पष्ट रूप से parameterized constructor को call करें।

    'Person' class के parameterized constructor को कैसे Call करें? constructor को 'TA' class में Call किया जाना है। उदाहरण के लिए, निम्नलिखित Program को consider करें।

    Example:-


        #include <iostream>
        using namespace std;
        class Person
        {
        public:
            Person(int x) { cout << "Person::Person(int ) called" << endl; }
            Person() { cout << "Person::Person() called" << endl; }
        };

        class Faculty : virtual public Person
        {
        public:
            Faculty(int x) : Person(x)
            {
                cout << "Faculty::Faculty(int ) called" << endl;
            }
        };

        class Student : virtual public Person
        {
        public:
            Student(int x) : Person(x)
            {
                cout << "Student::Student(int ) called" << endl;
            }
        };

        class TA : public Faculty, public Student
        {
        public:
            TA(int x) : Student(x), Faculty(x), Person(x)
            {
                cout << "TA::TA(int ) called" << endl;
            }
        };

        int main()
        {
            TA ta1(30);
        }

    Output:-

    Person::Person(int ) called
    Faculty::Faculty(int ) called
    Student::Student(int ) called
    TA::TA(int ) called

    Explain Example:-

    सामान्य तौर पर, grandparent के constructor को सीधे call करने की अनुमति नहीं है, इसे parent class के माध्यम से call किया जाना चाहिए। इसकी अनुमति तभी दी जाती है जब ‘virtual’ keyword का उपयोग किया जाता है।

    As an exercise, predict the output of following programs.

    Example #01:-


        #include <iostream>
        using namespace std;

        class A
        {
            int x;

        public:
            void setX(int i) { x = i; }
            void print() { cout << x; }
        };

        class B : public A
        {
        public:
            B() { setX(10); }
        };

        class C : public A
        {
        public:
            C() { setX(20); }
        };

        class D : public B, public C
        {
        };

        int main()
        {
            D d;
            d.print();
            return 0;
        }


    Example #02:-


        #include <iostream>
        using namespace std;

        class A
        {
            int x;

        public:
            A(int i) { x = i; }
            void print() { cout << x; }
        };

        class B : virtual public A
        {
        public:
            B() : A(10) {}
        };

        class C : virtual public A
        {
        public:
            C() : A(10) {}
        };

        class D : public B, public C
        {
        };

        int main()
        {
            D d;
            d.print();
            return 0;
        }


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

    Post a Comment

    0 Comments