Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active December 10, 2020 15:38
Show Gist options
  • Save rpivo/91bf67029d44b10fb9083bab128e313a to your computer and use it in GitHub Desktop.
Save rpivo/91bf67029d44b10fb9083bab128e313a to your computer and use it in GitHub Desktop.
Public & Private Access Modifiers in C++

Inside C++ classes, access modifiers like public and private are written similarly to Python branches like if, else, and for statements.

Public & Private Access Modifiers in C++

class Thing {
  public:
    int x;
  private:
    int y;
};

int main() {
  Thing thing;

  thing.x = 0; // fine because it's public
  thing.y = 0; // error because it's private

  return 0;
}

Even though the two languages are doing completely different things here, you would write a branch of flow in Python like how C++'s class access modifiers are written, with a colon following the branching statement, and subsequent items within that branch uniformly indented underneath:

for digit in digits:
  result += pow(digit, len(digits))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment