Access specifiers
public, protected, and private control which names of a class other code can use. They are a compile-time check. They do not hide the data in the compiled binary and they do not affect layout beyond the order you already wrote the members.
Detailed Explanation
Encapsulation is the idea that a class's invariant is enforced because the only way to change the data is through the class's own functions. Access specifiers are the language's enforcement. They are not a security boundary. A const_cast or a memcpy of the object can still smash the bytes. They are a contract the compiler refuses to let callers ignore.
private is the default for a class (a struct defaults to public). Members are usable only by the class's own members, its nested types, and friends. Data members that participate in an invariant should be private. Helper functions that exist only to keep public methods short should be private.
public is the API. Constructors, the destructor, and the operations callers are supposed to use live here. If everything is public, you have a struct in the everyday sense: a bag of fields.
protected is "public to descendants, private to everyone else". Use it for methods a derived class must call to participate in a template-method setup. Protected data is usually a mistake: every descendant can break the invariant, and you have lost the single place that maintains it. Prefer private data plus protected accessors.
Access is checked at the name, not at the object. A member function of User can read other.id_ on a different User object. Privacy is per class, not per instance.
Inheritance access is a second switch. class D : public B keeps B's public names public on D and protected names protected. class D : protected B makes B's public names protected on D. class D : private B makes them private: inheritance for implementation, not for interface. struct defaults to public inheritance; class defaults to private. Write the specifier anyway.
A private base is still a base. D can use B's public members inside D's methods. Outside callers cannot convert D* to B* (except friends). That is "implemented in terms of", not "is a".
friend punches a hole. A friend function or class can name private members. Use it for operator<< and for tight helpers that cannot be members. Do not make every test a friend; prefer a public testing seam or a named factory.
Access specifiers can appear multiple times and can be mixed with members. Style: one public block, one protected block, one private block, in that order, so a reader of the header meets the API first.
Changing a private member still changes sizeof and layout. Callers must recompile. Access control is not ABI insulation; PIMPL is.
Code Examples
#include <stdexcept>
#include <string>
class Account {
public:
explicit Account(int cents) { credit(cents); }
int balance() const { return cents_; }
void credit(int cents) {
if (cents < 0) throw std::invalid_argument("negative");
cents_ += cents;
}
private:
int cents_ = 0; // invariant: cents_ >= 0
};
int main() {
Account a(100);
a.credit(50);
// a.cents_ = -1; // error: private
(void)a.balance();
}struct Engine { void start() {} };
class Car : private Engine { // implemented-in-terms-of, not is-a
public:
void drive() { start(); } // Car may use Engine
};
int main() {
Car c;
c.drive();
// static_cast<Engine*>(&c); // error: private base
}Common Gotchas
Protected data is public to an unbounded set of derived classes
future subclasses can break invariants you cannot see today. Keep data private.
`class` versus `struct` is default access, including default inheritance
`class D : B` is private inheritance. Write `public` if you mean is-a.
Friends are part of the API
a friend can depend on private layout. Changing a private name can break a friend in another header.
Access is not per-object
`void User::merge(User& other) { other.id_ = id_; }` compiles. Do not assume privacy stops "the other instance".