Access Specifiers in C++

 

Access specifiers:

The access specifiers are the scope of the data.

The types are

·       Private

·       Public

·       Protected

Private:

Private access means a member data can only be accessed by the member function.

Members declared under private are accessible only within the class.

If no access specifier is mentioned, then by default, members are private.

Ex:

class simple

{

            private:

                        int x;

                        float y;

};

Public:

Public access means that member can be accessed by any functions outside the class also.

Ex:

class simple

{

            public:

int x;

float y;

};

 

Protected:

The members which are declared using protected can be accessed by member functions, member function of derived class and also by friend function.

class simple

{

            protected:

int x;

float y;

};

Comments