C++ Access Specifiers
C++ access specifiers are used for setting the boundary for the availability of class members (data members and member functions) beyond that class.
There are 3 main access specifiers, private, public and protected .
Suppose a country possesses some information that it wishes to hide from the outside world, it will classify this information as confidential and hence nobody will have access to this information. While programming there are certain data members or member functions that we wish to keep safe inside a class and prevent direct access to these members from outside the class. This accomplished using the keyword private.
Generally, all data members are usually private because providing direct access to data members is not considered a good practice.
We can not access private members from outside the class.
The code above will only allow us access to the public data member and not to the private member.
This access specifier is like the news we see on television. It is available to everyone and can be used by everyone. Its access is not limited to the class. Now to access private data members we make use of public member functions. The code given below shows us how we can make use of public functions to access private data members.
A protected access modifier is similar to a private access modifier in the sense that it can’t be accessed outside of its class unless with the help of a friend class. We will learn more about friend class friend functions in the coming lessons. The protected access modifier is functional only when we are deriving classes using inheritance we will learn more about this in access modifiers 2.