Access Modifiers in C++ (original) (raw)

Last Updated : 2 Jun, 2026

Access modifiers in C++ are keywords that control the accessibility of class members (data members and member functions). They are an important part of encapsulation and data hiding, allowing developers to restrict access to sensitive data and expose only the required functionality.

Types of Access Modifiers in C++

Access modifiers specify the access level of class members and define where they can be used. C++ provides three access modifiers:

types_of_access_modifier

Public Specifier

The public access specifier allows class members to be accessed from anywhere in the program through an object of the class.

#include using namespace std;

class Student { public: string name; };

int main() { Student s;

// Accessing public member
s.name = "John";

cout << s.name;

return 0;

}

`

**Explanation

Private Specifier

The private access specifier restricts access to class members so that they can only be accessed within the class itself.

#include using namespace std;

class Employee { private:

// salary and empId cannot be accessed 
// from outside the Class
double salary;
int empID;

public:
string name; // Name can be accessed anywhere

Employee(string n, double s, int id) {
    name = n;
    salary = s;
    empID = id;
}

};

int main() { Employee emp("Fedrick", 50000, 101); cout << "Name: " << emp.name << endl; return 0; }

`

**Explanation

**Attempting to Access Private Members

Trying to access a private member directly from outside the class results in a compilation error.

C++ `

#include using namespace std;

class Employee { private:
double salary; int empID;

public:
string name;

// Constructor
Employee(string n, double s, int id) {
    name = n;
    salary = s;
    empID = id;
}

};

int main() { Employee emp("Fedrick", 50000, 101); cout << emp.salary << endl;
return 0; }

`

**Output

compilation error
prog.cpp: In function ‘int main()’:
prog.cpp:22:17: error: ‘double Employee::salary’ is private within this context
cout << emp.salary << endl;
^~~~~~
prog.cpp:6:12: note: declared private here
double salary;
^~~~~~

**Explanation

Protected Specifier

The protected access specifier allows class members to be accessed within the class and its derived classes, while preventing direct access from outside the class.

#include using namespace std;

class Employee { private:
double salary;

protected:
int empID;

public:
string name;

Employee(string n, double s, int id) {
    name = n;
    salary = s;
    empID = id;
}

};

// Derived class (inherits from Employee) class Manager : public Employee { public: Manager(string n, double s, int id) : Employee(n, s, id) {}

void showDetails() {
    cout << "Manager Name: " << name << endl;     
    cout << "Manager ID: " << empID << endl;     
}

};

int main() { Employee emp("Fedrick", 50000, 101); cout << "Employee Name: " << emp.name << endl; Manager m("Rohit", 70000, 102); m.showDetails();
return 0; }

`

Output

Employee Name: Fedrick Manager Name: Rohit Manager ID: 102

**Explanation

**Note: This access through inheritance can alter the access modifier of the elements of base class in derived class depending on the mode of Inheritance.