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

C++ में Hierarchical Inheritance

Hierarchical inheritance तब है जब एक parent के कई children हों, same mom से inherit करने वाले siblings की तरह। हर child फिर अपने तरीके से अलग हो सकता है।
Syntax
cpp
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?

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

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

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

Unique Child Behavior

भले ही हर child same parent share करता है, हर एक अपने खुद के methods define करने के लिए free है जो कोई sibling class नहीं रखता, जो exactly है जो उनकी common ancestry के बावजूद उन्हें एक-दूसरे से differentiate करता है।

उदाहरण: Unique Child Behavior

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

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

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

Constructor Chaining

जब एक child object बनता है, shared base state establish करने के लिए parent का constructor पहले चलता है, और सिर्फ तभी child का अपना constructor चलता है उस particular subclass को unique बनाने वाली किसी भी चीज़ को configure करने के लिए।

उदाहरण: Constructor Chaining

cpp
// 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;
}
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. यह दोबारा check किए बिना base class बदलना कि हर derived class अभी भी काम करती है।
  2. एक base pointer इस्तेमाल करके सिर्फ एक derived class में defined एक function call करना, जो compile नहीं होता।
  3. base function पर virtual भूल जाना, ताकि base pointers के जरिए derived versions call न हों।

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.