What is Access Modifiers in C++

Introduction to Access Modifiers in C++:

Access modifiers are a technique that is applied to members of a class and restricts access between classes. In C ++, access modifiers can be accomplished using three keywords. Public, private, protected and public members can be accessed from anywhere inside and outside the class, but only within the program and only private members can be accessed inside the class. It can be accessed by both child / derived and private classes.

Types of Access Modifiers in C++:

There are 3 types of Access Modifiers in C++

  1. Public
  2. Private
  3. Protected

These access modifiers are used to demarcate the availability of members of classes that are members of data or member functions.

The program access modifier is followed by a colon. You can use one, two, or all three modifiers on the same class to set different limits for different members of the class. The limits of all subsequent declarations change.

1. Public Access Modifier in C++:

As the name implies, it is available to everyone. All members of the class will be available to anyone after declaring it public. Public members can access it from anywhere in the program outside of class. If it is declared public, other classes can also access the data members. There are no restrictions on the public modifier, so you can use the period (.) Operator to directly access member functions and data.

For example:

Practice Your Code Here

In the above program, since the radius of the data member is declared as public, it can be accessed from outside the class, so access is allowed from within main ().

2. Private Access Modifier in C++:

Private modifiers are one of the best access modifiers in C ++. The scope of the private data members remains within the class, so functions within the class can access the class members declared as private. It’s private means that you are the only one who decides who can use yours (like your friends). Members cannot be accessed directly from objects or functions outside of the class. You can use your friend’s role (you can use yours, as I said) to access the private data of your class members. Accessing private data members from anywhere outside the class will result in a compile-time error.

For example:

Practice Your Code Here

The output of the above program is a compile-time error because you are not allowed to access the private data members of the class outside of the class. Still, an attempt is made to access obj.radius and a compilation error occurs because radius is a private data member.

However, you can use the public member functions of the class to indirectly access the private data members of the class.

For example:

Practice Your Code Here

3. Protected Access Modifier in C++:

The last access specifier, the most important one used as an access modifier in C ++, is because its behavior is very similar to that of a private access modifier. Other classes cannot directly access protected data functions or members. The friend function enables this function so that you can access protected members. Protected modifiers have some restrictions. Members declared protected can only be protected to the next level and then become private.

Note: This inheritance access can change the access modifiers of the base class elements of the derived class, depending on the mode of inheritance.

For example:

Practice Your Code Here

Advantages of Access Modifiers in C++:

The following are several benefits of C ++ access modifiers.

  • Access modifiers provide permission to control data based on scenario. If you are working on a banking domain, you must use private data members to hide your data from other users. Authority is in your hands. You can post it if you want, but that’s not a good approach because anyone can change the data at any time.
  • The public members of all base classes are public members of derived classes. Similarly, a protected member of each base class becomes a protected member of a derived class. Using public inheritance in your programming language does not change access to these members, making it easy to manage your data in all respects.
  • In a private inheritance scenario, the public members of all base classes are private members of the derived class. Similarly, the protected members of all base classes are private members of the derived class, but in a protected inheritance scenario, all public members of the base class are protected members of the derived class and all. A protected member of the base class becomes a protected member of the derived class. Note that in C ++, the access specification works by class, not by object.

Summary: public, private, and protected:

  • Other classes and functions can access public items.
  • Private items cannot be accessed from outside the declared class, except for friend classes and functions.
  • Protected items are the same as private items, except that they can be accessed using derived classes.

Conclusion:

Some programming languages do not have private or protected access, so any user can use them as they wish. The C ++ encoder doesn’t trust the user, so the user can’t use it. Members of the public data can pose a serious potential risk for bugs and hackers.

Recommended Articles:

  1. Friend Class and Friend Function in C++
  2. What is Copy Constructor in C++
  3. What is Constructor and Destructor in C++
  4. What is static data members in C++
  5. What is Inheritance in C++

References:

  • https://www.programiz.com/cpp-programming/access-modifiers#:~:text=The%20access%20modifiers%20of%20C%2B%2B,tampering%20with%20the%20class%20data.
  • https://www.w3schools.com/cpp/cpp_access_specifiers.asp
  • https://www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm
  • http://www.trytoprogram.com/cplusplus-programming/access-specifiers/

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 640 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] What is Access Modifiers in C++ […]

You cannot copy content of this page
Scroll Up
1
0
Would love your thoughts, please comment.x
()
x