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

C++ में Single Inheritance

Single inheritance तब है जब एक class का exactly एक parent हो, एक parent के skills copy करके अपनी add करने वाले एक child की तरह। यह parent के code reuse करता है।
Syntax
cpp
class Base {
public:
  // shared members
};

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

Basic Single Inheritance

Single inheritance C++ में सबसे simple inheritance relationship है: exactly एक derived class exactly एक base class से inherit करती है, एक ज़्यादा complex hierarchy की बजाय एक straightforward parent-child pair बनाते हुए।

उदाहरण: Basic Single Inheritance

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

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

// Define the type Car
class Car : public Vehicle {};

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

Base Class Members Access करना

एक single derived class किसी भी base class member को freely पढ़ और लिख सकती है जो public या protected declared था, क्योंकि inheritance वह accessibility child में carry करता है — private base members यहाँ भी off-limits रहते हैं।

उदाहरण: Accessing Base Class Members

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

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

// Define the type Car
class Car : public Vehicle {};

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

Constructor Order of Execution

Base class constructors object के inherited हिस्से को establish करने के लिए हमेशा पहले execute होते हैं, और सिर्फ उसके बाद derived class का अपना constructor इसने खुद introduce किए properties की initialization खत्म करने के लिए चलता है।

उदाहरण: Constructor Order of Execution

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

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

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

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

Private Inheritance

Private inheritance base class के हर public और protected member को derived class के अंदर एक private member में convert करती है, मतलब बाहरी code एक derived object के जरिए उन inherited members तक हर access खो देता है — भले ही derived class खुद उन्हें internally अभी भी इस्तेमाल कर सकती है।

उदाहरण: Private Inheritance

cpp
#include <iostream>

class Vehicle {
public:
	int wheels = 4;
};

class Car : private Vehicle {
public:
	void showWheels() { std::cout << wheels << std::endl; }
};

int main() {
	Car c;
	// c.wheels would fail to compile: private inheritance
	c.showWheels();
	return 0;
}

Protected Inheritance

Protected inheritance एक middle ground है: यह सारे inherited public और protected base members को derived class में protected members में convert करती है, उन्हें main() या बाहरी code से hidden रखते हुए फिर भी derived class की किसी भी आगे की subclass को उन्हें access करने देते हुए।

उदाहरण: Protected Inheritance

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

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

// Define the type Car
class Car : protected Vehicle {
// Members below can be accessed from outside the class
public:
	void showWheels() { std::cout << wheels << std::endl; }
};

// Program execution starts in main()
int main() {
	Car c;
	c.showWheels();
	// 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. derived class में सीधे base class के private members access करने की कोशिश करना।
  2. यह उम्मीद करना कि derived constructor पहले चलेगा, जब base constructor पहले चलता है।
  3. inheritance list में public भूल जाना, जो derived class में base members को private बना देता है।

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.