Access Specifiers (modifiers) in C++

Access specifiers , access modifiers or visibility modes are different words used for the same purpose to show the access level of data in c++ for various purposes. These are used for data hiding. Here, are the three types of access modifiers.

  1. Public
  2. Private
  3. Protected

1. Public

If the data members are declared public access then they can be accessed from other functions out side the class. It is declared by the key word “public”.

class XYZ
{
     public:
        member of class;
};

2. Private

If the data members are declared as private access then they cannot be accessed from other functions outside the class. It can only be accessed by the functions declared within the class. It is declared by the key word “private” .

If no access specifier is specified then it is treated by default as private in c++.

class XYZ
{
    member of class;
};

3. Protected

The access level of protected declaration lies between public and private. This access specifier is used at the time of inheritance.

class XYZ
{
    member of class;
public:
    member of class;
protected:
    member of class;
};

Related posts