C++ में Multilevel Inheritance
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Call Sequence
इस chain के साथ constructors strictly top-down चलते हैं: grandparent पहले, फिर parent, फिर grandchild, क्योंकि हर level इसके ऊपर वाले level के पूरी तरह initialized होने पर depend करता है इससे पहले कि यह इसके ऊपर build कर सके।
Destructors फिर exact opposite order में चलते हैं।
उदाहरण: Constructor Call Sequence
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Multilevel Inheritance में Member Access
Topmost class में declared members bottommost class तक पूरी तरह accessible रहते हैं, जब तक chain की हर link ने उनकी visibility को public या protected preserve किया हो इसे रास्ते में private तक narrow करने की बजाय।
उदाहरण: Member Access in Multilevel Inheritance
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- constructor arguments को chain में ऊपर pass करना भूल जाना।
- यह मान लेना कि destructors constructors जैसे same order में चलते हैं, जब वे reverse में चलते हैं।
- multilevel को multiple inheritance से confuse करना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: