Inheritance in C++ -W3 Coding Club

    एक Class की दूसरे Class की properties और characteristics को derive (प्राप्त) करने की क्षमता को Inheritance कहा जाता है । Inheritance Object-Oriented Programming(ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग) की सबसे महत्वपूर्ण विशेषताओं में से एक है।

        ● Sub Class: वह class जो किसी अन्य Class से properties inherits (प्राप्त) करती है उसे Sub Class या Derived class कहा जाता है।

        ● Super Class: जिस Class  से   properties Derived Class  द्वारा inherits (प्राप्त) करती है , उसे  Base या Super Class कहा जाता है।

    inheritance का उपयोग क्यों और कब करें?

    vehicles के एक Group को Consider करते हैं । आपको Bus, Car और truck के लिए Class बनानी होंगी। सभी तीन Class के लिए fuelAmount(), capacity(), applyBrakes() Method समान होंगे। यदि हम इन Classes को inheritance से बचते हुए बनाते हैं तो हमें इन सभी फंगक्शन को तीनों Classes में से प्रत्येक में लिखना होगा जैसा कि नीचे दिखाया गया है:

    आप clearly देख सकते हैं कि उपरोक्त process के परिणामस्वरूप एक ही Code का 3 बार लिखा गया है। इससे error और data redundancyकी संभावना बढ़ जाती है। इस प्रकार की स्थिति से बचने के लिए inheritance का उपयोग किया जाता है। यदि हम एक Class vehicle बनाते हैं और उसमें इन तीन Function को लिखते हैं और बाकी Class को vehicle Class से inherit करते हैं, तो हम केवल Data के duplication से बच सकते हैं और re-usability को increase कर सकते हैं। 

    नीचे दिए गए diagramको देखें जिसमें तीन classes vehicle class से विरासत में मिले हैं:

    inheritance, w3 coding club, w3codingclub

    inheritance का उपयोग करते हुए, हमें Function को तीन बार के बजाय केवल एक बार लिखना होता है क्योंकि हमें बाकी तीनों क्लास Base Class (Vehicle) से inherit में मिली है।

    ● Implementing inheritance in C++: Base Class(बेस क्लास) से inherit(इनहेरिट) किए गए Sub-class(सब-क्लास) को बनाने के लिए हमें नीचे दिए गए syntax का Use करना होगा।

    Syntax 

    class subclass_name : access_mode base_class_name
    {
      // body of subclass
    };

    यहां, subclass_name subclass का नाम है, access_mode वह Mode है जिसमें आप Subclass को inherit करना चाहते हैं उदाहरण के लिए public, private, आदि और base_class_name उस Base Class का नाम है जिससे आप Subclass को inherit (इनहेरिट) करना चाहते हैं।

    Note:- एक derived class, private data members को access नहीं कर सकती । हालांकि, यह एक पूर्ण parent object को access नहीं कर सकती है, जिसमें कोई भी private members होता है जिसे वह Class declares करता है।



        // C++ program to demonstrate implementation
        // of Inheritance

        #include <iostream>
        #include <stdio.h>
        using namespace std;

        // Base class
        class Parent
        {
        public:
            int id_p;
        };

        // Sub class inheriting from Base Class(Parent)
        class Child : public Parent
        {
        public:
            int id_c;
        };

        // main function
        int main()
        {
            Child obj1;

            // An object of class child has all data members
            // and member functions of class parent
            obj1.id_c = 7;
            obj1.id_p = 91;
            cout << "Child id is: " << obj1.id_c << '\n';
            cout << "Parent id is: " << obj1.id_p << '\n';

            return 0;
        }

    Output:-

    Child id is: 7
    Parent id is: 91

    Explain Example:-

    उपरोक्त Program में, 'Child' Class सार्वजनिक रूप से 'Parent' Class से inherit में मिला है, इसलिए 'Parent' Class के public data members को भी 'Child' Class द्वारा inherit में लिया जाएगा।

    Modes of Inheritance 

    Inheritance के 3 तरीके हैं।

    1. Public Mode : यदि हम Public Base Class से Subclass प्राप्त करते हैं। तब Base Class का Public Member Derived Class में Public हो जाएगा और Base Class के protected Member Derived Class में protected हो जाएंगे।
    2. Protected Mode : यदि हम एक Protected Base Class से Sub Class प्राप्त करते हैं। तब Base Class के Public Members और Protected Members दोनों Derived Class में Protected हो जाएंगे।
    3. Private Mode : यदि हम एक privet Base Class से Subclass बनाते हैं तो Base Class के Public Members और Protected Members दोनों Derived Class में Private हो जाएंगे।

    Note:- Base Class में private members को directly derived class में access नहीं किया जा सकता है, जबकि protected members को directly access किया जा सकता है। उदाहरण के लिए, Class B, C, और D सभी में नीचे दिए गए उदाहरण में variables x, y, और z शामिल हैं।


        // C++ Implementation to show that a derived class
        // doesn’t inherit access to private data members.
        // However, it does inherit a full parent object.
        class A
        {
        public:
            int x;

        protected:
            int y;

        private:
            int z;
        };

        class B : public A
        {
            // x is public
            // y is protected
            // z is not accessible from B
        };

        class C : protected A
        {
            // x is protected
            // y is protected
            // z is not accessible from C
        };

        class D : private A // 'private' is default for classes
        {
            // x is private
            // y is private
            // z is not accessible from D
        };

    नीचे दी गई table उपरोक्त तीन modes को summarizes करती है और public, protected और private modes में व्युत्पन्न होने पर Subclass में Base Class के members के access specifier को दिखाती है:

    Inheritance in C++, w3 Coding club, w3codingclub

    Types of Inheritance in C++

    1. Single Inheritance(सिंगल इनहेरिटेंस) : -

    Single Inheritance(सिंगल इनहेरिटेंस) में, एक Class को केवल एक ही Class से Inherit(इनहेरिट) करने की अनुमति होती है। यानी एक Base-Class ko एक ही subclass Inherit(इनहेरिट) कर सकती हैं।

    Single Inheritance, w3 coding club, w3codingclub

    Syntax

    class subclass_name : access_mode base_class
    {
      // body of subclass
    };

        // C++ program to explain
        // Single inheritance
        #include <iostream>
        using namespace std;

        // base class
        class Vehicle
        {
        public:
            Vehicle()
            {
                cout << "This is a Vehicle\n";
            }
        };

        // sub class derived from a single base classes
        class Car : public Vehicle
        {
        };

        // main function
        int main()
        {
            // Creating object of sub class will
            // invoke the constructor of base classes
            Car obj;
            return 0;
        }

    Output:-

    This is a Vehicle

    2. Multiple Inheritance:-

    Multiple Inheritance(मल्टीपल इनहेरिटेंस) C++ का एक feature है जहां एक Class एक से अधिक Class से इनहेरिट कर सकती है। यानी एक sub class एक से अधिक Base Class से मिलकर बनती है ।

    Multiple Inheritance, W3 coding club, w3codingclub

    Syntax

    class subclass_name : access_mode base_class1, access_mode base_class2, ....
    {
      // body of subclass
    };

    यहां, Base Classes संख्या को comma (',') से separate किया जाएगा और प्रत्येक Base Class के लिए Access Mode specified किया जाना चाहिए।


        // C++ program to explain
        // multiple inheritance
        #include <iostream>
        using namespace std;

        // first base class
        class Vehicle
        {
        public:
            Vehicle() { cout << "This is a Vehicle\n"; }
        };

        // second base class
        class FourWheeler
        {
        public:
            FourWheeler()
            {
                cout << "This is a 4 wheeler Vehicle\n";
            }
        };

        // sub class derived from two base classes
        class Car : public Vehicle, public FourWheeler
        {
        };

        // main function
        int main()
        {
            // Creating object of sub class will
            // invoke the constructor of base classes.
            Car obj;
            return 0;
        }

    Output:-

    This is a Vehicle
    This is a 4 wheeler Vehicle


    3. Multilevel Inheritance:

    इस प्रकार के inheritance में एक derived class दूसरे derived class से create की जाती हैं।

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

    Example:-


        // C++ program to implement
        // Multilevel Inheritance
        #include <iostream>
        using namespace std;

        // base class
        class Vehicle
        {
        public:
            Vehicle() { cout << "This is a Vehicle\n"; }
        };

        // first sub_class derived from class vehicle
        class fourWheeler : public Vehicle
        {
        public:
            fourWheeler()
            {
                cout << "Objects with 4 wheels are vehicles\n";
            }
        };
        // sub class derived from the derived base class fourWheeler
        class Car : public fourWheeler
        {
        public:
            Car() { cout << "Car has 4 Wheels\n"; }
        };

        // main function
        int main()
        {
            // Creating object of sub class will
            // invoke the constructor of base classes.
            Car obj;
            return 0;
        }

    Output:-

    This is a Vehicle
    Objects with 4 wheels are vehicles
    Car has 4 Wheels

    4. Hierarchical Inheritance:-

    इस प्रकार की inheritance में, एक से अधिक subclass एक ही Base Class से inherit करके बनाई जाती हैं। यानी एक से अधिक derived class, single base class से बनायी जाते हैं।

    Hierarchical Inheritance, W3 Coding Club, w3codingclub

    Example:-


        // C++ program to implement
        // Hierarchical Inheritance
        #include <iostream>
        using namespace std;

        // base class
        class Vehicle
        {
        public:
            Vehicle() { cout << "This is a Vehicle\n"; }
        };

        // first sub class
        class Car : public Vehicle
        {
        };

        // second sub class
        class Bus : public Vehicle
        {
        };

        // main function
        int main()
        {
            // Creating object of sub class will
            // invoke the constructor of base class.
            Car obj1;
            Bus obj2;
            return 0;
        }

    Output:-

    This is a Vehicle
    This is a Vehicle

    5. Hybrid (Virtual) Inheritance:-

    Hybrid Inheritance(हाइब्रिड इनहेरिटेंस) एक से अधिक प्रकार के Inheritance(इनहेरिटेंस) को मिलाकर लागू किया जाता है। उदाहरण के लिए: Combining Hierarchical inheritance और Multiple Inheritance का combination ।

    नीचे दी गई छवि Combining Hierarchical inheritance और multiple inheritances के combination को दर्शाती है:

    Hybrid (Virtual) Inheritance, W3 coding club, w3codingclub

    Example:-


        // C++ program for Hybrid Inheritance

        #include <iostream>
        using namespace std;

        // base class
        class Vehicle
        {
        public:
            Vehicle() { cout << "This is a Vehicle\n"; }
        };

        // base class
        class Fare
        {
        public:
            Fare() { cout << "Fare of Vehicle\n"; }
        };

        // first sub class
        class Car : public Vehicle
        {
        };

        // second sub class
        class Bus : public Vehicle, public Fare
        {
        };

        // main function
        int main()
        {
            // Creating object of sub class will
            // invoke the constructor of base class.
            Bus obj2;
            return 0;
        }

    Output:-

    This is a Vehicle
    Fare of Vehicle

    6. A special case of hybrid inheritance: Multipath inheritance:

    दो derived classes के साथ एक base class और इन दो base classes में एक common base classes होती हैं
    जिसे multipath inheritance कहा जाता है। इस प्रकार की inheritance में Ambiguity उत्पन्न हो सकती है।

    Example:-


        // C++ program demonstrating ambiguity in Multipath
        // Inheritance

        #include <iostream>
        using namespace std;

        class ClassA
        {
        public:
            int a;
        };

        class ClassB : public ClassA
        {
        public:
            int b;
        };

        class ClassC : public ClassA
        {
        public:
            int c;
        };

        class ClassD : public ClassB, public ClassC
        {
        public:
            int d;
        };

        int main()
        {
            ClassD obj;

            // obj.a = 10;               // Statement 1, Error
            // obj.a = 100;              // Statement 2, Error

            obj.ClassB::a = 10;  // Statement 3
            obj.ClassC::a = 100; // Statement 4

            obj.b = 20;
            obj.c = 30;
            obj.d = 40;

            cout << " a from ClassB : " << obj.ClassB::a;
            cout << "\n a from ClassC : " << obj.ClassC::a;

            cout << "\n b : " << obj.b;
            cout << "\n c : " << obj.c;
            cout << "\n d : " << obj.d << '\n';
        }

    Output:-

    a from ClassB : 10
    a from ClassC : 100
    b : 20
    c : 30
    d : 40

    Explain Example:-

    उपरोक्त उदाहरण में, ClassB और ClassC दोनों ClassA को Inherit करते हैं, उन दोनों के पास ClassA की एक ही copy है। हालाँकि class-D को ClassB और ClassC दोनों Inherit में मिलते हैं, इसलिए Class-D में ClassA की दो Copies हैं, एक Class-B से और दूसरी Class-C से।

    यदि हमें Class-D के Object के माध्यम से Class-A के data member तक पहुंचने की आवश्यकता है, तो हमें उस path को specify करना होगा जिससे a access किया जाएगा, चाहे वह classB या classC से हो, क्योंकि compiler classA की दो Copies के बीच differentiate नहीं कर सकता है।

    इस Ambiguity से बचने के 2 तरीके हैं:

    1) scope resolution operator का उपयोग करके ambiguity को Avoid :-

    scope resolution operator(स्कोप रेजोल्यूशन ऑपरेटर) का उपयोग करके हम manually उस path को specify कर सकते हैं जिससे data member 'a' को access किया जाएगा, जैसा कि उपरोक्त उदाहरण में statements 3 और 4 में दिखाया गया है।

        
        obj.ClassB::a = 10;  // Statement 3
        obj.ClassC::a = 100;     // Statement 4

    Note:- Still, there are two copies of ClassA in Class-D.


    2) Avoiding ambiguity using the virtual base class:


        #include <iostream>
        using namespace std;
        class ClassA
        {
        public:
            int a;
        };

        class ClassB : virtual public ClassA
        {
        public:
            int b;
        };

        class ClassC : virtual public ClassA
        {
        public:
            int c;
        };

        class ClassD : public ClassB, public ClassC
        {
        public:
            int d;
        };

        int main()
        {
            ClassD obj;

            obj.a = 10;  // Statement 3
            obj.a = 100; // Statement 4

            obj.b = 20;
            obj.c = 30;
            obj.d = 40;

            cout << "\n a : " << obj.a;
            cout << "\n b : " << obj.b;
            cout << "\n c : " << obj.c;
            cout << "\n d : " << obj.d << '\n';
        }

    Output:-

    a : 100
    b : 20
    c : 30
    d : 40

    Explain Example:-

    उपरोक्त उदाहरण के अनुसार, Class-D में ClassA की केवल एक Copy है, इसलिए statement 4 statement, 3 में दिए गए 'a' के मान को overwrite कर देगा।

    Post a Comment

    2 Comments