3.access Modifiers
3.access Modifiers
(Access Modifiers)
Access Modifiers in C++
Access Modifiers or Access Specifiers in a class are used to set the accessibility
of the class members.
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
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.
Access Modifier :: Public
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.
Access Modifier :: Private
Access Modifier :: Private
The output of the below 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.
However, we can access the private data members of a class indirectly using the
public member functions of the class.
Access Modifier :: Private
Access Modifier :: Protected