There are three types of access protection defined in C++ programming language for hiding the data. Data hiding is an essential part of the object-oriented programming. The private and protected keywords offer the level of access protection to hide the data and function within a class. The private members cannot be inherited while the protected member can be inherited but in a limited range. These specifiers indicate the visibility of the members where private is more restrictive than protected.
Content: Private and Protected
Comparison Chart
Basis for comparison | Private | Protected |
---|---|---|
Inheriting property to the derived class | Derived class cannot access base class private members. | Derived class can access base class protected members. |
Accessibility | The private members of the class are inaccessible out of the class scope. | The protected members of the class are inaccessible out of the class scope except the class derived immediately. |
Accessible from own class | Yes | Yes |
Accessible from derived class | No | Yes |
Accessible from outside | No | No |
Definition of Private
The class members declared as private can only be retrieved within the class. In other words, the members declared in the private section of the class can only be accessed by the friend function and member function of the class. Private members are not obtainable to the derived class directly and are inaccessible out of the class scope.
The C++ program shows the concept of private access specifiers.
#include<iostream> using namespace std; class A { private: int i ; public: void get() { cout<<"\n Enter i = "; cin>>i; } }; class B : public A { public: void show() { i++; cout<<"\n Your entered number after increment ="<<i; } }; int main() { B obj; obj.get(); obj.show(); return 0; }
The program generates errors as private members cannot be accessed from the derived class.
There is another example to show the functionality of the private access specifiers.
#include <iostream> using namespace std; class mammal { public: void mammalMethod( void ) { cout<<"It comes under the mammal category."<<endl; } }; class marineMammal: private mammal { public: void marine_mammalMethod( void) { cout<<"The whale is a marine mammal."<<endl; mammalMethod(); } }; int main( void ) { marineMammal ob; ob.marine_mammalMethod(); return 0; } Output The whale is a marine mammal. It comes under the mammal category.
In the above-given program, one object of the child class is created with name “ob” and the member function of the parent class can be accessed through calling child class method “marine_mammalMethod”. Although, the parent method cannot be accessed directly with object “ob”.
Definition of Protected
The class members declared as protected in a class can only be accessible through the member function and friend function of the class. These protected members cannot be accessed out of the class scope except the child class (immediately derived from it). It serves the restricted purpose in inheritance and inherits the properties in a limited way.
The below given C++ program illustrates the concept of protected access specifiers.
#include<iostream> using namespace std; class account1 { protected: int x,y;//accessible to derived class public: void set(int a, int b) { x=a; y=b; } void show() { cout<<x<<" "<<y<<"\n"; } }; class account2: public account1 { int k; public://derived class may access the base class data members void setk() { k= x+y; } void showk() { cout<<k<<"\n"; } }; int main() { account2 obj; obj.set(2000,3000); obj.show(); obj.setk(); obj.showk(); return 0; } Output 2000+3000 5000
In the above example, child class inherit the properties of base class because the data members are declared as public or protected. It would not be possible if x and y had been declared as private in the base class.
Key Differences Between Private and Protected in C++
- The protected keyword can be used to build class members that are private to their class but can be inherited and used by the derived class. Conversely, this is not possible while using private, because private members cannot be inherited directly to the derived class.
- When the base class is inherited using the private access specifier, it makes all the public and protected members of the base class, the private members of derived class. As against, this is not the case in protected access specifiers.
Conclusion
Private and protected are the access specifiers that are used to set the visibility of the class and its members differently. However, protected is more flexible than private.
Leave a Reply