Access Modifiers in C++






Access modifiers are one of the important parts of OOPs concepts of C++. They are also known as "Data Hiding" concept in the OOPs
world. They mainly used inside the Classes in C++. In the upcoming chapters will also discuss Classes and Objects in detail.
Access modifiers are used to provide the accessibility categories to the class member variables and member functions. It means it will
give the feature of hiding some of the members of the class from the outside world and expose other members which need to accessed
from the outside world.
There are three types of access modifier in C++ :

  • public
  • private
  • protected

Let's see details about each of them.


Public Member:

When we need to expose the class members to the outside world then we have to declare them as "public" access modifiers. This means
all the other classes and functions can now easily access the public member variables and the functions of that class. To access the public
member function of a class one needs to create the object of that class and then access the member using the dot operator. We can see
the usage detail in the below code example.

              
                // Demo code for public members 
                #include < iostream>
                using namespace std;
                 
                class volume{
                  //declearing public members
                  public:
                    int length, breadth, height;
                    void set_dimension(int l, int b, int h);
                    int get_volume(void);
                };
                  
                //defining public members functions
                void volume::set_dimension(int l, int b, int h){
                  length = l;
                  breadth = b;
                  height = h;
                }
                  
                //defining public members functions
                int volume::get_volume(void){
                  int volume = 0;
                  volume = length * breadth * height;
                  return volume;
                }
                  
                int main(){     
                  volume obj; // Creating object of volume class 
                  obj.set_dimension(10,15,5);
                     
                  // accessing public members of volume class form its object
                  cout<< "Dimension set are:- ";
                  cout<< "\nlength = "<< obj.length;
                  cout<< "\nbreadth = "<< obj.breadth;
                  cout<< "\nheight = "<< obj.height;
                  cout<< "\nCalculated volume is: "<< obj.get_volume();
                     
                  return 0;
                } 
              
            

The output will be:

              
                Dimension set are:- 
                length = 10
                breadth = 15
                height = 5
                Calculated volume is: 750
              
            

In the above class named volume, we have defined three public member variable named length, breadth, height and two public member
functions set_dimension() and get_volume(void). After that from the main function we are accessing these from the object obj of the
volume class.


Private Member:

Whenever we need to hide the class members from the outside world then we have to declare them as "private" access modifiers. These
members are only for class internal use not accessible by any other class of outside functions.
Below is the code example for the same.

              
              // Demo code for private members 
              #include < iostream>
              using namespace std;
                 
              class area{
                  //declearing public members
                  public:
                  void set_dimension(int s);
                  int get_area(void);
                  
                  //declearing private member
                  private:
                  int side;
              };
              
              //defining public members functions
              void area::set_dimension(int s){
                  side = s;
              }
              
              //defining public members functions
              int area::get_area(void){
                  int area = 0;
                  area = side * side;
                  return area;
              }
              
              int main(){
                  area area_obj; // Creating object of volume class 
                  
                  area_obj.set_dimension(15);
                  
                  // accessing private member of area class form its object
                  // cout<< "\nvalue of side = "<< area_obj.side; // NOK: this will give error on compilation.
                  cout<< "\nCalculated area is: "<< area_obj.get_area();
                  
                  return 0;
              } 
              
            

The output will be:

              
                Calculated area is: 225
              
            

Here side is the private member of the area class. When we try to access it using the object of the class, in line
cout<< "\nvalue of side = "<< area_obj.side;
the compiler will throw an error, saying that "side" is deleared as private. This can only be accesed from the member function of the area
class in the way we are doing inside the function set_dimension().


Protected Member:

Protected members are almost similar to that of the Private members, which means they are not accessible from outside of the class. But
protected members have one advantage over the private members. That is when we declared a child class from the existing class then
protected members of the parent class will be accessible by the protected members of the child class.
Below is the code example for the same.

                
                // Demo code for protected members 
                #include < iostream>
                using namespace std;
                   
                class parent{
                    //protected members of parent class
                    protected:
                    int balance;
                };
                
                // creating child class from parent class
                class child:protected parent{
                    //public members of child class
                    public:
                    void setValue(int val);
                    int getValue(void);
                };
                
                //defining public members functions of child class
                void child::setValue(int val){
                    balance = val;
                }
                
                //defining public members functions of child class
                int child::getValue(void){
                    return balance;
                }
                
                int main(){
                    parent p_obj; // Creating object of parent class 
                    child c_obj; // Creating object of child class 
                    
                    //p_obj.balance = 60; //NOK: This will gives error
                    //c_obj.balance = 50; //NOK: This will gives error
                    c_obj.setValue(150);
                    
                    cout<< "\nBalance value is: "<< c_obj.getValue();
                    
                    return 0;
                }
                
              

The output will be:

              
                Balance value is: 150
              
            

Here inside the main() function we are trying to access the protected member using the object of both parent and child class.
p_obj.balance = 60;
c_obj.balance = 50;
But both of these lines will throw the compilation error as we cannot access the protected member directly similar to private member.
So the correct way to access the protected member balance is from the member function of child class that is setValue().
Protected member is mainly used in case of Inheritance, which we will see in the upcoming chapters.


Bonus point to remember:

  • If we will not provide any access modifer to class member then by default it is "private".
  • In the special cases if we want to access the private members of a class thenwe have to use the concept of "friend function" and "friend classes".
    Please check chapter "Friends in C++" for details.