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

C++ Inheritance का परिचय

Inheritance एक नई class को एक पुरानी class से सब कुछ receive करने देता है, एक parent के traits inherit करते एक child की तरह। Child अपनी चीज़ें add या change कर सकता है।
Syntax
cpp
class Base {
  // members
};

class Derived : public Base {
  // extra members
};

Inheritance क्या है?

Inheritance एक नई class को, derived class कहलाती है, एक existing class की properties और methods automatically acquire करने देता है, base class कहलाती है।

यह related classes को हर एक अलग से reimplement करने की बजाय common behavior share करने देकर code reuse promote करता है।

उदाहरण: What is Inheritance?

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

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

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void bark() { std::cout << "Barking" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.eat();
	d.bark();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base और Derived Class Constructors

जब आप एक derived class का एक object बनाते हैं, base class का constructor object के inherited हिस्से को setup करने के लिए हमेशा पहले चलता है, और वह खत्म होने के बाद ही derived class का अपना constructor इसने add किए parts को initialize करने के लिए चलता है।

उदाहरण: Base and Derived Class Constructors

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

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

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

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

Public Inheritance Access

Public inheritance के साथ, जो भी member base class में public था वह derived class में public रहता है, मतलब external code इसे एक derived object के जरिए exactly उतनी ही freely access कर सकता है जितना एक base object के जरिए कर सकता था।

उदाहरण: Public Inheritance Access

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

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	int legs = 4;
};

// Define the type Dog
class Dog : public Animal {};

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

Protected Access Specifier

Protected members को class hierarchy के बाहर से कभी access नहीं किया जा सकता, लेकिन private members के उलट, वे उस class से derive करने वाली किसी भी class के अंदर freely accessible रहते हैं — एक subclass को legitimately जिस internal state पर build करना चाहिए उसके लिए useful।

उदाहरण: Protected Access Specifier

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

// Define the type Animal
class Animal {
protected:
	int age = 2;
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void showAge() { std::cout << age << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.showAge();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base Class Methods Override करना

Method overriding एक derived class को उस function का अपना specific implementation देने देता है जो base class पहले ही define करता है, inherited behavior को उस particular subclass के लिए tailored logic से replace करते हुए।

उदाहरण: Overriding Base Class Methods

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

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

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() { std::cout << "Woof" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.speak();
	// 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. initializer list में arguments के साथ base class constructor call करना भूल जाना, ताकि base का default constructor required हो।
  2. public के बिना class B : A लिखना और private inheritance पाना।
  3. base pointer के जरिए derived objects delete करते समय base destructor को virtual न बनाना।

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.