Difference Between Private and Protected in C++



In this article, we'll explain the difference between private and protected access specifiers in C++. Access specifiers in C++ control who can access the variables and functions inside a class. Two commonly used specifiers are private and protected.

Private members are accessible only within the class, while protected members can be accessed by the class and its derived classes.

We'll show how each specifier works in C++, especially with inheritance, using simple examples to make it easy to understand.

Private Access Modifier

When we declare a class member as private, it means that this member is only accessible within the class itself. This means that the data is hidden from outside code, and even from derived classes that inherit from this class. The private modifier protects sensitive data, allowing it to be modified only by class functions.

Key Points About Private Access Modifier

Below are key points about the private access modifier:

  • Access Control: Can only be accessed within the class where they are declared.
  • Purpose: Used to protect sensitive data from direct external access.
  • Encapsulation: Helps in hiding implementation details and prevents unauthorized modifications.

Example

In this example, we have a class MyClass with a private member x, which is initialized by the constructor. We can't access x directly (as shown by the commented line obj.x = 5;) because it's private. We can only access it via the public method showX().

#include <iostream>
using namespace std;

class MyClass {
private:  // Private access modifier
    int x;  // This member is private

public:
    MyClass(int val) {  // Constructor to initialize 'x'
        x = val;
    }

    void showX() {  // Public function to access 'x'
        cout << "Value of x is: " << x << endl;
    }
};

int main() {
    MyClass obj(10);
    
    // obj.x = 5;  // Error: Cannot access 'x' directly because it's private

    obj.showX();  // Valid: Accessing 'x' via a public function
    return 0;
}

The output code shows how a private member (x) in the class can be accessed through a public function, as it cannot be accessed directly outside the class.

Value of x is: 10

Protected Access Modifier

The protected access modifier is similar to private, but with one key difference: protected members can be accessed within the class and by any derived class (subclass). This makes protected more flexible than private, as it allows derived classes to access and modify certain base class members while still keeping the member hidden from outside code.

Key Points About Protected Access Modifier

Below are key points about the protected access modifier that define its behavior in class inheritance and accessibility:

  • Access: It can be accessed within the class and by derived classes.
  • Purpose: It is used when you want to allow derived classes to access or modify base class data, but still keep it hidden from other external access.
  • Encapsulation: It helps maintain controlled access to data, especially in inheritance structures.

Example

In this example, the class Base has a protected member y, which the derived class Derived can access directly. Although y is protected, we can access it inside Derived through the showY() method. We can't access y directly from outside the class (as shown by the commented line obj.y = 5;).

#include <iostream>
using namespace std;

class Base {
protected:  // Protected access modifier
    int y;  // This member is protected

public:
    Base(int val) {  // Constructor to initialize 'y'
        y = val;
    }
};

class Derived : public Base {  // Derived class
public:
    Derived(int val) : Base(val) {}  // Constructor to call the base class constructor

    void showY() {  // A public function to access 'y'
        cout << "Value of y is: " << y << endl;  // Accessing protected member in derived class
    }
};

int main() {
    Derived obj(20);
    
    // obj.y = 5;  // Error: Cannot access 'y' directly because it's protected in the base class

    obj.showY();  // Valid: Accessing 'y' via a public function in the derived class
    return 0;
}

The output code shows how a protected member (y) in the base class can be accessed by a derived class through a public function.

Value of y is: 20

Key Differences Between Private and Protected

This table highlights the differences between private and protected access modifiers in terms of accessibility within classes, derived classes, and external code.

Feature Private Protected
Access in the Same Class Members are accessible only within the same class. Members are accessible within the same class.
Access in Derived Class Members are not accessible in derived classes. Members are accessible in derived classes.
Access Outside the Class Members are not accessible from outside the class. Members are not accessible from outside the class.
Use Case Used when you need to fully hide data from both subclasses and external code. Ideal when you want to share data with derived classes but keep it hidden from the outside world.
Encapsulation Control Provides stronger encapsulation by restricting access even to derived classes. Allows controlled access in derived classes, but still prevents external code from accessing the data.

Conclusion

In C++, both private and protected access modifiers control how class members can be accessed, but they do so in different ways:

  • Private is the most restrictive. It hides members from everyone, including derived classes, so only the class itself can access or modify its private members.
  • Protected is more flexible. It allows derived classes to access and modify members, but still keeps them hidden from outside code.

Understanding these differences helps you choose the right access modifier for improved security and easier code maintenance.

Updated on: 2025-03-03T13:16:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements