C++ में Hierarchical Inheritance
In this page:
class Parent { };
class Child1 : public Parent { };
class Child2 : public Parent { };
Hierarchical Inheritance क्या है?
Hierarchical inheritance एक structure है जहाँ कई distinct child classes सब एक single shared parent class से inherit करती हैं, एक root और कई branches वाला एक tree बनाते हुए।
यह तब natural fit है जब कई related types common behavior share करते हों लेकिन एक-दूसरे से diverge भी करते हों।
उदाहरण: What is Hierarchical Inheritance?
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
void describe() { std::cout << "I am a shape" << std::endl; }
};
// Define the type Circle
class Circle : public Shape {};
class Square : public Shape {};
// Program execution starts in main()
int main() {
Circle c;
Square s;
c.describe();
s.describe();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Shared Base Members
इस structure में हर child class parent में defined variables और methods share करती है, लेकिन हर object को अभी भी उस inherited data की अपनी independent copy मिलती है — एक shared color member inherit करने वाला एक Circle और एक Square एक-दूसरे का color affect नहीं करते।
उदाहरण: Shared Base Members
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
int sides = 0;
};
// Define the type Circle
class Circle : public Shape {};
class Square : public Shape {};
// Program execution starts in main()
int main() {
Circle c;
Square s;
s.sides = 4;
// Print to the console with cout
std::cout << c.sides << " " << s.sides << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unique Child Behavior
भले ही हर child same parent share करता है, हर एक अपने खुद के methods define करने के लिए free है जो कोई sibling class नहीं रखता, जो exactly है जो उनकी common ancestry के बावजूद उन्हें एक-दूसरे से differentiate करता है।
उदाहरण: Unique Child Behavior
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
void roll() { std::cout << "Rolling" << std::endl; }
};
class Square : public Shape {
// Members below can be accessed from outside the class
public:
void stack() { std::cout << "Stacking" << std::endl; }
};
// Program execution starts in main()
int main() {
Circle c;
Square s;
c.roll();
s.stack();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Base Class Pointers
parent class type का एक pointer इसकी किसी भी child class के object की ओर point कर सकता है, जो related objects के एक mixed collection को एक single container में store करना और उन्हें उनके shared base interface के जरिए uniformly process करना आसान बनाता है।
उदाहरण: Base Class Pointers
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
virtual void describe() { std::cout << "Shape" << std::endl; }
};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
void describe() override { std::cout << "Circle" << std::endl; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Shape *ptr = new Circle();
ptr->describe();
// Free the heap memory with delete
delete ptr;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Chaining
जब एक child object बनता है, shared base state establish करने के लिए parent का constructor पहले चलता है, और सिर्फ तभी child का अपना constructor चलता है उस particular subclass को unique बनाने वाली किसी भी चीज़ को configure करने के लिए।
उदाहरण: Constructor Chaining
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
Shape() { std::cout << "Shape created" << std::endl; }
};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
Circle() { std::cout << "Circle created" << std::endl; }
};
// Program execution starts in main()
int main() {
Circle c;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह दोबारा check किए बिना base class बदलना कि हर derived class अभी भी काम करती है।
- एक base pointer इस्तेमाल करके सिर्फ एक derived class में defined एक function call करना, जो compile नहीं होता।
- base function पर
virtualभूल जाना, ताकि base pointers के जरिए derived versions call न हों।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: