What is access specifier in C++?

(Last Updated On: February 26, 2019)

Answers: Access specifier in C++ is to control the access of data and member functions of a class. We may want some data members or functions not to be visible out side of the class or to main program or to another class etc. So, we use access specifiers in C++.
C++ supports three type of access specifiers that is public, private and protected access specifiers.

Default access specifier in C++ is private access specifier. Means, if we don’t put any access specifier in a class before data and functions, all data and functions will be private.
For example, in below class all the data members and functions are private.

Class containing all type of access specifiers in C++ – example



Private access specifier: –Data and functions can’t be accessed outside of a class.
Protected access specifier:  Data and functions are only accessible to child class in an inheritance.
Public access specifier: Data and functions are accessible from everywhere outside of a class.
I have answer here use of access specifiers in C++ with detail and that also describes what issue is if we use public specifier instead of private.