C++ Encapsulation
The following two essential components are present in all C++ programmes:
- Functions are the parts of a programme that perform actions, and they are termed programme statements (code).
- Program data is the program's information that is influenced by the program's functions.
What do you mean by Encapsulation?
Data encapsulation is the act of combining data with the functions or procedures that operate on it to create a single unit that is shielded from outside intervention and misuse.
This is a crucial notion in object-oriented programming, and it leads to another OOP concept known as "data hiding." Abstraction exposes just the essential information or interfaces to the outside world, whereas encapsulation hides data and its members.
In a sense, abstraction shows the outer world a "abstract image" of the concealed facts. As a result, we've already established that encapsulation and abstraction are mutually exclusive.

Encapsulation is represented by a class in C++, which has data members and methods that operate on them, as well as access specifiers such as private, public, and protected.
We also know that class members are private by default. We are genuinely establishing encapsulation when we define class members as private and methods to access class members as public. At the same time, we provide the outside world with an abstract view of data in the form of public methods.

Encapsulation Implementation
Encapsulation is implemented in C++ as a class that encapsulates data and the methods that operate on it. Data is typically designated as private so that it cannot be accessed outside of the class. The public methods or functions are defined and may be accessed using the class's object.
However, we are unable to directly access private members, which is referred to as data masking. Data is secured and can only be accessed by functions of the class in which it is declared when this is done.
For example,
// Example program #include <iostream> #include <string> using namespace std; //example class to demonstrate encapsulation class sampleData{ int num; char ch; public: //getter methods to read data values int getInt() const{ return num; } char getCh() const{ return ch; } //setter methods to set data values void setInt(int num) { this->num = num; } void setCh(char ch){ this->ch = ch; } }; int main() { sampleData s; s.setInt(100); s.setCh('Z'); cout<<"num = "<<s.getInt()<<endl; cout<<"ch = "<<s.getCh(); return 0; }
Output
num = 100 ch = Z
We've created a class out of two member variables and the getter and setter methods in the previous example. This is an illustration of encapsulation.
We've defined two variables, num and ch, as private variables, making them inaccessible to the rest of the programme. They are only available to the public functions that we have disclosed. As a result, we have private variables in a class with concealed data members.
Creating a Strategy
Unless we have a specific need to reveal class members, most of us have learned to make them private by default. That is excellent encapsulation.
This is most commonly applied to data members, although it may be applied to any member, including virtual functions.

Encapsulation vs. Abstraction: What's the Difference?
Abstraction and encapsulation are inextricably linked. Encapsulation supports abstraction by grouping together data and functions that operate on that data.
Encapsulation | Abstraction |
The data is hidden. | Implementation is hidden. |
Combines data and procedures in a single package. | Provides a user with an abstract interface that only shows them what they need to know. |
Helps with abstraction. | Assists with code reuse and security. |
Access specifiers define access to data members and methods and are implemented as a class. | Implemented as an abstract class with non-instantiable interfaces. |
Access specifiers' role in encapsulation
As we saw in the last example, access specifiers are crucial in the implementation of encapsulation in C++. The implementation of encapsulation may be broken down into two steps:
- Using the private access specifiers, the data members should be identified as private.
- The public access specifier should be used to designate the member function that manipulates the data members.
What is the purpose of encapsulation?
- Encapsulation in C++ allows us to group together relevant data and methods, making our code clearer and easier to read.
- It aids in the management of our data members' modifications.
Consider the case when we want a class's length field to be non-negative. We can now make the length variable private and use the logic within the setAge function (). As an example,
class Rectangle { private: int age; public: void setLength(int len) { if (len >= 0) length = len; } };
- The getter and setter methods provide our class members read-only or write-only access. As an example,
getLength() // provides read-only access setLength() // provides write-only access
- It aids in the decoupling of system components. We can, for example, divide code into numerous bundles.
Decoupled components (bundles) can be produced, tested, and debugged separately and simultaneously. And any modifications to one component have no bearing on the other components.
- Encapsulation can also be used to conceal data. If we alter the length and breadth variables in Example 1 to private or protected, access to these fields is limited.
They are also kept secret from the upper classes. This is known as data obfuscation.
Conclusion
One of the most significant properties of OOP is encapsulation, which allows us to conceal data. As a result, data is more secure and protected from unwanted usage.
Encapsulation supports abstraction by allowing us to offer only the appropriate interface to the end-user while hiding all other features.