Encapsulation
Encapsulation is a programming concept that organizes code into units that contain both data and functions that operate on that data. It is not strictly related to Object-Oriented Programming (OOP) and is often used in other programming paradigms. Encapsulation allows us to decouple code into units with single responsibilities, making the code easier to reason about, improving readability, and facilitating maintenance.
In terms of OOP, encapsulation can also refer to hiding an object’s members or restricting access to these members from the outside. In C++, this can be achieved using access specifiers. C++ has the following specifiers:
- Public
- Private
- Protected
Public and private are the most commonly used specifiers. They give us the ability to control the interface of the class, that is, to control which class members are available to the users of a class. The following example demonstrates how to define a class with public...