Is there a point of private class members at a low level?

In C++ class members be default are private so they can’t be read or modified except from within the class. However in CC’s lesson you build public functions who’s only purpose is to return the private variable. For example:

#include <iostream>

class Player {
  std::string name;

  void change_name(std::string new_name) {
    name = new_name;

  std::string get_name() {
    return name;

On a large scale, or a massive program I can see the use of having members private. For example if you are working with a team and the function needs to perform actions before or after changing the variable than it would be bad if someone changed a single one without taking the actions that were supposed to accompany the change.

Or if you have a class who variables depend on each other:

class equilateral_triangle {
  int side1_length;
  int side2_length;
  int side3_length;

  equilateral_triangle(int length) {
    side1_length = length;
    side2_length = length;
    side3_length = length;
  void change_sides(int new_length) {
    side1_length = new_length;
    side2_length = new_length;
    side3_length = new_length;

In this instance it makes sense that you wouldn’t change the lengths directly, because if you did not change all of them at the same time it would void the object from being an equilateral triangle. Adding a function that changes all sides at once would prevent you from only changing one or two.

However on a low level scale, like CC’s lessons, is there actually a purpose to the private members and then a function which’s purpose is only to change or return a variable?
If a variable can be read and modified independent of others without causing a problem, is there a reason not to make it public?

I don’t know anything about C++, but I would assume there is no reason not to. In fact, if the variable is not in a position to impact anything, why have it in the first place?

What about conditional statements? If you are creating a function dependent on the correct private variable (member?), using a function to return a private member then checking that against a condition might be useful.

1 Like

Hmm… That could be a case.
It does seem there are a multitude of reasons to keep class members private.

After looking at some examples, I think I was looking at CC’s lessons from the wrong perspective. They seem to be more so showing how to obtain a private variable rather than saying you have to keep them private.