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

C++ Inheritance में Access Control

Inherit करते समय इस्तेमाल किया गया access specifier (public, protected, या private) control करता है कि base class के public और protected members derived class में कैसे appear होते हैं, यह बदलते हुए कि आगे-derived classes और बाहरी code क्या देख सकता है।
Syntax
cpp
class Derived : public Base { };     // public stays public
class Derived : protected Base { };  // public becomes protected
class Derived : private Base { };    // public becomes private

public Inheritance

Public inheritance के साथ, base class के public members derived class में public रहते हैं और इसके protected members protected रहते हैं, जो inheritance की सबसे common और intuitive form है।

उदाहरण: public Inheritance

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

// Define the type Base
class Base {
// Members below can be accessed from outside the class
public:
	int a = 1;
protected:
	int b = 2;
};

// Define the type Derived
class Derived : public Base {
// Members below can be accessed from outside the class
public:
	void show() { std::cout << a << " " << b << std::endl; }
};

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

protected Inheritance

Protected inheritance के साथ, base class के दोनों public और protected members derived class में protected बन जाते हैं, class hierarchy के बाहर के code से hidden रहते हुए लेकिन फिर भी आगे की subclasses के इस्तेमाल के लिए।

उदाहरण: protected Inheritance

cpp
#include <iostream>

class Base {
public:
	int a = 1;
};

class Derived : protected Base {
public:
	void show() { std::cout << a << std::endl; }
};

int main() {
	Derived d;
	// d.a would fail to compile: no longer public
	d.show();
	return 0;
}

private Inheritance

Private inheritance के साथ, base class के दोनों public और protected members derived class में private बन जाते हैं, मतलब आगे की subclasses भी अब उन्हें सीधे access नहीं कर सकतीं।

उदाहरण: private Inheritance

cpp
#include <iostream>

class Base {
public:
	int a = 1;
};

class Derived : private Base {
public:
	void show() { std::cout << a << std::endl; }
};

int main() {
	Derived d;
	// d.a would fail to compile: private inheritance hides it entirely
	d.show();
	return 0;
}

Access Specifier Summary Table

तीनों inheritance access specifiers derived class में base class के public और protected members को अलग-अलग transform करते हैं, जबकि private base members कोई भी specifier इस्तेमाल हो derived class में कभी accessible नहीं होते।

उदाहरण: Access Specifier Summary Table

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

// Define the type Base
class Base {
// Members below can be accessed from outside the class
public:
	int pub = 1;
protected:
	int prot = 2;
// Members below are only accessible inside the class
private:
	int priv = 3;
};

// Define the type PublicDerived
class PublicDerived : public Base {};
class ProtectedDerived : protected Base {};
class PrivateDerived : private Base {};

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

सही Inheritance Access चुनना

Public inheritance एक true is-a relationship के लिए इस्तेमाल होती है जहाँ derived class को base का interface expose करना चाहिए, जबकि protected और खासकर private inheritance कम इस्तेमाल होती हैं, मुख्य रूप से इसे publicly expose किए बिना implementation reuse model करने के लिए।

उदाहरण: Choosing the Right Inheritance Access

cpp
#include <iostream>

class Animal {
public:
	void eat() { std::cout << "Eating" << std::endl; }
};

class Dog : public Animal {
	// public inheritance: a Dog genuinely "is-a" Animal
};

int main() {
	Dog d;
	d.eat();
	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. यह उम्मीद करना कि base class के private members derived class में accessible होंगे।
  2. inherit करते समय public भूल जाना, ताकि classes के लिए default private हो जाए।
  3. यह मान लेना कि protected members बाहर से accessible हैं।

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.