Updated March 17, 2023
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++
- Public
- Private
- 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 member 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 member functions and data.
Code:
#include <iostream>
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 member,no access
//c.q = 200; invalid : q is now private member of child class
//c.r = 300; invalid : r is also now a private member of child class
return 0;
}
Output:
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:
#include<iostream>
using namespace std;
// Defining class
class Circle
{ // private data member
private:
double radius;
// public member function
public:
void areaOfCircle(double r)
{ // member function can access private
// data member 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 member
directly outside the class */
c.areaOfCircle(4.5);
return 0;
}
Output:
By using the above code we can access private data members of a class indirectly using the public member 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.
Code:
#include <iostream>
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;
/* member function(derived class) can easily
access the data members(protected) of the base class */
p.set(07);
p.display();
return 0;
}
Output:
From the above code, you can see that id_protect is declared as protected and can be accessed using the member function of the derived class. This means you can access the protected data member of the base class by using the member 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 bank domain 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 member becomes public members of the derived class. In the same way, all base class protected member 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 member becomes private members of the derived class. In the same way, all base class protected member becomes private members of the derived class whereas, in protected inheritance scenario, all base class public member becomes protected members of the derived class and all base class protected member becomes protected members of the derived class. Note that in C++ access specification works on a per-class basis, not on a per-object basis.
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-