Difference between Private and Protected in C++ with Example
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:
// C++ program to demonstrate // protected access modifier #include <bits/stdc++.h> using namespace std; // base class class Parent { // protected data members protected: int id_protected; }; // sub class or derived class class Child : public Parent { public: void setId(int id) { // Child class is able to access the inherited // protected data members of the base class id_protected = id; } void displayId() { cout << "id_protected is: " << id_protected << endl; } }; // main function int main() { Child obj1; // member function of the derived class can // access the protected data members of the base class obj1.setId(81); obj1.displayId(); return 0; } |
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:
// C++ program to demonstrate private // access modifier #include <iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: void compute_area(double r) { // member function can access private // data member radius radius = r; double area = 3.14 * radius * radius; cout << "Radius is: " << radius << endl; cout << "Area is: " << area; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.compute_area(1.5); return 0; } |
Radius is: 1.5 Area is: 7.065
Difference between Private and Protected
| Private | Protected |
|---|---|
| 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. |
| 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. |
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Public vs Protected in C++ with Examples
- Protected keyword in Java with examples
- Can a constructor be private in C++ ?
- Private Destructor
- Can virtual functions be private in C++?
- How to call private method from another class in Java with help of Reflection API?
- Can we access private data members of a class without using a member or a friend function?
- Publicly inherit a base class but making some of public method as private
- Difference between C and C++
- Difference between Python and C++
- Difference between Inheritance and Polymorphism
- Difference between Abstraction and Encapsulation in C++
- Difference between std::quick_exit and std::abort
- Difference between while and do-while loop in C, C++, Java
- Difference between Definition and Declaration
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

