Types of inheritance single, multiple, multilevel, hybrid c++ In Hindi

Types of inheritance single, multiple, multilevel, hybrid c++ In Hindi

C++ ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग OOPS कांसेप्ट में इनहेरिटेंस एक सबक्लास (डिराइव्ड क्लास) को दूसरी अन्य मैन रुट सुपर क्लास (बेस क्लास) की प्रॉपर्टीज़ और उसके डिफ़ॉल्ट बिहेवियर को डिराइव्ड क्लास या सबक्लास्सेस में यूज़ अप्लाई करने की परमिशन प्रोवाइड करते  है। C++ प्रोग्रामिंग मल्टीप्ल टाइप के इनहेरिटेंस कांसेप्ट को सपोर्ट करता है. जिसमे सिंगल इनहेरिटेंस, मल्टीपल इनहेरिटेंस, मल्टीलेवल इनहेरिटेंस,

Types of inheritance single, multiple, multilevel, hybrid c++ In Hindi

हाइब्रिड इनहेरिटेंस, आदि है. इनका C++ में यूज़ रुट और सबक्लास क्लास के बीच अलग-अलग रिलेशनशिप मॉडल को रिप्रेजेंट करने में किया जाता है।

Key functions and features of the inheritance concept in C++.

  • सिंगल इनहेरिटेंस।
  • मल्टीपल इनहेरिटेंस।
  • मल्टीलेवल इनहेरिटेंस।
  • हाइब्रिड इनहेरिटेंस।

So, let’s learn more about each type of inheritance, its functions, features, and concepts in C++, with detailed examples and explanations.

Single inheritance concept in C++.

C++ प्रोग्राम में सिंगल इनहेरिटेंस कांसेप्ट में एक सबक्लास क्लास (डिराइव्ड क्लास) एक सिंगल बेस या रुट (पैरेंट) क्लास से इनहेरिट होती है। यह एक सिंगल बेसिक इनहेरिटेंस का सबसे बेसिक और कॉमन मेथड है. जिसमे एक सबक्लास डायरेक्ट पैरेंट या दूसरी क्लास से डिराइव्ड होती है।

Example of the single inheritance concept.

#include <iostream>

#include <string>

class Course {  // here we define a course name Base class

public:

    void select() {

        std::cout << “Select your desire course” << std::endl;

    }

};

class Cplus : public Course {  // Cplus Derived class define here

public:

    void selection() {

        std::cout << “C++ course selection” << std::endl;

    }

};

int main() {

    Cplus testCplus;

    testCplus.select();  // here it Inherited from Course base class

    testCplus.selection(); // here it Defined in Cplus class base class

    return 0;

}

Explanation of the single inheritance concept.

  • यहाँ सिंगल इनहेरिटेंस एक्साम्प्ल में Course एक बेस या रुट क्लास है. जिसमें select() मेथड डिफाइन है।
  • इसी तरह Cplus एक डिराइव्ड क्लास क्रिएटेड है. जो पब्लिक इनहेरिटेंस का यूज़ करके Course बेस क्लास से इनहेरिट करता है।
  • Cplus क्लास बेस क्लास से select() मेथड को एक्सेस कर सकता है, और यह अपना एक कस्टम क्लास मेथड, selection() भी डिफाइन कर सकता है।

Multiple inheritance concept in C++.

C++ प्रोग्राम में मल्टीपल इनहेरिटेंस कांसेप्ट में एक यूजर डिफाइन सबक्लास क्लास (डिराइव्ड क्लास) एक से ज़्यादा बेस या पैरेंट क्लास से इनहेरिट होती है। यह एक यूजर डिफाइन डिराइव्ड क्लास को कई रुट या बेस क्लास से क्लास मेंबर पैरामीटर एट्रिब्यूट और बिहेवियर प्रॉपर्टीज को इनहेरिट करने की परमिशन प्रोवाइड करता है।

Example of the multiple inheritance concept.

#include <iostream>

class Course {  // Course name Base or root class created class 1

public:

    void select() {

        std::cout << “Select your desire course” << std::endl;

    }

};

class Cplus {  // Cplus name Base or root class created class 2

public:

    void Cselection() {

        std::cout << “C++ course selection” << std::endl;

    }

};

class Java : public Course, public Cplus {  // Derived class created from both Course and Cplus class

public:

    void Jselection() {

        std::cout << “Java course selection” << std::endl;

    }

};

int main() {

    Java testJava;

    testJava.select();   // here it Inherited from Course base class 1

    testJava.Cselection(); // here it Inherited from Cplus base class 2

    testJava.Jselection();  // here it Defined in Java Derived class

    return 0;

}

Explanation of the multiple inheritance concept.

  • यहाँ मल्टीप्ल इनहेरिटेंस एक्साम्प्ल में Course और Cplus दोनों बेस या पैरेंट क्लास के रूप में डिफाइन हैं।
  • यहाँ Java एक डिराइव्ड क्लास के रूप में डिफाइन है, जो मल्टीपल इनहेरिटेंस कांसेप्ट का यूज़ करके Course और Cplus दोनों क्लास से क्लास बिहेवियर और प्रॉपर्टीज को इनहेरिट करता है।
  • Java क्लास अपने Jselection() मेथड के अलावा Course (जैसे, select() और Cplus Cselection()) (जैसे, Jselection()) दोनों से मेथड को एक्सेस कर सकता है।

Potential issues with using multiple inheritance in C++.

डायमंड प्रॉब्लम यदि किसी यूजर डिफाइन क्लास में दो या ज़्यादा बेस क्लास का एक कॉमन बेस क्लास डिक्लेअर किए गए है. तो उस क्लास में डिफाइन डिराइव्ड या सबक्लास एक ही रुट या बेस क्लास से कई बार इनहेरिट हो सकती है। इससे मौजूदा क्लास फंक्शन मेथड कॉल में एम्बिगुइटी इश्यूज डेवलप हो सकती है।

इस इश्यूज को सॉल्व करने के लिए, C++ प्रोग्राम में वर्चुअल इनहेरिटेंस जिसे बाद में यूज़ किया गया है, जिससे की यह फिक्स हो सके कि जरूरत पड़ने पर डिराइव्ड क्लास को एक कॉमन बेस क्लास के डेटा और मेथड की सिर्फ़ एक कॉपी ही मिल सके।

Multilevel inheritance concept in C++.

C++ प्रोग्राम में मल्टीलेवल इनहेरिटेंस कांसेप्ट में एक यूजर डिफाइन क्लास (डिराइव्ड क्लास) दूसरे अन्य सबक्लास या डिराइव्ड क्लास से इनहेरिट होती है. जिससे एक से अधिक मल्टी-लेवल डिराइव्ड क्लास कांसेप्ट इनहेरिटेंस की एक चेन हायरार्की क्रिएट होती है। इस कंडीशन में, एक डिराइव्ड क्लास दूसरे डिराइव्ड क्लास के लिए एक बेस क्लास के रूप में यूज़ हो सकती है।

Example of multilevel inheritance concept.

#include <iostream>

class Course {  // here Course name Base or root class created

public:

    void select() {

        std::cout << “Select your desire course” << std::endl;

    }

};

class Cplus : public Course {  // Cplus name Derived class 1 from Course class

public:

    void Cselection() {

        std::cout << “C++ course selection” << std::endl;

    }

};

class Java : public Cplus {  // Java name Derived class 2 from Cplus class

public:

    void Jselection() {

        std::cout << “Java course selection” << std::endl;

    }

};

int main() {

    Java testJava;

    testJava.select();   // here it Inherited from Course main base class

    testJava.Cselection(); // here it Inherited from Cplus derived class

    testJava.Jselection();  // here it Defined in Java derived class

    return 0;

}

Explanation of multilevel inheritance concept.

  • यहाँ मल्टीप्ल इनहेरिटेंस एक्साम्प्ल में Course एक बेस या रुट क्लास डिफाइन है।
  • Cplus, Course से डिराइव्ड कनेक्टेड एक सबक्लास है।
  • Java, Cplus से डिराइव्ड एक क्लास है, जो अपने आप में Course क्लास से इनहेरिट होती है।
  • Java क्लास Cplus (जैसे, Cselection()) और Course (जैसे, select()) दोनों क्लास से इसके बिहैवियर प्रॉपर्टीज और मेथड को एक्सेस कर सकता है।

Hybrid inheritance concept in C++.

C++ प्रोग्राम में हाइब्रिड इनहेरिटेंस कांसेप्ट में एक यूजर डिफाइन क्लास मल्टीप्ल इंडिविजुअल टाइप के इनहेरिटेंस (जैसे, मल्टीपल, मल्टीलेवल, या सिंगल इनहेरिटेंस का एक ग्रुप कॉम्बिनेशन) का यूज़ करके एक से ज़्यादा क्लास से बिहैवियर प्रॉपर्टीज और मेथड को इनहेरिट करता है।

Example of the hybrid inheritance concept.

#include <iostream>

class Course {  // Course Base or root class created class 1

public:

    void select() {

        std::cout << “Select your desire course” << std::endl;

    }

};

class Cplus {  // Cplus Base or root class created class 2

public:

    void Cselection() {

        std::cout << “C++ course selection” << std::endl;

    }

};

class BasicCourse {  // BasicCourse Base or root class created class 3

public:

    void Bcourse() {

        std::cout << “Basic course selection” << std::endl;

    }

};

class Java : public Course, public Cplus, public BasicCourse {  // here it Derived class from multiple above Course, Cplus, BasicCourse class bases

public:

    void Jselection() {

        std::cout << “Java course selection” << std::endl;

    }

};

int main() {

    Java testJava;

    testJava.select();    // here it Inherited from Course main base class

    testJava.Cselection();  // here it Inherited from Cplus main base class

    testJava.Bcourse();   // here it Inherited from BasicCourse main base class

    testJava.Jselection(); //here it Defined in Java derived class

    return 0;

}

Explanation of the hybrid inheritance concept.

  • यहाँ हाइब्रिड इनहेरिटेंस एक्साम्प्ल में Course, Cplus, और BasicCourse 3 अलग अलग बेस या रुट क्लास डिफाइन हैं।
  • जिसमे Java अपने आप में Course, Cplus, और BasicCourse की एक डिराइव्ड क्लास है. जो तीनों बेस क्लास से इसके बिहैवियर प्रॉपर्टीज और मेथड को इनहेरिट करता है।
  • Java क्लास अपने Jselection() मेथड के अलावा तीनों बेस क्लास (select(), Cselection(), Bcourse()) से क्लास मेथड को एक्सेस और इनहेरिट कर सकता है।

Leave a Reply