C++ Multiple Inheritance In Hindi

📔 : C++ 🔗

किसी derived / child class द्वारा एक से ज्यादा classes को inherit करा ही multiple inheritance कहते हैं। मतलब एक बार में ही कई classes को inherit करना।

Normally कई Object Oriented Language like : PHP और Java multiple inheritance को support नहीं करती है , क्योंकि इन languages में Inheritance के लिए parent class की property / method को access करने के लिए this / self और super / parent use किया जाता है। लेकिन C++ में ऐसा नहीं है।

C++ Multiple Inheritance Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

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

// define class B.
class B{
  public : 
  void test2(){
    cout << "\ntest2 method in class B";
  }
};

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

int main() {
  // now just create the object of C class.
  C cObj;

  // call class A,B method on class C Object.
  cObj.test();
  cObj.test2();
  return 0;
}
Output
test method in class A
test2 method in class B

Constructor in Multiple Inheritance

अगर classes में Constructors defined हैं तो , वो उसी order में execute होंगे जिस order में classes को inherit किया गया है। मतलब A, B में से सबसे पहले class A का constructor run होगा then class B का उसके बाद child / derived class का।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

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

class B{
  public : 
  B(){
    cout << "Constructor in class B\n";
  }
};

class C : public A, public B{
  public : 
  C(){
    cout << "Constructor in class C";
  }
};

int main() {
  // just reate class C Object.
  C cObj;
  return 0;
}
Output
Constructor in class A
Constructor in class B
Constructor in class C

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