Skip to content

Files

Latest commit

 

History

History
144 lines (110 loc) · 3.69 KB

05. Access Modifiers.md

File metadata and controls

144 lines (110 loc) · 3.69 KB

ACCESS MODIFIERS

Access modifiers are used to implement an important aspect of Object-Oriented Programming known as Data Hiding.

Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members, i.e., they set some restrictions on the class members so that they can’t be directly accessed by the outside functions.

TYPES OF ACCESS MODIFIERS

  1. Public
  2. Private
  3. Protected

DEFAULT

If we do not specify any access modifiers for the members inside the class, then by default the access modifier for the members will be Private.

PUBLIC

The data members and member functions declared as public can be accessed by other classes and functions too.

The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.

#include<iostream>
using namespace std;

class Solution
{
    public:
    
    int data=10;
    
    void getData()
    {
        cout<<"data="<<data<<endl;
    }
};
int main()
{
    Solution obj;
    
    cout<<obj.data<<endl; //accessible
    obj.getData();
}

PRIVATE

The class members declared as private can be accessed only by the member functions inside the class.

They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of the class.

#include<iostream>
using namespace std;

class Solution
{
    int data=10;
    
    public:
    
    void getData()
    {
        cout<<"data="<<data<<endl;
    }
};
int main()
{
    Solution obj;
    
    cout<<obj.data<<endl; //int Solution::dta is private within this context, hence not accessible
    obj.getData();
}

How to use it

#include<iostream>
using namespace std;

class Solution
{
    int data;
    
    public:
    
    void getData(int data)
    {
        this->data=data;
        cout<<"data="<<data<<endl;
    }
};
int main()
{
    Solution obj;
    
    obj.getData(10);
}

PROTECTED

Members declared as protected within a class are accessible within the class, derived classes (subclasses), and friend functions.

They are not directly accessible from outside the class (from main or other non-member functions) like public members.

Protected members provide a level of encapsulation, allowing derived classes to access and modify these members while still restricting access from unrelated code.

#include<iostream>
using namespace std;

class Solution
{
    protected:
    
    int data;
};
class Child : public Solution
{
    public:
    
    void getData(int data)
    {
        this->data = data; // Accessing the protected member from the base class
        cout<<data<<endl;
    }
};
int main()
{
    Child obj;
    
    obj.getData(10);
}

DIFFERENCE

|-----------------|---------------------------|--------------------------------------|----------------------------------------|
| Access Modifier | Accessibility Within Class | Accessibility Within Derived Classes | Accessibility Outside Class Hierarchy |
|-----------------|---------------------------|--------------------------------------|----------------------------------------|
| Public          | Accessible                | Accessible                           | Accessible                             |
| Private         | Accessible                | Not Accessible                       | Not Accessible                         |
| Protected       | Accessible                | Accessible                           | Not Accessible                         |
|-----------------|---------------------------|--------------------------------------|----------------------------------------|