C++ protected Keyword

The protected keyword in C++ is an access specifier used to control the accessibility of class members. Members declared as protected can be accessed within the class in which they are declared and by derived classes. However, they are not accessible from outside the class or its derived classes.

The protected access specifier is typically used in inheritance scenarios to allow derived classes to use or modify the base class’s members while still restricting access from the outside.


Syntax

</>
Copy
class ClassName {
protected:
    // Protected members
};
class
The keyword used to define a class.
protected
The access specifier indicating that the members below it are accessible only within the class and its derived classes.
members
The data members or member functions that are accessible within the class and derived classes.

Examples

Example 1: Accessing Protected Members in a Derived Class

In this example, you will learn how a derived class can access the protected members of its base class.

</>
Copy
#include <iostream>
using namespace std;

class Base {
protected:
    int protectedValue;

public:
    Base(int val) : protectedValue(val) {}
};

class Derived : public Base {
public:
    Derived(int val) : Base(val) {}

    void display() {
        cout << "Protected Value: " << protectedValue << endl;
    }
};

int main() {
    Derived obj(42);
    obj.display(); // Access protected member through derived class

    // Uncommenting the line below will cause a compilation error
    // cout << obj.protectedValue;

    return 0;
}

Output:

Protected Value: 42

Explanation:

  1. The Base class has a protected member protectedValue, which is initialized using its constructor.
  2. The Derived class inherits from the Base class and accesses the protectedValue member in its display method.
  3. In the main function, obj.display() successfully displays the value of the protected member, but direct access to protectedValue outside the class results in a compilation error.

Example 2: Protected Members in a Multi-Level Inheritance

In this example, you will learn the use of protected members in a multi-level inheritance hierarchy.

</>
Copy
#include <iostream>
using namespace std;

class Base {
protected:
    int protectedValue;

public:
    Base(int val) : protectedValue(val) {}
};

class Intermediate : public Base {
public:
    Intermediate(int val) : Base(val) {}

    void modifyValue(int newValue) {
        protectedValue = newValue; // Access and modify protected member
    }
};

class Derived : public Intermediate {
public:
    Derived(int val) : Intermediate(val) {}

    void display() {
        cout << "Protected Value: " << protectedValue << endl;
    }
};

int main() {
    Derived obj(10);
    obj.display();

    obj.modifyValue(20); // Modify protected member using Intermediate class
    obj.display();

    return 0;
}

Output:

Protected Value: 10
Protected Value: 20

Explanation:

  1. The Base class has a protected member protectedValue.
  2. The Intermediate class inherits from Base and provides a method modifyValue to modify the protected member.
  3. The Derived class inherits from Intermediate and accesses the protected member in its display method.
  4. In the main function, the protected member is modified and displayed through methods in the derived classes.

Key Points about protected Keyword

  1. The protected keyword allows access to members within the class and its derived classes.
  2. Protected members cannot be accessed directly from outside the class or its derived classes.
  3. It is commonly used in inheritance to allow derived classes to access or modify base class members.
  4. Encapsulation is maintained while still enabling flexibility in inheritance hierarchies.
  5. By default, all members of a structure (struct) are public, but using protected changes the access level for specified members.