Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation page of C++ access modifier: protected #5998

Merged
merged 20 commits into from
Jan 25, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
946f048
Added documentation page of C++ access modifier: protected
E1StOrm Jan 21, 2025
68d82bf
Merge branch 'main' into cpp_access_mod_protected
E1StOrm Jan 21, 2025
df17a41
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
5b514ef
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
348a85a
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
12f9f8a
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
5154edc
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
e82455f
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
8501c53
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
2c3f926
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
9ea962d
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
a462c17
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
3d61a00
Merge branch 'main' into cpp_access_mod_protected
E1StOrm Jan 22, 2025
bd883aa
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
127b02b
Update content/cpp/concepts/access-modifiers/terms/protected/protecte…
E1StOrm Jan 22, 2025
0ae169d
Merge branch 'main' into cpp_access_mod_protected
E1StOrm Jan 23, 2025
ad5fc75
Merge branch 'main' into cpp_access_mod_protected
PragatiVerma18 Jan 24, 2025
afdf9fb
Update protected.md
PragatiVerma18 Jan 24, 2025
481178c
Merge branch 'main' into cpp_access_mod_protected
Sriparno08 Jan 25, 2025
6175406
Minor changes
Sriparno08 Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions content/cpp/concepts/access-modifiers/terms/protected/protected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
Title: 'Protected'
Description: 'Allows class members to be accessed within their class and by derived classes.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
E1StOrm marked this conversation as resolved.
Show resolved Hide resolved
Tags:
- 'Classes'
- 'Encapsulation'
- 'Inheritance'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

In C++, the **`protected`** access specifier is used to define the accessibility of [class](https://www.codecademy.com/resources/docs/cpp/classes) members. Members declared as `protected` are accessible within the class itself and by its derived classes (subclasses).

This approach balances [encapsulation](https://www.codecademy.com/resources/docs/cpp/encapsulation) and [inheritance](https://www.codecademy.com/resources/docs/cpp/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:

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

## Example

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

```cpp
#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](https://www.codecademy.com/resources/docs/cpp/variables) `myVar` and a protected [function](https://www.codecademy.com/resources/docs/cpp/functions) `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.
Loading