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
| public | private | |
|---|---|---|
| Methods of the same class | Can read and write | Can read and write |
Code in main | Can read and write | Cannot; compile error |
| Typical use | Functions other code should call | Fields and helpers |
| Class default | No — 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;
}