In this article, you will learn about C++ access specifiers i.e. private, public and protected with example and explanation
Access Specifiers: Introduction |
Private Access Specifiers |
Protected Access Specifiers |
Public Access Specifiers |
C++ access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
For example, the class members are grouped into sections, private
protected
and public
. These keywords are called access specifiers which define the accessibility or visibility level of class members.
By default the class members are private
. So if the visibility labels are missing then by default all the class members are private
.
In inheritance, it is important to know when a member function in the base class can be used by the objects of the derived class. This is called accessibility and the access specifiers are used to determine this.
Access specifier can be either private or protected or public. In general access specifiers are the access restriction imposed during the derivation of different subclasses from the base class.
If private access specifier is used while creating a class, then the public and protected data members of the base class become the private member of the derived class and private member of base class remains private.
In this case, the members of the base class can be used only within the derived class and cannot be accessed through the object of derived class whereas they can be accessed by creating a function in the derived class.
Following block diagram explain how data members of base class are inherited when derived class access mode is private.