Protected C++

Like many other programming languages, C++ also provides us with the feature of data hiding for security purposes. We use access modifiers within a C++ program to protect our data at different security levels. C++ includes the public, private and protected access specifiers to secure its variables and functions. The protected access specifier is a little different from both private and public. This article will focus on the “protected” access modifier in C++ code examples. So, let’s get started. Open the shell application, create a new c++ file and open it in the editor.

Example 01: Public vs. Private

Before focusing on the protected access specifier, we need to learn the working of a public and private access specifier. After using the C++ namespace at the start of a code, we have declared a class Test with one public variable “a” and one private variable “b”. We have initialized an object “t” of a test class in the main() method.

The value 10 is assigned to variable “a”, which is public. Using the “cout” object, we have displayed the value of variable “a” using the “t” object. The same test class object “t” has been utilized here to assign the value “12” to private data member “b”. Using the “cout” object again, we utilize the “t” object to display the variable “b” value. Last, the return 0 statement will exit the program successfully. It’s time to save this new C++ code first, and compile it on the shell.

You must ensure the C++ compiler “g++” is installed and fully configured in your system. So, we have tried the g++ compiler instruction on the shell following the name of a file “protected.cc”. Execution of this compiler instruction, we have the error on line 13 when we assign the value to the private variable “b” using the object “t”. This is because the private variable in the class Test cannot be used outside of this class, i.e., in any function or class. The public access modifier will allow every variable and function to be available to everyone. In contrast, the private access modifier doesn’t allow the access of variables and functions to anyone, i.e., every class.

Let’s see how we can use private data members to be displayed on the shell. We have updated the same C++ file in the class Test by declaring the two public functions, get() and set(). The implementation of both the functions has been done outside the “Test” class. The “get()” function is used to return the value of private data member “y” to the main() function for display.

The set() function uses the value passed by the main() function and assigns it to the private data member “y” of the class Test. Within the main() method, we have been using the object “t” of the class Test to assign value “10” to the public variable “x” of the class test. The cout statement displays the value of “x” on the shell. Now, we have been calling the set() function by object “t” to pass the value “14” to private data member “y” of class “Test”. The cout statement has called the “get” function to display the value of variable “y” on the shell. This code will allow us to display the private data member value on the shell using the same class public data functions. Let’s save this code and execute it:

We have both the values for “x” and “y” displayed on running this recently updated code:

Example 02:

Let’s take a look at the protected access modifier now. We have opened the code file and updated it a little. Added the required libraries along with the C++ namespace. Declared a Parent class and a Child class derived from the “Parent”. The parent class contains a protected variable “x” of integer type. The child class only contains two public functions, i.e., set and get. After the classes, we have implemented both the functions of the child class by setting the value of “x” and returning to the main() function.

The main() function is to create object “c” for the child class. This object is used to set the value for variable “x” and display it on the shell via the get() method. The use of an object to assign value to variable “x” will show an error upon the execution. Then, save the code and exit the file.

The output is the same as expected, i.e., error.

To avoid this error upon assigning the value to a protected data member, we should use the public data functions, i.e., set and get functions in the child class. Using the set() function, we can assign the value to the protected variable “x” as we did in the following image code. After that, we can easily use the get() function to display the protected data member on the shell within the cout object statement. Save this code and compile it after that:

After compiling the updated code, we have no compilation errors this time. We have the value of the protected variable “x” displayed on the shell screen using the execution query.

Example 03:

Let’s take a look at another example to use the protected access modifier. Within this example, we will be using multi-inheritance to make it clearer and add three classes A, B, and C. Class A is the parent class of B, and B is the parent of C. Both A and B contain the protected variables “x” and “y”. The class B and C contains functions as public data members, i.e., set() and get().

Class B uses its functions to set and return value for Class A protected data member “x” as B is its derived class. In contrast, class C uses its functions to set and get the value “y” from class B as class C is its derived class. Within the main() driver code function, we have created an object of class B and set the value for the “x” variable. The same object displays the value of variable “x” on the shell. The class C object has been created to set the value for the protected variable “y” and display it on the shell. Let’s run the following code to see the results:

The output shows the use of set() and gets() functions are replacing the already initialized value for both protected variables.


This was all about using a protected access modifier to secure the data members for classes in C++. To make it more clear, we have also discussed the example of using public and private data members. Also, we have discussed the inheritance to demonstrate the working and use of secure access specifiers in our code. Using the set() and get() function is a plus to avoid errors while executing this article. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.