Encapsulation in C++ means bundling data with the functions that operate on it and hiding the internal representation so outside code can only interact through a controlled public interface. You achieve it by making data members private, exposing public member functions that enforce rules, and keeping implementation details out of the interface.
The payoff is that a class can protect its invariants (for example, "balance is never negative") and can change its internals later without breaking the code that uses it.
Encapsulation has two parts that work together:
Without it, any code can set any field to any value. With it, every change passes through code that can check the change. Encapsulation is closely related to abstraction, which is about presenting a simple model, but they are different: abstraction chooses what to show, encapsulation enforces what is hidden.
Compare a class with public data to one that protects it.
Every path that changes celsius_ goes through set_celsius, so the rule cannot be bypassed. The constructor uses the same function, so a bad object can never be created.
Accessors are not encapsulation by themselves. A class that has a private field with a public getter and setter for every field is just a public struct with extra steps. Prefer behaviour over raw access.
Guidelines:
const.withdraw, rename, add_item) over setting fields.const reference. Never return a non-const reference or pointer to a private member, since that hands out write access.Even private members appear in the header, so changing them forces every includer to recompile. The pimpl (pointer to implementation) idiom moves the private data into a type defined only in the .cpp file.
This is a trade-off: it gives a stable binary interface and faster builds, at the cost of one heap allocation and an extra indirection. Use it for library boundaries, not for every class.
The three access levels give you fine control: private for details, protected for members that derived classes need, public for the interface. Keep as much private as you can; you can always open something up later, but closing it breaks callers.
friend declarations let a specific function or class see private members. Use them for tightly coupled helpers such as operator<< or a test fixture, because each friend is another place that depends on your internals.
private is security. It prevents accidental misuse at compile time only.Data hiding is one half of it. Encapsulation also means grouping the data with the functions that operate on it, so the class owns its rules.
No. Prefer meaningful operations. Add a getter only when callers need to read a value, and a setter only when any valid value is allowed.
Abstraction decides what the interface presents; encapsulation enforces that the rest stays hidden and inaccessible.
private members be accessed at run time?Technically, through pointer tricks that cause undefined behaviour. Access control is a compile-time check, so do not rely on it for security.
Achieve encapsulation in C++ by keeping data private, exposing a small public interface of meaningful operations, validating every change in one place, and never leaking references to internals. Use const accessors, apply pimpl when you need a stable interface, and keep friend rare.