C++ Tutorial

C++ Access Specifiers

public, private, and protected control who can read and write members.

Who can touch a member

Access specifiers split a class into sections. Members after public: are open to any code that has an object. Members after private: are only for methods (and constructors) of that class.protected: sits between those two when inheritance is in play.

The specifier lasts until the next one. You can switch more than once in the same class. Put data you want to protect in private, and the functions other files should call in public.

public

Public members are the class's outer surface. main can read and write them. That was useful while you learned dots and objects. It is a poor default for real fields: any line of code can setscore = -9 and break a rule the class was supposed to keep.

Example

#include <iostream>
using namespace std;

class Meter {
 public:
  int reading;

  void show() {
    cout << reading << endl;
  }
};

int main() {
  Meter m;
  m.reading = 7;
  m.show();
  return 0;
}

private

Private members are invisible outside the class. main cannot write v.code. It must call a public method such as setCode. Methods inside Vault can still usecode directly.

Example

#include <iostream>
using namespace std;

class Vault {
 private:
  int code;

 public:
  void setCode(int c) {
    code = c;
  }

  int getCode() {
    return code;
  }
};

int main() {
  Vault v;
  v.setCode(42);
  cout << v.getCode() << endl;
  return 0;
}

Paste this into /cpp/try, then add v.code = 0; in main and compile. g++ should reject it: code is private.

public vs private

publicprivate
Methods of the same classCan read and writeCan read and write
Code in mainCan read and writeCannot; compile error
Typical useFunctions other code should callFields and helpers
Class defaultNo — you must write public:Yes — members start private

A class with no specifier at all treats members as private. That is why the earlier lessons wrotepublic: before the fields. Without it, p.name = "Rin" would not compile.

protected

protected: is like private for code in main, but a derived class can use those members. You need inheritance to see the difference. The inheritance chapter covers it. Until then, use public for the interface and private for the data.

Do not mark fields protected just to reach them from main. That is not what protected is for, and main still cannot touch them.

A constructor can set private fields

Constructors belong to the class, so they may assign private members. That is the clean way to start aVault with a code: pass it into the constructor, store it in the private field, never expose the field itself.

Example

#include <iostream>
using namespace std;

class Vault {
 private:
  int code;

 public:
  Vault(int c) : code(c) {}

  int getCode() {
    return code;
  }
};

int main() {
  Vault v(42);
  cout << v.getCode() << endl;
  return 0;
}

Hide the data, publish the operations

Access specifiers are the first half of encapsulation. The next chapter puts that into a habit: private fields, public getters and setters, and rules the class enforces in one place.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Physics

g stays private

Standard gravity used in school problems is 9.81 m/s². Making g private stops a later line overwriting it with 10 “for ease”. weight() is public because that is what callers need.

W = m g

Example

#include <iostream>
using namespace std;

class Earth {
  const double g = 9.81;
public:
  double weight(double mass) { return mass * g; }
};

int main() {
  Earth e;
  cout << e.weight(70.0) << " N" << endl;
  return 0;
}

Biology

A pulse you cannot scribble over

bpm is private. The constructor sets it. There is no setter here on purpose: a vitals object is a reading, not a dial you twist.

Example

#include <iostream>
using namespace std;

class Pulse {
  int bpm;
public:
  Pulse(int b) : bpm(b) {}
  int get() { return bpm; }
};

int main() {
  Pulse p(72);
  cout << p.get() << " bpm" << endl;
  return 0;
}

FAQ: C++ Access Specifiers

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ access specifiers examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ access specifiers in this C++ C++ lesson (C++ Access Specifiers).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.