State all the class access modifiers and explain each one of them
![]() Don't want to miss a single bit? Subscribe By Email for Daily Jobs |
C++ has three access modifiers namely public, private and protected.
Public: a public modifier means that the data or the functions of a class are accessible by any program. There are no restrictions in accessing public members of a class. Ideally all the functions that allow manipulating or accessing the class data are made public.
Private: the private keyword means that the data and member functions that are private cannot be accessed by other programs. They are accessible only within the class or struct in which they are declared. If one tries to access the private members of a class it results in a compile time error. Encapsulation is possible due to the private access modifier.
Protected: this access modifier plays a key role in inheritance. This keyword gives a protected access to the member variables and functions which means that these are accessible from within the class as well as any classes that are derived from the class that declared this member. However the protected members are not accessible to the any other programs.
Related Articles
- What is Inheritance ? How is a Class Inherited in C++ ?
- What are static Data and Static Member Functions in C++ ?
- What are classes ? How are they defined ?
- What are Friend Functions and Friend Classes in C++ ?
- Which members of outer class are accessible within inner class in Java


