What are Access Specifiers in C++?
Data hiding is an important concept of Object-Oriented Programming, implemented with these Access modifiers' help. It is also known as Access Specifier.
Access Specifiers in a class decide the accessibility of the class members, like variables or methods in other classes. That is, it will decide whether the class members or methods will get directly accessed by the blocks present outside the class or not, depending on the type of Access Specifier.
In a program, we need to create methods or variables that can be accessed by the object of the same class or accessible in the entire program. And Access Modifiers help us to specify that.
There are three types of access modifiers in C++:
- Public
- Private
- Protected
To manipulate and fetch the data, a public specifier is used, and to protect the data from outside members, a private specifier is used so that the crucial or sensitive data cannot be tampered with or leaked outside of its block.
Syntax of Declaring Access Specifiers in C++
The syntax to use these specifiers is very simple. We need to mention the type of specifier with a colon just before the class's variable or methods.
Example of Access Specifiers in C++
Suppose there is a company with different departments to do specific tasks. Since the HR department works in different areas, they also manage the company's finance. They credit the salary to the employee account every month and handle all the money-related issues.
And the salary is one such thing, which everybody should not have access to. It should be accessible by the HR department or the employee-manager, who can update their salaries after the appraisal.
So, if there is a class named Employee where there are different variables, the information related to them like their Employee Id, Employee name, salary, Date of Joining, performance rating, etc.
And we have to design the system so that every employee can find an employee's name, date of joining, etc., using the employee id, but salary can not be accessible to all. These access specifiers or access modifiers come into use. We can make the salary variable private and the rest all public, and for updating the salary or getting the salary, we can make proper methods for that.
Example - There are three variables in an Employee Class. One is Employee name, the second is Employee Id, and the third is Employee Salary.
Here, employeeSalary is a private access specifier, but employeeId and employeeName are public access specifiers.
And the setEmployeeSalary method/function is a protected specifier.
Types of Access Specifiers & How do they work in C++?
1. Public Access Specifier
This keyword is used to declare the functions and variables public, and any part of the entire program can access it. The members and member methods declared public can be accessed by other classes and functions.
The public members of a class can be accessed from anywhere in the program using the (.) with the object of that class.
Example - In the above example,employeeId and employeeName are public access specifiers.
2. Private Access Specifiers
The private keyword is used to create private variables or private functions. The private members can only be accessed from within the class. Only the member functions or the friend functions are allowed to access the private data of a class or the methods of a class.
Note -
- Protected and Private data members or class methods can be accessed using a function only if that function is declared as the friend function.
- We can use the keyword friend to ensure the compiler understands and make the data accessible to that function.
Example - In the above example,employeeSalary is a private access specifier.
3. Protected Access Specifiers
The protected keyword is used to create protected variables or protected functions. The protected members can be accessed within and from the derived/child class.
Note - A class created or derived from another existing class(base class) is known as a derived class. The base class is also known as a superclass. It is created and derived through the process of inheritance.
For more details about derived class or inheritance, you can follow this blog.
Protected access specifier is similar to the private modifier. It cannot be accessed outside of its class except the derived class or subclass of that class. However, it can be accessed by the friend function of that class, similar to a private specifier.
Example - In the above example,setEmployeeSalary method/function is a protected specifier.
Conclusion
-
If any sensitive information that, when leaked or tampered with, can cause trouble, always make sure to set the access specifier as private.
-
All other classes and functions can access public elements.
-
Private elements cannot be accessed outside the class in which they are declared.
-
Private elements can be accessed outside the class in which they are declared only when the class is a friend class or function is a friend function of that class.
-
Protected elements can be accessed by derived classes. They are almost similar to the private access specifier.
- If we do not mention any access specifier for the members inside the class, then by default, the Access Specifier in C++ for the members will be Private.
- All functions are accessible to the friend function of the class.
Access Specifier | Same Class | Outside Class | Derived Class |
---|---|---|---|
Public Modifier | YES | YES | YES |
Private Modifier | YES | NO | NO |
Protected Modifier | YES | NO | YES |