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:
- The
Baseclass has a protected memberprotectedValue, which is initialized using its constructor. - The
Derivedclass inherits from theBaseclass and accesses theprotectedValuemember in itsdisplaymethod. - In the
mainfunction,obj.display()successfully displays the value of the protected member, but direct access toprotectedValueoutside 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:
- The
Baseclass has a protected memberprotectedValue. - The
Intermediateclass inherits fromBaseand provides a methodmodifyValueto modify the protected member. - The
Derivedclass inherits fromIntermediateand accesses the protected member in itsdisplaymethod. - In the
mainfunction, the protected member is modified and displayed through methods in the derived classes.
Key Points about protected Keyword
- The
protectedkeyword allows access to members within the class and its derived classes. - Protected members cannot be accessed directly from outside the class or its derived classes.
- It is commonly used in inheritance to allow derived classes to access or modify base class members.
- Encapsulation is maintained while still enabling flexibility in inheritance hierarchies.
- By default, all members of a structure (
struct) arepublic, but usingprotectedchanges the access level for specified members.
