Access Modifiers in C++


Access Modifiers are used to implement data hiding in object oriented programming. There are three types of access modifiers used in C++. These are public, private and protected. Details about these are given as follows.

Public Access Modifier

The data members and member functions in a class that are declared public are available to everyone, including other classes. They can be accessed from any place in the program using the dot operator with the class object.

A program that demonstrates public access specifier is given as follows.

Example

 Live Demo

#include<iostream>
using namespace std;
class Add {
   public:
   int a, b;
   void sum() {
      cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b;
   }
};
int main() {
   Add obj;
   obj.a = 2;
   obj.b = 5;
   obj.sum();
   return 0;
}

Output

Sum of 2 and 5 is 7

In the above program, the class Add has two public data members a and b. The function sum() displays the sum of a and b. This is seen below.

class Add {
   public:
   int a, b;
   void sum() {
      cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b;
   }
};

In the function main(), the object of class Add is created. Then a and b are initialized in main(). This can be done because they are public data types. Finally sum() is called that displays the sum of a and b. This is shown below.

Add obj;
obj.a = 2;
obj.b = 5;

obj.sum();

Private Access Modifier

The data members that are declared private are only accessible from the functions inside the class and not by any functions outside the class. Friend functions can also access the private data members of a class.

A program that demonstrates private access modifier is given as follows.

Example

 Live Demo

#include<iostream>
using namespace std;
class Add {
   private:
   int a, b;
   public:
   void setdata(int x, int y) {
      a = x;
      b = y;
   }
   void sum() {
      cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b;
   }
};
int main() {
   Add obj;
   obj.setdata(9,5);
   obj.sum();
   return 0;
}

Output

Sum of 9 and 5 is 14

In the above program, the class Add has two private data members a and b. The function setdata() provides the values of a and b as they are private variables. The function sum() displays the sum of a and b. This is seen below.

class Add {
   private:
   int a, b;
   public:
   void setdata(int x, int y) {
      a = x;
      b = y;
   }
   void sum() {
      cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b;
   }
};

In the function main(), the object of class Add is created. Then the function setdata() is called to initialize a and b as they are private variables. Finally sum() is called that displays the sum of a and b. This is shown below.

Add obj;
obj.setdata(9,5);
obj.sum();

Protected Access Modifier

The data members of the class that are declared protected are similar to those declared private. They cannot be directly accessed outside the class but they can be accessed by the derived class of the base class.

A program that demonstrates protected access modifier in C++ is as follows −

Example

 Live Demo

#include<iostream>
using namespace std;
class Parent {
   protected:
   int a, b;
};
class Child: public Parent {
   public:
   void getdata(int x, int y) {
      a=x;
      b=y;
   }
   void putdata() {
      cout<<"a = "<<a<<endl;
      cout<<"b = "<<b<<endl;
   }
};
int main() {
   Child obj;
   obj.getdata(9,1);
   obj.putdata();
   return 0;
}

Output

a = 9
b = 1

In the above program, the parent class has two protected variables a and b. This is shown below.

class Parent {
   protected:
   int a, b;
};

The Child class has two functions getdata() and putdata() that take the values of a and b and display them respectively. These functions can do this as a and b are protected variables and Child is a derived class of Parent. The code snippet for this is given below −

class Child: public Parent {
   public:
   void getdata(int x, int y) {
      a = x;
      b = y;
   }
   void putdata() {
      cout<<"a = "<<a<<endl;
      cout<<"b = "<<b<<endl;
   }
};

In the function main(), an object obj of Child class is created. Then the functions getdata() and putdata() are called. This is shown below.

Child obj;
obj.getdata(9,1);
obj.putdata();

Updated on: 24-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements