Access Modifiers in C++
Access modifiers are used to implement an important feature of Object Oriented Programming known as Data Hiding. Consider a real-life example: Consider Indian secret informatic system having 10 senior members, have some top secret regarding national security. So we can think that 10 people as class data members or member functions who can directly access secret information from each other but anyone can’t access this information other than these 10 members i.e. outside people can’t access information directly without having any privileges. This is what data hiding is.
Access modifiers or Access Specifiers in a class are used to set the accessibility of 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++:
- Public
- Private
- Protected
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:
-
Public: All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes 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.
// C++ program to demonstrate public// access modifier#include<iostream>usingnamespacestd;// class definitionclassCircle{public:doubleradius;doublecompute_area(){return3.14*radius*radius;}};// main functionintmain(){Circle obj;// accessing public datamember outside classobj.radius = 5.5;cout <<"Radius is:"<< obj.radius <<"\n";cout <<"Area is:"<< obj.compute_area();return0;}chevron_rightfilter_noneOutput:
Radius is:5.5 Area is:94.985
In the above program the data member radius is public so we are allowed to access it outside the class.
-
Private: The class members declared as private can be accessed only by the 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.
Example:// C++ program to demonstrate private// access modifier#include<iostream>usingnamespacestd;classCircle{// private data memberprivate:doubleradius;// public member functionpublic:doublecompute_area(){// member function can access private// data member radiusreturn3.14*radius*radius;}};// main functionintmain(){// creating object of the classCircle obj;// trying to access private data member// directly outside the classobj.radius = 1.5;cout <<"Area is:"<< obj.compute_area();return0;}chevron_rightfilter_noneThe output of above program will be a compile time error because we are not allowed to access the private data members of a class directly outside the class.
Output:In function 'int main()': 11:16: error: 'double Circle::radius' is private double radius; ^ 31:9: error: within this context obj.radius = 1.5; ^However, we can access the private data members of a class indirectly using the public member functions of the class. Below program explains how to do this:
// C++ program to demonstrate private// access modifier#include<iostream>usingnamespacestd;classCircle{// private data memberprivate:doubleradius;// public member functionpublic:voidcompute_area(doubler){// member function can access private// data member radiusradius = r;doublearea = 3.14*radius*radius;cout <<"Radius is:"<< radius << endl;cout <<"Area is: "<< area;}};// main functionintmain(){// creating object of the classCircle obj;// trying to access private data member// directly outside the classobj.compute_area(1.5);return0;}chevron_rightfilter_noneOutput:
Radius is:1.5 Area is: 7.065
-
Protected: Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.
// C++ program to demonstrate// protected access modifier#include <bits/stdc++.h>usingnamespacestd;// base classclassParent{// protected data membersprotected:intid_protected;};// sub class or derived classclassChild :publicParent{public:voidsetId(intid){// Child class is able to access the inherited// protected data members of base classid_protected = id;}voiddisplayId(){cout <<"id_protected is:"<< id_protected << endl;}};// main functionintmain() {Child obj1;// member function of the derived class can// access the protected data members of the base classobj1.setId(81);obj1.displayId();return0;}chevron_rightfilter_noneOutput:
id_protected is:81
This article is contributed by Abhirav Kariya and Harsh Agarwal . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Access Modifiers in Java
- Modifiers for Vector in C++ STL
- Interesting facts about data-types and modifiers in C/C++
- Random-access Iterators in C++
- 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?
- Can we access global variable if there is a local variable with same name?
- Check if the first and last digit of number N is prime and their sum is less than K
- Program to parse the Molecules and get the Atoms count
- What is an Expression and What are the types of Expressions?
- How to learn Pattern printing easily?
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples



