Difference Between Private and Protected in C++

C++Server Side ProgrammingProgramming

In this post, we will understand the difference between private and protected access modifiers in C++.

Private Access Modifier

  • They are declared using the ‘private’ keyword, followed by a ‘:’.

  • They can’t be accessed outside the class.

  • The ‘private’ keyword is an access modifier that ensures that the functions and attributes inside the class are accessed only by class members in which they have been declared.

  • Only member functions or friend functions are allowed to access data that is labelled as ‘private’.

Example

 Live Demo

#include <iostream>
using namespace std;
class base_class{
   private:
   string my_name;
   int my_age;
   public:
   void getName(){
      cout << "Enter the name... ";
      cin >> my_name;
      cout << "Enter the age... ";
      cin >> my_age;
   }
   void printIt(){
      cout << "The name is : " << my_name << endl;
      cout << "The age is: " << my_age << endl;
   }
};
int main(){
   cout<<"An object of class is created"<< endl;
   base_class my_instance;
   my_instance.getName();
   my_instance.printIt();
   return 0;
}

Output

/tmp/u5NtWSnX5A.o
An object of class is created
Enter the name... Jane
Enter the age... 34
The name is : Jane
The age is: 34

Protected Access Modifier

  • Protected access modifier is similar to private access modifier.

  • They are declared using the ‘protected’ keyword, followed by ‘:’.

  • Class member that is declared as ‘Protected’ can’t be accessed outside the class.

  • They can be accessed within the class in which they are declared.

  • They can also be accessed by a derived class whose parent class contains ‘protected’ members.

  • They are used while working with inheritance concept.

Example

 Live Demo

#include <iostream>
using namespace std;
class base_class{
   private:
   string my_name;
   int my_age;
   protected:
   int my_salary;
   public:
   void getName(){
      cout << "Enter the name... ";
      cin >> my_name;
      cout << "Enter the age... ";
      cin >> my_age;
   }
   void printIt(){
      cout << "The name is : " << my_name << endl;
      cout << "The age is: " << my_age << endl;
   }
};
class derived_class : public base_class{
   private:
   string my_city;
   public:
   void set_salary(int val){
      my_salary = val;
   }
   void get_data_1(){
      getName();
      cout << "Enter the city... ";
      cin >> my_city;
   }
   void print_it_1(){
      cout << "The salary is: " << my_salary << endl;
      printIt();
      cout << "The city is: " << my_city << endl;
   }
};
int main(){
   cout<<"Instance of derived class is being created.."<<endl;
   derived_class my_instance_2 ;
   my_instance_2.set_salary(100);
   my_instance_2.get_data_1();
   my_instance_2.print_it_1();
   return 0;
}

Output

/tmp/u5NtWSnX5A.o
Instance of derived class is being created..
Enter the name... Jane
Enter the age... 23
Enter the city... NewYork
The salary is: 100
The name is : Jane
The age is: 23
The city is: NewYork
raja
Published on 23-Mar-2021 11:42:39
Advertisements