VOOZH about

URL: https://www.geeksforgeeks.org/cpp/difference-between-private-and-protected-in-c-with-example/

⇱ Difference between Private and Protected in C++ with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between Private and Protected in C++ with Example

Last Updated : 12 Jul, 2025

Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.
Example:


Output
id_protected is: 81

The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
Example:


Output
Radius is: 1.5
Area is: 7.065
PrivateProtected
The class members declared as private can be accessed only by the functions inside the class.Protected access modifier is similar to that of private access modifiers.
Private members keep implementation details in a program.Protected members enhanced access for derived classes. 
Only the member functions or the friend functions are allowed to access the private data members of a class.The class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.
Private members are inherited by derived classes but are not directly accessible in the derived classes..Protected member are inherited in class.
Comment
Article Tags:
Article Tags: