C++ Inheritance In Hindi

📔 : C++ 🔗

किसी class को या Class की properties / behavior को extend करना Inheritance कहते हैं। जिस class को extend किया गया है उसे Parent / Base Class , जिस class के द्वारा extend किया गया है उसे Child / Derived Class कहते हैं। Extend करने पर Child Class में लगभग बो सभी properties / methods होंगे जो Parent Class में हैं, हालाँकि यह Parent Class के Access Specifiers पर depend करता है कि बो Properties Accessible हैं या नहीं ।


C++ में किसी class को inherit करने के लिए colon ( :  ) का use किया जाता है।

C++ Types of Inheritance

C+ मुख्य रूप से 4 Types की Inheritance को support करती है -

  1. Single Inheritance
  2. Hierarchical Inheritance
  3. Multilevel Inheritance
  4. Multiple Inheritance

C++ Single Inheritance

Inheritance का यह सबसे easy way है , जिसमे किसी single class को किसी single class के द्वारा Inherit किया जाता है।

C++ Inheritance Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  // define a property as protected so that we can access it's child class.
  protected : 
    string property_a = "Class A Property";

  // define a public method.
  public :
    void test(){
      cout << "\ntest method in class A";
    }
};

// define another class that inherits A class.
class B : public A{
  // we can access Class A's property_a only inside class.
  public : 
    B(){
      cout << property_a;
    }
};

int main() {
  // just create class B object.
  B bObj;

  // call class A method on class B Object.
  bObj.test();
  return 0;
}
Output
Class A Property
test method in class A

Explain : ऊपर दिए गए example में Class A को class B द्वारा inherit किया गया है , और आप clearly देख सकते हैं कि class A की सभी protected / public properties / methods को B class से भी access किया जा सकता है।

Important

Base / Parent class की सभी public properties / methods को publically (class के बाहर) access करने के लिए आपको inherit करते समय colon के बाद public use करना पड़ेगा , जैसे कि ऊपर दोनों examples में दिखाया गया है। बिना public use किये inherit करके parent class की public properties को access करने पर कुछ ऐसी error आती है।


For Example , ऊपर दिए गए example में A class extend करते समय अगर public न लगाएं तो कुछ इस तरह से error आएगी।

class B : A{
  // block of code
}

In function 'int main()':
31:13: error: 'void A::test()' is inaccessible within this context
31 |   bObj.test();
   |
31:13: error: 'A' is not an accessible base of 'B'
31 |   bObj.test();

? Inheritance में Child Class के द्वारा Parent Class की सिर्फ public & protected Properties / Methods को access किया जा सकता है , private नहीं। By default Class की सभी Properties / Methods private होते हैं।


जब भी किसी class को inherit करके child / derived class का object बनाते हैं , तो सभी parent / base classes का constructor automatically run हो जाता है। लेकिन उसके लिए सभी parent classes में constructor public define होना चाहिए।
Example :

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  // define constructor.
  public :
  A(){
    cout << "Class A constructor \n";
  }
};

class B : public A{
  //  define constructor for class B.
  public :
  B(){
    cout << "Class B constrcutor";    
  }
};

int main() {
  // just create class B object.
  B bObj;
  return 0;
}
Output
Class A constructor 
Class B constrcutor

C++ Hierarchical Inheritance

यह भी normal inheritance ही है, इस type के Inheritance में किसी single class को एक से ज्यादा Classes के द्वारा Inherit किया जाता है।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  // define a public method.
  public :
  void test(){
    cout << "test method in class A. \n";
  }
};

// define class B that inherits class A.
class B : public A{
};

// define another class that also inherits A.
class C : public A{
};

int main() {
  /*
  * Now class B,C both have the A class methos test.
  */
  B bObj;
  C cObj;

  cObj.test();
  bObj.test();
  return 0;
}
Output
test method in class A. 
test method in class A.

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers