Wednesday, November 15, 2023

Access Specifiers

Access specifier specifies how the data of a class can be accessed in a program. It is useful to control the data of a class. C++ provides three types of access specifiers. They are:

  • private
  • public
  • protected

private

It is used to declare both the data members and member functions of a class.

It is identified by the keyword “private‟ followed by : (colon).

The private members can be accessed only within the class or by the class members only. They are not accessible from outside the class (function) or another class.

public

It is also be used to declare the data members and member functions of a class.

The public members are accessible in any part of the program that is, inside the class as well as outside the class.

The member functions of a class must be declared in public specifier only.

Protected

It is also used to declare both the data members and member functions of a class.

It is also similar to private, but the protected members can be accessed in the derived classes.                        

Note: By default members of the class both data & functions are private. If any visibility label public/private/protected) is missing, they are automatically treated as private.

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top