Protected

Anonymous contributor's avatar
Anonymous contributor
Published Jan 25, 2025
Contribute to Docs

In C++, the protected access specifier is used to define the accessibility of class members. Members declared as protected are accessible within the class itself and by its derived classes (subclasses).

This approach balances encapsulation and inheritance by restricting access while enabling derived classes to extend functionality.

Syntax

The following syntax defines a base class with two protected members that can be accessed from derived classes:

class BaseClass {
protected:
  // Protected members
  int myVar;
  void myFunc();
};

Example

The following example demonstrates the use of the protected access specifier in C++:

#include <iostream>
using namespace std;
// Base class
class BaseClass {
protected:
int myVar;
void myFunc() {
cout << "Accessing protected function from BaseClass" << endl;
}
};
// Derived class
class DerivedClass : public BaseClass {
public:
void useProtectedMembers() {
myVar = 10; // Accessing protected variable
cout << "Protected variable value: " << myVar << endl;
myFunc(); // Accessing protected function
}
};
int main() {
DerivedClass obj;
obj.useProtectedMembers();
// The following lines will cause compilation errors if uncommented
// obj.myVar = 20;
// obj.myFunc();
return 0;
}

In this example:

BaseClass includes a protected variable myVar and a protected function myFunc(), which DerivedClass can access and modify through inheritance.

In the main function, useProtectedMembers() demonstrates accessing these members in a controlled manner.

Note: Attempts to access protected members directly from objects of BaseClass or DerivedClass result in compilation errors, maintaining encapsulation.

All contributors

Contribute to Docs

Learn C++ on Codecademy