C++ में Multiple Inheritance
In this page:
class Base1 { };
class Base2 { };
class Derived : public Base1, public Base2 { };
Multiple Inheritance का Introduction
Multiple inheritance तब होता है जब एक single derived class एक साथ एक से ज़्यादा base class से inherit करे, इसे कई unrelated sources से behavior को एक type में combine करने देते हुए — एक capability जिसे कई दूसरी OOP languages जानबूझकर omit करती हैं।
उदाहरण: Introduction to Multiple Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Flyable
class Flyable {
// Members below can be accessed from outside the class
public:
void fly() { std::cout << "Flying" << std::endl; }
};
// Define the type Swimmable
class Swimmable {
// Members below can be accessed from outside the class
public:
void swim() { std::cout << "Swimming" << std::endl; }
};
class Duck : public Flyable, public Swimmable {};
// Program execution starts in main()
int main() {
Duck d;
d.fly();
d.swim();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Call Order
कई base classes के साथ, उनके constructors exactly उसी order में चलते हैं जिसमें वे base classes derived class के inheritance declaration में list की गई हैं, derived constructor की initializer list उन्हें जिस order में लिखे उस order में नहीं।
उदाहरण: Constructor Call Order
// Include std::cout and std::cin
#include <iostream>
// Define the type A
class A {
// Members below can be accessed from outside the class
public:
A() { std::cout << "A first" << std::endl; }
};
// Define the type B
class B {
// Members below can be accessed from outside the class
public:
B() { std::cout << "B second" << std::endl; }
};
class C : public A, public B {};
// Program execution starts in main()
int main() {
C c;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Ambiguity Problem
अगर दोनों base classes में से हर एक same नाम वाला एक member function declare करे, derived class पर वह नाम call करना ambiguous हो जाता है, और compiler guess करने से मना कर देता है कि आपका मतलब कौन सा था — जब तक आप explicitly qualify न करें कि आपको किस base का version चाहिए तब तक एक compile error मिलती है।
उदाहरण: The Ambiguity Problem
#include <iostream>
class A {
public:
void show() { std::cout << "A::show" << std::endl; }
};
class B {
public:
void show() { std::cout << "B::show" << std::endl; }
};
class C : public A, public B {};
int main() {
C c;
// c.show(); would fail to compile: ambiguous between A::show and B::show
c.A::show();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Diamond Problem का Introduction
Diamond problem तब होती है जब एक derived class दो base classes से inherit करती है जो दोनों, बदले में, same grandparent class से inherit करती हैं।
Special handling के बिना, derived class उस shared grandparent के data की दो अलग copies के साथ खत्म होती है।
उदाहरण: Diamond Problem Introduction
#include <iostream>
class Grandparent {
public:
int value = 1;
};
class ParentA : public Grandparent {};
class ParentB : public Grandparent {};
class Child : public ParentA, public ParentB {};
int main() {
Child c;
// c.value would fail to compile: two ambiguous copies of Grandparent
std::cout << c.ParentA::value << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Member Access से Ambiguity Solve करना
आप derived class में छोटे helper functions लिखकर base classes के बीच name ambiguity resolve कर सकते हैं जो explicitly एक call को एक specific base के version पर redirect करते हैं, scope resolution operator इस्तेमाल करके यह disambiguate करते हुए कि आपका मतलब किस parent के member से है।
उदाहरण: Solving Ambiguity with Member Access
// Include std::cout and std::cin
#include <iostream>
// Define the type A
class A {
// Members below can be accessed from outside the class
public:
void show() { std::cout << "A::show" << std::endl; }
};
// Define the type B
class B {
// Members below can be accessed from outside the class
public:
void show() { std::cout << "B::show" << std::endl; }
};
class C : public A, public B {
public:
void showA() { A::show(); }
};
// Program execution starts in main()
int main() {
C c;
c.showA();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- दोनों base classes के define किए एक member को बिना qualify किए call करना, जो ambiguous है।
- diamond problem ignore करना, जो most-derived class को एक shared base की दो copies देता है।
- ambiguity resolve करने के लिए
A::show()जैसे base class नाम से qualify न करना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: