If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
किसी 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++ में ऐसा नहीं है।
#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;
}
test method in class A test2 method in class B
अगर classes में Constructors defined हैं तो , वो उसी order में execute होंगे जिस order में classes को inherit किया गया है। मतलब A, B में से सबसे पहले class A का constructor run होगा then class B का उसके बाद child / derived class का।
#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;
}
Constructor in class A Constructor in class B Constructor in class C
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook