Access modifiers are used to implement an important aspect of Object-Oriented Programming known as Data Hiding. Consider a real-life example:
The Research and analysis wing (R&AW), having 10 core members has come in possession of sensitive confidential information regarding national security. Now we can corelate these core members to data members or member functions of a class which in turn can be corelated to the R&A wing. These 10 members can directly access confidential information from their wing (the class), but anyone apart from these 10 members can’t access this information directly i.e. outside functions other than those prevalent in the class itself can’t access information that is not entitled to them, without having either assigned privileges (such as those possessed by friend class and inherited class as will be seen in this article ahead) or access to one of these 10 members who is allowed direct access to the confidential information (similar to how private members of a class can be accessed in the outside world through public member functions of the class that have direct access to private members). This is what data hiding is in practice.
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
There are 3 types of access modifiers available in C++:
Note: 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.
Let us now look at each one these access modifiers in details:
1. Public: All the class members declared under the public specifier will be available to everyone. 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.
Radius is: 5.5 Area is: 94.985
In the above program the data member radius is declared as public so it could be accessed outside the class and thus was allowed access from inside main().
2. 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 a class.
In function 'int main()': 11:16: error: 'double Circle::radius' is private double radius; ^ 31:9: error: within this context obj.radius = 1.5; ^
The output of above program is a compile time error because we are not allowed to access the private data members of a class directly outside the class. Yet an access to obj.radius is attempted, radius being a private data member we obtain a compilation error.
However, we can access the private data members of a class indirectly using the public member functions of the class.
Radius is: 1.5 Area is: 7.065
3. Protected: Protected access modifier is similar to private access modifier in the sense that it can’t be accessed outside of it’s class unless with the help of friend class, the difference is that the class members declared as Protected can be accessed by any subclass(derived class) of that class as well.
Note: This access through inheritance can alter the access modifier of the elements of base class in derived class depending on the modes of Inheritance.
id_protected is: 81
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Access Modifiers in Java
- Modifiers for Vector in C++ STL
- Interesting facts about data-types and modifiers in C/C++
- Can we access global variable if there is a local variable with same name?
- Access specifier of methods in interfaces
- What happens when more restrictive access is given to a derived class method in C++?
- Can we access private data members of a class without using a member or a friend function?
- Random-access Iterators in C++
- How to access private/protected method outside a class in C++
- Count perfect square fractions from given array
- Maximize absolute difference between X and Y by at most N decrements
- Rearrange array by interchanging positions of even and odd elements in the given array
- Convert given Float value to equivalent Fraction
- Check if a point having maximum X and Y coordinates exists or not