• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Private and Protected in C++

private vs protectedThere 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

    1. Comparison Chart
    2. Definition
    3. Key Differences
    4. Conclusion

Comparison Chart

Basis for comparisonPrivateProtected
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 classYesYes
Accessible from derived classNo Yes
Accessible from outsideNoNo

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++

  1. 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.
  2. 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.

Related Differences:

  1. Difference Between Single and Multiple Inheritance
  2. Difference Between Virtual and Pure Virtual Function
  3. Difference Between Inheritance and Polymorphism
  4. Difference Between Friend Function and Friend Class
  5. Difference Between Stack and Queue

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy