Access specifiers:
1. private
2. protected
3. public
Private access specifier: Class members declared as private can be accessed only from within the class. Outside class access is not allowed for private members of class. By default members are private.
Protected access specifier: Class members declared as protected can be accessed by the member functions within its class and any class immediately derived from it. These members cannot be accessed by the functions outside these two classes.
Public access specifier: Class members declared as public can be accessed from outside the class also.
Example:-
class base
{
private:
int a;
protected:
int b;
public:
void display()
{
cout<<a<<b;
}
};
class derived:public base
{
public:
void show()
{
cout<<b;
}
};
void main()
{
derived d;
d.display();
d.show();
}
In the above example, variable „a‟ can be access by its member function display () as it is a private variable. Variable „b‟ can be accessed by its member function display () as well as member function show () of its derived class as it is a protected member. Member function display () and show () can be accessed from main () as they are public members of class