Access Specifiers in C++ Inheritance - Public, Private and Protected Inheritance
Access Specifiers play an important role in C++ Inheritance. They control the accessibility of inherited members in a derived class and determine how the public and protected members of a base class are treated. C++ provides three inheritance access modes: Public Inheritance, Protected Inheritance and Private Inheritance.
C++ Inheritance में Access Specifiers महत्वपूर्ण भूमिका निभाते हैं। ये निर्धारित करते हैं कि Base Class के members को Derived Class में किस प्रकार access किया जा सकता है और inheritance के बाद उनकी accessibility क्या होगी। C++ में Inheritance के तीन access modes होते हैं: Public Inheritance, Protected Inheritance और Private Inheritance।
Access Specifiers in C++
C++ provides three access specifiers for class members:
| Access Specifier | Accessibility |
|---|---|
| Public | Accessible from anywhere through an object, subject to normal scope rules. |
| Protected | Accessible within the class and its derived classes. |
| Private | Accessible only within the class and its friends. |
C++ में तीन मुख्य Access Specifiers होते हैं:
Public: Public members को class के बाहर भी object के माध्यम से access किया जा सकता है।
Protected: Protected members को उसी class और उसकी Derived Classes में access किया जा सकता है।
Private: Private members को केवल उसी class और उसके friends के अंदर access किया जा सकता है।
Types of Inheritance Based on Access Specifiers
C++ supports three inheritance access modes:
Public Inheritance
Protected Inheritance
Private Inheritance
These modes determine how the public and protected members of the base class are accessible through the derived class. Private members of the base class are not directly accessible in the derived class, regardless of the inheritance mode.
C++ में inheritance के तीन access modes होते हैं। ये निर्धारित करते हैं कि Base Class के public और protected members, Derived Class में किस प्रकार accessible होंगे। Base Class के private members किसी भी inheritance mode में Derived Class के अंदर directly accessible नहीं होते।
Public Inheritance in C++
Public Inheritance is a type of inheritance in which a derived class inherits a base class using the public keyword.
In public inheritance, the public members of the base class remain public in the derived class, and the protected members remain protected.
Public Inheritance में Derived Class, Base Class को public keyword की सहायता से inherit करती है।
इसमें Base Class के public members, Derived Class में public रहते हैं तथा protected members protected ही रहते हैं।
Syntax of Public Inheritance
class Base {public:int publicData;protected:int protectedData;private:int privateData;};class Derived : public Base {// Derived class members};
Accessibility in Public Inheritance
| Base Class Member | Accessibility in Derived Class |
|---|---|
| Public | Public |
| Protected | Protected |
| Private | Not directly accessible |
Program: Public Inheritance in C++
The following program demonstrates public inheritance.
निम्नलिखित program में Public Inheritance को प्रदर्शित किया गया है।
#include <iostream>using namespace std;class Person {public:string name;protected:int age;private:int salary;public:void setPersonDetails() {name = "Rahul";age = 20;salary = 25000;}void displaySalary() {cout << "Salary: " << salary << endl;}};class Student : public Person {public:void displayDetails() {cout << "Name: " << name << endl;cout << "Age: " << age << endl;}};int main() {Student s;s.setPersonDetails();cout << "Student Details\n";s.displayDetails();s.displaySalary();return 0;}
Output
Student DetailsName: RahulAge: 20Salary: 25000
Explanation
The
Personclass contains public, protected and private data members.The
Studentclass publicly inherits fromPerson.The derived class can directly access the public member
nameand protected memberage.The private member
salarycannot be accessed directly insideStudent.The salary is displayed through a public member function of the base class.
इस program में:
Personclass में public, protected और private data members हैं।Studentclass,Personको publicly inherit करती है।Derived Class, public member
nameऔर protected memberageको directly access कर सकती है।Private member
salaryकोStudentclass में directly access नहीं किया जा सकता।Salary को Base Class के public member function के माध्यम से display किया गया है।
Protected Inheritance in C++
Protected Inheritance is a type of inheritance in which a derived class inherits a base class using the protected keyword.
In protected inheritance, the public and protected members of the base class become protected members of the derived class.
Protected Inheritance में Derived Class, Base Class को protected keyword की सहायता से inherit करती है।
इसमें Base Class के public और protected members, Derived Class में protected members बन जाते हैं।
Syntax of Protected Inheritance
class Base {public:int publicData;protected:int protectedData;};class Derived : protected Base {// Derived class members};
Accessibility in Protected Inheritance
| Base Class Member | Accessibility in Derived Class |
|---|---|
| Public | Protected |
| Protected | Protected |
| Private | Not directly accessible |
Program: Protected Inheritance in C++
The following program demonstrates protected inheritance.
निम्नलिखित program में Protected Inheritance को प्रदर्शित किया गया है।
#include <iostream>using namespace std;class Person {public:string name;protected:int age;public:void setPersonDetails() {name = "Amit";age = 21;}};class Student : protected Person {public:void displayDetails() {cout << "Name: " << name << endl;cout << "Age: " << age << endl;}};int main() {Student s;s.setPersonDetails();s.displayDetails();return 0;}
Output
Name: AmitAge: 21
Explanation
The
Studentclass inherits fromPersonusing protected inheritance.The public member
nameofPersonbecomes protected inStudent.The protected member
ageremains protected.Both members can be accessed inside the public member function
displayDetails()ofStudent.However, these inherited members cannot be accessed directly through the
Studentobject from outside the class.
इस program में:
Studentclass,Personको Protected Inheritance के माध्यम से inherit करती है।Personका public membername,Studentमें protected बन जाता है।Protected member
age, protected ही रहता है।दोनों members को
Studentके public member functiondisplayDetails()में access किया जा सकता है।लेकिन class के बाहर
Studentobject के माध्यम से इन inherited members को directly access नहीं किया जा सकता।
Private Inheritance in C++
Private Inheritance is a type of inheritance in which a derived class inherits a base class using the private keyword.
In private inheritance, the public and protected members of the base class become private members of the derived class.
Private Inheritance में Derived Class, Base Class को private keyword की सहायता से inherit करती है।
इसमें Base Class के public और protected members, Derived Class में private members बन जाते हैं।
Syntax of Private Inheritance
class Base {public:int publicData;protected:int protectedData;};class Derived : private Base {// Derived class members};
Accessibility in Private Inheritance
| Base Class Member | Accessibility in Derived Class |
|---|---|
| Public | Private |
| Protected | Private |
| Private | Not directly accessible |
Program: Private Inheritance in C++
The following program demonstrates private inheritance.
निम्नलिखित program में Private Inheritance को प्रदर्शित किया गया है।
#include <iostream>using namespace std;class Person {public:string name;protected:int age;public:void setPersonDetails() {name = "Rohit";age = 22;}};class Student : private Person {public:void displayDetails() {cout << "Name: " << name << endl;cout << "Age: " << age << endl;}};int main() {Student s;s.setPersonDetails();s.displayDetails();return 0;}
Output
Name: RohitAge: 22
Explanation
The
Studentclass privately inherits fromPerson.The public member
namebecomes private inStudent.The protected member
agealso becomes private inStudent.The members can be used inside the member functions of
Student.They cannot be accessed directly through the
Studentobject from outside the class.
इस program में:
Studentclass,Personको privately inherit करती है।Public member
name,Studentमें private बन जाता है।Protected member
ageभीStudentमें private बन जाता है।इन members का उपयोग
Studentके member functions के अंदर किया जा सकता है।इन्हें class के बाहर
Studentobject के माध्यम से directly access नहीं किया जा सकता।
Difference Between Public, Protected and Private Inheritance
The main difference between these inheritance modes is how they change the accessibility of public and protected members of the base class.
इन तीनों inheritance modes में मुख्य अंतर यह है कि ये Base Class के public और protected members की accessibility को किस प्रकार बदलते हैं।
| Base Class Member | Public Inheritance | Protected Inheritance | Private Inheritance |
|---|---|---|---|
| Public | Public | Protected | Private |
| Protected | Protected | Protected | Private |
| Private | Not directly accessible | Not directly accessible | Not directly accessible |
Comparison of Inheritance Modes
| Basis | Public Inheritance | Protected Inheritance | Private Inheritance |
|---|---|---|---|
| Keyword | public | protected | private |
| Base public members | Remain public | Become protected | Become private |
| Base protected members | Remain protected | Remain protected | Become private |
| External access to inherited public members | Allowed | Not allowed directly | Not allowed directly |
| Common use | Represents an "is-a" relationship | Restricts external access while allowing further derivation | Reuses implementation without exposing the base interface |
Public Inheritance का उपयोग सामान्यतः "is-a" relationship को दर्शाने के लिए किया जाता है। Protected और Private Inheritance का उपयोग तब किया जा सकता है जब Derived Class, Base Class की functionality reuse करना चाहती है लेकिन उसे उसी प्रकार public interface के रूप में expose नहीं करना चाहती।
Default Access Mode of Inheritance in C++
In C++, the default inheritance access mode depends on whether the derived class is declared using class or struct.
For a derived class declared with
class, the default inheritance mode is private.For a derived class declared with
struct, the default inheritance mode is public.
C++ में default inheritance access mode इस बात पर निर्भर करता है कि Derived Class को class से declare किया गया है या struct से।
classके साथ default inheritance modeprivateहोता है।structके साथ default inheritance modepublicहोता है।
Example
class Base {public:int value;};class Derived1 : Base {// Private inheritance by default};struct Derived2 : Base {// Public inheritance by default};
For clarity and readability, it is generally recommended to specify the inheritance access mode explicitly.
Code को स्पष्ट और readable बनाने के लिए inheritance access mode को हमेशा explicitly लिखना बेहतर होता है।
Frequently Asked Questions (FAQs)
What are the three types of inheritance access modes in C++?
The three inheritance access modes are Public, Protected and Private Inheritance.
C++ में inheritance के तीन access modes Public, Protected और Private Inheritance हैं।
What happens to public members in public inheritance?
Public members of the base class remain public in the derived class.
Public Inheritance में Base Class के public members, Derived Class में public ही रहते हैं।
What happens to public members in protected inheritance?
Public members of the base class become protected members in the derived class.
Protected Inheritance में Base Class के public members, Derived Class में protected बन जाते हैं।
What happens to public members in private inheritance?
Public members of the base class become private members in the derived class.
Private Inheritance में Base Class के public members, Derived Class में private बन जाते हैं।
Can a derived class directly access private members of its base class?
No. Private members of the base class cannot be directly accessed by the derived class, regardless of the inheritance mode. They may be accessed through suitable public or protected functions of the base class.
नहीं। Base Class के private members को Derived Class किसी भी inheritance mode में directly access नहीं कर सकती। इन्हें Base Class के उपयुक्त public या protected functions के माध्यम से access किया जा सकता है।
What is the default inheritance mode in C++?
The default inheritance mode is private for a class and public for a struct.
C++ में class के लिए default inheritance mode private और struct के लिए public होता है।
Which inheritance mode is commonly used for an "is-a" relationship?
Public inheritance is commonly used to represent an "is-a" relationship.
"Is-a" relationship को दर्शाने के लिए सामान्यतः Public Inheritance का उपयोग किया जाता है।
Access Specifiers in C++ Inheritance control the accessibility of inherited members. Public Inheritance preserves the public interface of the base class, Protected Inheritance makes inherited public members protected, and Private Inheritance makes inherited public and protected members private. Choosing the correct inheritance mode helps create secure, clear and maintainable class designs.
C++ Inheritance में Access Specifiers inherited members की accessibility को नियंत्रित करते हैं। Public Inheritance, Base Class के public interface को बनाए रखता है। Protected Inheritance में inherited public members protected बन जाते हैं, जबकि Private Inheritance में inherited public और protected members private बन जाते हैं। सही inheritance mode का चयन सुरक्षित, स्पष्ट और maintainable class design बनाने में मदद करता है।
Comments
Post a Comment