Difference Between Private and Protected in C++

In this post, we will understand the difference between private and protected access modifiers in C++.
They are declared using the ‘private’ keyword, followed by a ‘:’.
They are declared using the ‘private’ keyword, followed by a ‘:’.
They can’t be accessed outside the class.
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.
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’.
Only member functions or friend functions are allowed to access data that is labelled as ‘private’.
 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;
}
/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 is similar to private access modifier.
Protected access modifier is similar to private access modifier.
They are declared using the ‘protected’ keyword, followed by ‘:’.
They are declared using the ‘protected’ keyword, followed by ‘:’.
Class member that is declared as ‘Protected’ can’t be accessed outside the class.
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 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 can also be accessed by a derived class whose parent class contains ‘protected’ members.
They are used while working with inheritance concept.
They are used while working with inheritance concept.
 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;
}
/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