← Back to C++ Course | Chapter 9: Inheritance | Lesson 7 of 8

C++ में Hybrid Inheritance

Hybrid inheritance एक family tree में कई तरह की inheritance मिलाती है, कई connections वाले एक family की तरह। यह एक diamond shape बना सकती है जिसे careful handling चाहिए।
Syntax
cpp
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?

cpp
// 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;
}

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

cpp
#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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. virtual के बिना दो paths से inherit करते समय grandparent class की दो copies के साथ खत्म होना।
  2. shared base के एक member को access करते समय ambiguity error ignore करना।
  3. diamond problem solve करने के लिए virtual inheritance इस्तेमाल न करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.