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

C++ में Multiple Inheritance

Multiple inheritance तब है जब एक class एक से ज़्यादा parent से सीखती है, mom और dad दोनों से skills लेने वाले एक child की तरह। जब दोनों के पास same चीज़ हो तो यह confusion cause कर सकता है।
Syntax
cpp
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

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

Constructor Call Order

कई base classes के साथ, उनके constructors exactly उसी order में चलते हैं जिसमें वे base classes derived class के inheritance declaration में list की गई हैं, derived constructor की initializer list उन्हें जिस order में लिखे उस order में नहीं।

उदाहरण: Constructor Call Order

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

Ambiguity Problem

अगर दोनों base classes में से हर एक same नाम वाला एक member function declare करे, derived class पर वह नाम call करना ambiguous हो जाता है, और compiler guess करने से मना कर देता है कि आपका मतलब कौन सा था — जब तक आप explicitly qualify न करें कि आपको किस base का version चाहिए तब तक एक compile error मिलती है।

उदाहरण: The Ambiguity Problem

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

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

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

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

cpp
// 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;
}
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. दोनों base classes के define किए एक member को बिना qualify किए call करना, जो ambiguous है।
  2. diamond problem ignore करना, जो most-derived class को एक shared base की दो copies देता है।
  3. ambiguity resolve करने के लिए A::show() जैसे base class नाम से qualify न करना।

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.