Access modifiers are special keywords in C++, that control the visibility of class members (data and functions).
They help in implementing data hiding by restricting or allowing access to certain parts of a class.
Let's discuss it one by one
1. Public Specifier
Public specifier makes class members (variables and functions) accessible from anywhere - inside the class, outside the class, or even other files.
2. Private Specifier
Private specifier makes class members accessible only inside the class itself (mainly member methods), and is mainly used to hide data from outside so that we can make changes internally later without changing the code of its users. By default, all class members in C++ are private if no specifier is mentioned.
CPP
#include <iostream>
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;
}
What happens if we try to access private members?
The code gives compiler error as we can see below.
C++
#include <iostream>
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;
^~~~~~
3. Protected Specifier
- The protected specifier makes members accessible inside the class itself and in its derived (child) classes, but not form outside code.
- It is mainly used in inheritance, allowing child classes to reuse or modify data and functions while still keeping them hidden from the outside world.
CPP
#include <iostream>
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;
}
OutputEmployee Name: Fedrick
Manager Name: Rohit
Manager ID: 102
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.
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems