Access Modifiers in C++

Introduction to Access Modifiers in C++

Access modifiers is the techniques that is applied to members of class to restrict their access beyond the class. In C++, access modifiers can be achieved by using three keywords – public, private and protected, public members can be accessed anywhere i.e. inside or outside the class but within the program only, private members can be accessed inside the class only, protected members are similar to the private, they can be accessed in the child class/derived class.

Types of Access Modifiers in C++

There are 3 types of Access Modifiers in C++

Start Your miễn phí PM Development Course

Website development, programming languages, PM testing & others

  1. Public
  2. Private
  3. Protected

Let’s take a look at these modifiers with examples:

1. Public

As the name suggests, available to all. All the members of the class will be available to everyone after declaring them as public. A public thành viên can be accessed anywhere outside the class but within a program. Data members can be also accessed by other classes if declared public. As there are no restrictions in public modifier, we can use the (.)dot operator to directly access thành viên functions and data.

Code:

#include <iostreamvàgt;
using namespace std;
class parent
{
private:
int p;
protected:
int q;
public:
int r;
parent() //constructor to initialize data members
{
p = 100;
q = 200;
r = 300;
}
};
//Inheritance concept
class child: public parent
{
//q becomes protected and r becomes public members of class child
public:
void showdata()
{
cout << "p is not accessible" << endl;
cout << "value of q is " << q << endl;
cout << "value of r is " << r << endl;
}
};
int main()
{
child c; //object of derived class(child)
c.showdata();
//c.p = 100;   invalid : private thành viên,no access
//c.q = 200;   invalid : q is now private thành viên of child class
//c.r = 300;   invalid : r is also now a private thành viên of child class
return 0;
}

Output:

Xem Thêm  Tải xuống Overwolf cho Windows - tải xuống overolf

public
2. Private

A private modifier is one of the best Access Modifiers in C++. The scope of private data members remains inside the class that’s why the function inside the class can access class members declared as private. Because that’s what private means only you decide who can use your things (like a friend) or not. You can’t access members directly by any object or function which is outside the class. A friend’s function (as I said can use your things )can be used to access private data members of the class. You will get a compile-time error while accessing private data members from anywhere outside the class.

Code:

#includevàlt;iostreamvàgt;
using namespace std;
// Defining class
class Circle
{    // private data thành viên
private:
double radius;
// public thành viên function
public:
void areaOfCircle(double r)
{   // thành viên function can access private
// data thành viên radius
radius = r;
double area = 3.14*radius*radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
} };
// Main function
int main()
{    // creating class object
Circle c;
/* trying to access private data thành viên
directly outside the class */
c.areaOfCircle(4.5);
return 0;
}

Output:

private

By using the above code we can access private data members of a class indirectly using the public thành viên functions of the class indirectly.

3. Protected

The last access specifier most importantly used as Access Modifiers in C++ as it’s behavior is quite similar to a private access modifier. Protected data members or functions can’t be access directly from other classes. You can use a friend function to access protected members as it allows this functionality. There are some restrictions on the protected modifier. Members declared in protected can only be protected up to the next level then it becomes private.

Xem Thêm  max

Code:

#include <iostreamvàgt;
using namespace std;
// Base class
class Parent
{    // Protected data members
protected:
int id_protect;
};
// Derived class
class Child : public Parent
{  public:
void set(int id)
{  /* Child class to access the inherited protected data
members of the Base class */
id_protect = id;
}
void display() {
cout << "id_protect is: " << id_protect << endl;
}
};
// Main function
int main() {
Child p;
/* thành viên function(derived class) can easily
access the data members(protected) of the base class */
p.set(07);
p.display();
return 0;
}

Output:

protected

From the above code, you can see that id_protect is declared as protected and can be accessed using the thành viên function of the derived class. This means you can access the protected data thành viên of the base class by using the thành viên function of the derived class.

Advantages of Access Modifiers in C++

Below are the different advantages of Access Modifiers in C++:

  • Access modifier provides you the authority to control your data depending upon the scenarios. If you are working in a ngân hàng tên miền then you have to use private data members to keep your data hidden from other users, authority is in your hand. You can make them public if you want to but it won’t be a great approach because in that case, anyone can change your data at any time
  • All base class public thành viên becomes public members of the derived class. In the same way, all base class protected thành viên becomes protected members of the derived class which will help you in managing data easily in every aspect because as you can see there is no change in accessing these members if you use public inheritance in your programming language.
  • In private inheritance scenario, all base class public thành viên becomes private members of the derived class. In the same way, all base class protected thành viên becomes private members of the derived class whereas, in protected inheritance scenario, all base class public thành viên becomes protected members of the derived class and all base class protected thành viên becomes protected members of the derived class. cảnh báo that in C++ access specification works on a per-class basis, not on a per-object basis.
Xem Thêm  bitmaprenderer""

Conclusion

Several programming languages don’t have private and protected access, therefore, any user can use it in the ways they want. C++ coders won’t trust users so they are not allowed to use it. As public data members can be a serious potential risk for bugs or hackers.

Recommended Articles

This is a guide to Access Modifiers in C++. Here we discuss the types of Access Modifiers in C++ along with its examples and some advantages. You may also look at the following articles to learn more-

0

Shares

Share

Viết một bình luận