Explanation of
private ,protected & public
access modifier / Access
Specifiers / Visibility Modes
What are access
specifiers / Visibility Labels
• Access modifiers are used to implement an important feature of Object Oriented
Programming known as Data Hiding.
• 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++:
1. Private
2. Protected
3. Public
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.
Private
• By private we mean that members can be accessed only from within the
class i.e member data can be accessed by the member function.
• The private data members are not accessible to the outside world (i.e
outside the class)
• By default members of a class are private.
• Private members are not inheritable.
• 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.
Protected
• The members which are declared as protected ,can only
be accessed by member functions and friend function of
that class.
• Protected members are similar to private members ,the
only difference is that protected members are inheritable .
Public
• The members which are declared as public can be
accessed from out side classes also.
• 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.
Example
class Student
int rollno;
float marks;
public:
void getdata();
void display();
};