Access specifiers in C++

Access specifiers or (access modifiers) are keywords in object oriented language that set the accessibility of class , functions(methods), and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.

In C++ there are 3 access specifiers.


Access specifiers in the program, are followed by colon(:).

Private Member :

  • Private members (variables and functions) of class cannot be accessed outside of the class.

  • Only the class and friend functions can access private members.

  • By default all members of the class would be private.

  • To declare a member as private use keyword private.

  • If someone tries to access private members outside of the class, it will raise an error.

Protected Member :

  • Protected members (variables and functions) of class is similar to private, it makes class members inaccessible outside of the class.

  • But they can be accessed by any subclass of that class, it means, if class A is inherited by class B, then class B is subclass of class A(Discuss later).

  • To declare a member as protected use keyword protected.

Public Member :

  • Public members (variables and functions) of class can be access anywhere outside the class.

  • To declare a member as public use keyword public.

Code Example:

Program to understand private, protected, and public member of class.
using namespace std;
class Test
 int a;

  int b;
  int c;

  int d;

  void showData()
   cout<<"a = "<< a<< endl;
   cout<<"b = "<< b<< endl;
   cout<<"c = "<< c<< endl;
   cout<<"d = "<< d<< endl;

int main() 
 Test ob;
//cannot access a is private
 ob.a = 10;
//cannot access b is protected
 ob.b = 20;
 ob.c = 30;// fine 
//cannot access d is private
 ob.d = 40; 
 return 0;

Next topic is Friend function in C++


Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at

Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at

(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017,, All rights reserved