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 SpecifierAccessibility
PublicAccessible from anywhere through an object, subject to normal scope rules.
ProtectedAccessible within the class and its derived classes.
PrivateAccessible 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 MemberAccessibility in Derived Class
PublicPublic
ProtectedProtected
PrivateNot 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 Details
Name: Rahul
Age: 20
Salary: 25000

Explanation

  • The Person class contains public, protected and private data members.

  • The Student class publicly inherits from Person.

  • The derived class can directly access the public member name and protected member age.

  • The private member salary cannot be accessed directly inside Student.

  • The salary is displayed through a public member function of the base class.

इस program में:

  • Person class में public, protected और private data members हैं।

  • Student class, Person को publicly inherit करती है।

  • Derived Class, public member name और protected member age को directly access कर सकती है।

  • Private member salary को Student class में 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 MemberAccessibility in Derived Class
PublicProtected
ProtectedProtected
PrivateNot 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: Amit
Age: 21

Explanation

  • The Student class inherits from Person using protected inheritance.

  • The public member name of Person becomes protected in Student.

  • The protected member age remains protected.

  • Both members can be accessed inside the public member function displayDetails() of Student.

  • However, these inherited members cannot be accessed directly through the Student object from outside the class.

इस program में:

  • Student class, Person को Protected Inheritance के माध्यम से inherit करती है।

  • Person का public member name, Student में protected बन जाता है।

  • Protected member age, protected ही रहता है।

  • दोनों members को Student के public member function displayDetails() में access किया जा सकता है।

  • लेकिन class के बाहर Student object के माध्यम से इन 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 MemberAccessibility in Derived Class
PublicPrivate
ProtectedPrivate
PrivateNot 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: Rohit
Age: 22

Explanation

  • The Student class privately inherits from Person.

  • The public member name becomes private in Student.

  • The protected member age also becomes private in Student.

  • The members can be used inside the member functions of Student.

  • They cannot be accessed directly through the Student object from outside the class.

इस program में:

  • Student class, Person को privately inherit करती है।

  • Public member name, Student में private बन जाता है।

  • Protected member age भी Student में private बन जाता है।

  • इन members का उपयोग Student के member functions के अंदर किया जा सकता है।

  • इन्हें class के बाहर Student object के माध्यम से 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 MemberPublic InheritanceProtected InheritancePrivate Inheritance
PublicPublicProtectedPrivate
ProtectedProtectedProtectedPrivate
PrivateNot directly accessibleNot directly accessibleNot directly accessible

Comparison of Inheritance Modes

BasisPublic InheritanceProtected InheritancePrivate Inheritance
Keywordpublicprotectedprivate
Base public membersRemain publicBecome protectedBecome private
Base protected membersRemain protectedRemain protectedBecome private
External access to inherited public membersAllowedNot allowed directlyNot allowed directly
Common useRepresents an "is-a" relationshipRestricts external access while allowing further derivationReuses 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 mode private होता है।

  • struct के साथ default inheritance mode public होता है।

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

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Course - OOP and Modern C++

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)