C++ Inheritance में Access Control
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
protected Inheritance
Protected inheritance के साथ, base class के दोनों public और protected members derived class में protected बन जाते हैं, class hierarchy के बाहर के code से hidden रहते हुए लेकिन फिर भी आगे की subclasses के इस्तेमाल के लिए।
उदाहरण: protected Inheritance
#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;
}
Login to try C/C++/Java/PHP code in the editor
private Inheritance
Private inheritance के साथ, base class के दोनों public और protected members derived class में private बन जाते हैं, मतलब आगे की subclasses भी अब उन्हें सीधे access नहीं कर सकतीं।
उदाहरण: private Inheritance
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
सही 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
#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;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि base class के private members derived class में accessible होंगे।
- inherit करते समय
publicभूल जाना, ताकि classes के लिए default private हो जाए। - यह मान लेना कि protected members बाहर से accessible हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: