C++ | Access Modifiers

In this article, we will learn about the access modifiers in C++ and their types with examples.

Access Modifiers

One of the main features of object-oriented programming languages such as C++ is data hiding.

Data hiding refers to restricting access to data members of a class. This is to prevent other functions and classes from tampering with the class data.

However, it is also important to make some member functions and member data accessible so that the hidden data can be manipulated indirectly.

Let us understand this better with a real-life example:

Consider the Research and Analysis Wing (R&AW), having 10 core members, has come into possession of sensitive confidential information regarding national security. Now we can correlate these core members to data members or member functions of a class, which in turn can be correlated to the R&A Wing. These 10 members can directly access the confidential information from their wing (the class), but anyone apart from these 10 members can’t access this information directly, i.e., outside functions other than those prevalent in the class itself can’t access the information (that is not entitled to them) without having either assigned privileges (such as those possessed by a friend class or an inherited class, as will be seen in this article ahead) or access to one of these 10 members who is allowed direct access to the confidential information (similar to how private members of a class can be accessed in the outside world through public member functions of the class that has direct access to private members). This is what data hiding is in practice.

See also  Applications of Loops in C++

Types of Access Modifiers

The Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members, i.e., they set some restrictions on the class members so that they can’t be directly accessed by the outside functions.

There are 3 types of access modifiers available in C++:

  1. Public
  2. Private
  3. Protected

Note: If we don’t specify any access modifier for the members inside the class, then by default the access modifier for the members will be Private.

Now let’s look into each type of access modifier in detail.

Public:

All the class members declared under the public specifier will be available to everyone. The data members and member functions declared as public can be accessed by any other classes and functions in the program. We can access the public members of a class directly in the program by using the operator (.) with the name of the object that has been created.

Example:

Output:

Radius is: 5.5
Area is: 94.985

In the example program, the data member radius is declared as public so it could be accessed outside the class and thus was allowed to access inside the main().

Private:

The class members declared as private can be accessed only by the public member functions inside the class. They cannot be accessed by any other object or function outside the class. Only the member functions also called the friend functions are allowed to access and modify the private data in the class.

Example:

Output:

In function 'int main()':
11:16: error: 'double Circle::radius' is private
         double radius;
                ^
31:9: error: within this context
     obj.radius = 1.5;
         ^

The output of the above program is a compile-time error because we are not allowed to access the private data of members of a class directly from outside the class. Yet access to obj.radius is attempted, but radius being a private data member, we obtained the above compilation error.

See also  Lambda Expressions in c++

However, we can access the private data of members of a class indirectly using the public member functions of the class.

Example:

Output:

Radius is: 1.5
Area is: 7.065

Protected:

The protected access modifier is similar to the private access modifier in the sense that it can’t be accessed outside of its class unless with the help of a friend class also called the child class. The difference is that the class members declared as Protected can be accessed by any subclass (derived class) of that class.

Note: This type of access through inheritance can alter the access modifier for the elements of the base class in the derived class depending on the mode of Inheritance.

Example:

Output

id_protected is: 81

 That is all for this article. Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.