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

C++ में Multilevel Inheritance

Multilevel inheritance grandparent, parent, और child जैसे parents की एक chain है, जहाँ हर level चीज़ें नीचे pass करता है। सबसे youngest को उन सबसे features मिलते हैं।
Syntax
cpp
class Grandparent { };
class Parent : public Grandparent { };
class Child : public Parent { };

Multilevel Inheritance समझना

Multilevel inheritance में, एक derived class खुद एक और derived class के लिए base class बन जाती है, single inheritance के simple one-level relationship की बजाय एक chain — grandparent, parent, grandchild — बनाते हुए।

उदाहरण: Understanding Multilevel Inheritance

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:
	void greet() { std::cout << "Grandparent" << std::endl; }
};

// Define the type Parent
class Parent : public Grandparent {};
class Child : public Parent {};

// Program execution starts in main()
int main() {
	Child c;
	c.greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Constructor Call Sequence

इस chain के साथ constructors strictly top-down चलते हैं: grandparent पहले, फिर parent, फिर grandchild, क्योंकि हर level इसके ऊपर वाले level के पूरी तरह initialized होने पर depend करता है इससे पहले कि यह इसके ऊपर build कर सके।

Destructors फिर exact opposite order में चलते हैं।

उदाहरण: Constructor Call Sequence

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:
	Grandparent() { std::cout << "Grandparent" << std::endl; }
};

// Define the type Parent
class Parent : public Grandparent {
// Members below can be accessed from outside the class
public:
	Parent() { std::cout << "Parent" << std::endl; }
};

class Child : public Parent {
public:
	Child() { std::cout << "Child" << std::endl; }
};

// Program execution starts in main()
int main() {
	Child c;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multilevel Chain में Parameter Passing

एक grandchild class हर intermediate constructor की initializer list से गुज़ारते हुए initialization parameters को chain में ऊपर forward कर सकती है, bottom पर एक single call को grandparent level तक defined state configure करने देते हुए।

उदाहरण: Parameter Passing in Multilevel Chain

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;
	Grandparent(int v) : value(v) {}
};

// Define the type Parent
class Parent : public Grandparent {
// Members below can be accessed from outside the class
public:
	Parent(int v) : Grandparent(v) {}
};

class Child : public Parent {
public:
	Child(int v) : Parent(v) {}
};

// Program execution starts in main()
int main() {
	Child c(42);
	// Print to the console with cout
	std::cout << c.value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multilevel Inheritance में Member Access

Topmost class में declared members bottommost class तक पूरी तरह accessible रहते हैं, जब तक chain की हर link ने उनकी visibility को public या protected preserve किया हो इसे रास्ते में private तक narrow करने की बजाय।

उदाहरण: Member Access in Multilevel Inheritance

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 = 100;
};

// Define the type Parent
class Parent : public Grandparent {};
class Child : public Parent {};

// Program execution starts in main()
int main() {
	Child c;
	// Print to the console with cout
	std::cout << c.value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Real-World Multilevel Scenarios

Multilevel inheritance naturally उन चीज़ों को model करती है जो real world में पहले से एक hierarchy बनाती हैं, जैसे एक general Employee class जो एक Manager class में refine होती है, जो फिर अपनी additional responsibilities वाली एक SeniorManager class में आगे refine होती है।

उदाहरण: Real-World Multilevel Scenarios

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Employee
class Employee {
// Members below can be accessed from outside the class
public:
	void work() { std::cout << "Working" << std::endl; }
};

// Define the type Manager
class Manager : public Employee {
// Members below can be accessed from outside the class
public:
	void manage() { std::cout << "Managing" << std::endl; }
};

class SeniorManager : public Manager {
public:
	void strategize() { std::cout << "Strategizing" << std::endl; }
};

// Program execution starts in main()
int main() {
	SeniorManager sm;
	sm.work();
	sm.manage();
	sm.strategize();
	// 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. constructor arguments को chain में ऊपर pass करना भूल जाना।
  2. यह मान लेना कि destructors constructors जैसे same order में चलते हैं, जब वे reverse में चलते हैं।
  3. multilevel को multiple inheritance से confuse करना।

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.