Access Modifiers in C++

Access Modifiers in C Plus Plus

Introduction to Access Modifiers in C Plus Plus:

Access modifiers in C Plus Plus 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 Plus Plus:

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:

[sc name=”compiler”]

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 Plus Plus:

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:

[sc name=”compiler”]

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:

[sc name=”compiler”]

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:

[sc name=”compiler”]

Advantages of Access Modifiers in C Plus Plus:

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.

References:

Leave a Reply

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