C++ में Hybrid Inheritance
In this page:
class Base { };
class Left : public Base { };
class Right : public Base { };
class Derived : public Left, public Right { };
Hybrid Inheritance क्या है?
Hybrid inheritance एक single class hierarchy में दो या ज़्यादा inheritance patterns combine करती है — उदाहरण के लिए, siblings के एक set के लिए hierarchical inheritance को multiple inheritance के साथ मिलाना जहाँ उन siblings में से एक एक दूसरे, unrelated base class से भी draw करता है।
उदाहरण: What is Hybrid Inheritance?
// Include std::cout and std::cin
#include <iostream>
// Define the type Base
class Base {
// Members below can be accessed from outside the class
public:
void show() { std::cout << "Base" << std::endl; }
};
// Define the type Sibling1
class Sibling1 : public Base {};
class Sibling2 : public Base {};
class Combo : public Sibling1 {
// Members below can be accessed from outside the class
public:
void extra() { std::cout << "Combo" << std::endl; }
};
// Program execution starts in main()
int main() {
Combo c;
c.show();
c.extra();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Diamond Problem का Intro
Diamond problem hybrid hierarchies के अंदर तब दोबारा surface हो सकती है जब एक class दो parents से inherit करते हुए खत्म होती है जो खुद एक common grandparent share करते हैं, उस shared ancestor तक duplicate paths और इसके data की duplicate copies produce करते हुए।
उदाहरण: The Diamond Problem Intro
#include <iostream>
class Grandparent {
public:
int value = 1;
};
class ParentA : public Grandparent {};
class ParentB : public Grandparent {};
class Grandchild : public ParentA, public ParentB {};
int main() {
Grandchild g;
// g.value would fail to compile: ambiguous, two Grandparent copies
std::cout << g.ParentA::value << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Scope Resolution से Ambiguity Resolve करना
Virtual inheritance के बिना, आप अभी भी scope resolution operator इस्तेमाल करके explicitly यह qualify करके एक diamond की वजह से हुई ambiguity resolve कर सकते हैं कि आपका मतलब किस inheritance path से है, compiler को exactly बताते हुए कि आप किस parent की member की copy refer कर रहे हैं।
उदाहरण: Resolving Ambiguity with Scope Resolution
// Include std::cout and std::cin
#include <iostream>
// Define the type Grandparent
class Grandparent {
// Members below can be accessed from outside the class
public:
int value = 5;
};
// Define the type ParentA
class ParentA : public Grandparent {};
class ParentB : public Grandparent {};
class Grandchild : public ParentA, public ParentB {};
// Program execution starts in main()
int main() {
Grandchild g;
// Print to the console with cout
std::cout << g.ParentA::value << " " << g.ParentB::value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Simple Hybrid Flow
जब एक hybrid hierarchy के inheritance paths actually एक diamond shape में overlap नहीं होते, design clean और predictable रहता है, आपको types के बीच genuinely complex real-world relationships model करने के लिए inheritance styles freely combine करने देते हुए।
उदाहरण: Simple Hybrid Flow
// Include std::cout and std::cin
#include <iostream>
// Define the type Engine
class Engine {
// Members below can be accessed from outside the class
public:
void start() { std::cout << "Engine starts" << std::endl; }
};
// Define the type Wheels
class Wheels {
// Members below can be accessed from outside the class
public:
void roll() { std::cout << "Wheels roll" << std::endl; }
};
class Car : public Engine, public Wheels {};
// Program execution starts in main()
int main() {
Car car;
car.start();
car.roll();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Call Sequence
एक hybrid hierarchy में constructor execution order एक strict, well-defined sequence follow करता है, topmost base classes से शुरू होकर और finally most-derived class के अपने constructor तक पहुँचने से पहले हर intermediate level के जरिए नीचे काम करते हुए।
उदाहरण: Constructor Call Sequence
// Include std::cout and std::cin
#include <iostream>
// Define the type Base1
class Base1 {
// Members below can be accessed from outside the class
public:
Base1() { std::cout << "Base1" << std::endl; }
};
// Define the type Base2
class Base2 {
// Members below can be accessed from outside the class
public:
Base2() { std::cout << "Base2" << std::endl; }
};
class Derived : public Base1, public Base2 {
public:
Derived() { std::cout << "Derived" << std::endl; }
};
// Program execution starts in main()
int main() {
Derived d;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
virtualके बिना दो paths से inherit करते समय grandparent class की दो copies के साथ खत्म होना।- shared base के एक member को access करते समय ambiguity error ignore करना।
- diamond problem solve करने के लिए virtual inheritance इस्तेमाल न करना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: