Protected Class Members

Learn about protected class members in C++, including how and when to use them in your code, especially in the context of inheritance and class design
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
3D3D art showing a dragon character
Ryan McCombe
Ryan McCombe
Updated

In our earlier lesson on encapsulation, we saw how we can (and should) make some of the variables and functions of our class private.

This hides those functions and variables from the outside world. This makes our systems easier to design and use.

For example, we can create objects that allow the outside world to see our character’s health, but not change it:

class Character {
public:
  int GetHealth() { return mHealth; }

private:
  int mHealth { 100 };
};

Access Restriction with Inheritance

An interesting problem arises when we’re using inheritance in this context. Specifically, code in a derived class also can’t access any inherited private members.

Below, we have a new class that inherits from Character, but this code also can’t access the private mHealth variable:

class Character {
public:
  int GetHealth() { return mHealth; }

private:
  int mHealth { 100 };
};

class Healer : public Character {
public:
  void Heal(int Amount) { mHealth += Amount; }
};
error: 'Character::mHealth': cannot access private
member declared in class 'Character'

protected Members

Fortunately, we have an access level between public and private that solves this problem. This access level is called protected.

protected members cannot be accessed by functions that exist outside our class hierarchy, but they can still be accessed by code in child classes.

Below, we’ve changed mHealth to be protected rather than private:

#include <iostream>
using namespace std;

class Character {
public:
  int GetHealth(){ return mHealth; }

protected:
  int mHealth{100};
};

class Healer : public Character {
public:
  void Heal(int Amount){ mHealth += Amount; }
};

int main(){
  Healer Player;
  cout << "Health: " << Player.GetHealth();

  Player.Heal(50);
  cout << "\nHealth: " << Player.GetHealth();
}
Health: 100
Health: 150
Test your Knowledge

Access Modifiers

What are the three access specifiers we have for classes?

What is the difference between protected and private access?

Summary

We’ve now covered the three access levels we have in C++:

  • private members are only accessible within the same class they are defined
  • protected members are accessible within the same class, and any sub-class
  • public members are accessible from anywhere

Next Lesson: Member Initializer Lists

In the next lesson, we'll delve into Member Initializer Lists. Here's what we’ll learn:

  • Understanding Member Initializer Lists: Learn what member initializer lists are and how they differ from assignments in constructors.
  • Why we would want to use them: Discover the three reasons why member initializer lists are preferred over equivalent code in a constructor body.
  • Examples and Use Cases: See practical examples to illustrate the use of member initializer lists in various scenarios.

Was this lesson useful?

Next Lesson

Member Initializer Lists

This lesson introduces Member Initializer Lists, focusing on their advantages for performance and readability, and how to use them effectively
3D art showing a female blacksmith character
Ryan McCombe
Ryan McCombe
Updated
Lesson Contents

Protected Class Members

Learn about protected class members in C++, including how and when to use them in your code, especially in the context of inheritance and class design

3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
Inheritance
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 56 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

Member Initializer Lists

This lesson introduces Member Initializer Lists, focusing on their advantages for performance and readability, and how to use them effectively
3D art showing a female blacksmith character
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved