+1 vote
in Class 12 by kratos

What are access specifiers? Explain any two with examples.

1 Answer

+6 votes
by kratos
 
Best answer

The data members and member functions can be accessed using access specifiers. They define the scope of members.
The different access specifiers are private, protected and public:

1. Private members:
The member data and members functions defined using access specifier private, can be accessed by member functions of that class only. Non-members of the class cannot access private members of the class. If no access specifier is mentioned for the members, then it is treated as private members.

2. Public members:
The public members of a class can be accessed by member functions of that class and also non member functions (outside the class) of the class. The public member functions can access private, protected and public data members of the class.

For example:

The private data members regno, fees and name can be accessed by only getdata() and printdata() public member functions of the class student.

...