Encapsulation, as the name suggests, is used to encapsulate data and methods together. It is used to hide the values from any direct access from outside the class. Just like a capsule, the data and methods are bundled together to protect them from being accessed. In this tutorial on Encapsulation in C++, you will explore all about encapsulation.
What Is Data Hiding?
Encapsulation usually involves the process of hiding the data. This data hiding can also be called information hiding, it is one of the essential features of object-oriented programming, which helps in hiding the details from outside class.
Data hiding is related to the two important concepts of oops, i.e., encapsulation and abstraction.
So now, you will understand encapsulation in C++.
What Is Encapsulation in C++?
Encapsulation is a feature that is used for information hiding. It is a process of binding the data and functions together into a single unit called class. This data is also known as data members, and functions can be called methods that work on these data members. Encapsulation restricts direct access to the class data by making the variables and functions private.
Now, you will understand why you need encapsulation in C++
Why Do You Need Encapsulation?
The data members and functions are important details that are present inside the class, and it is not safe to keep those methods publicly accessible. Encapsulation helps in hiding those essential details from users. It binds the data and methods together and keeps the details of the object hidden from direct access.
Encapsulation provides many advantages that you will discuss later in this tutorial. But first, you will understand how encapsulation hides the data.
C++ Access Modifiers
The data is kept hidden from outside the class with the help of access modifiers. The access modifiers or access specifiers are used to decide the scope of the class members, i.e., whether they are accessible or restricted to the members outside the class.
There are three types of access modifiers:
1. Private:The members having private scope are those members that can only be accessed from within the same class. Outside members or functions cannot access private members.
2. Public: The members having public scope are those members that can be accessed from inside as well as from outside, or in other words it can be accessed from anywhere.
3. Protected: The members with the protected scope cannot be accessed from outside the class but can easily be accessed from the derived class.
Now, go ahead and learn how the private data is accessed in C++.
Accessing the Private Data in C++
As you have understood, the data members having the private scope cannot be accessed from outside the class, so is there a way to access those data members?
These private data members can be accessed with the help of setter and getter methods. These methods are declared in the public scope inside the class and helps in accessing the private data.
The setter method is used to write the value or assign the value to a private variable inside the class.
The getter method returns the value or allows the function outside the class to read the data assigned to the private variable.
Now, you will understand it better with the help of an example.
In this example, there is a class named Company having a private variable salary. You need to access that private variable salary because only then you can assign the total value of salary to it.
The setSalary() function is a setter function and getSalary() function is a getter function. From the main function, two values are being passed to the setSalary() function as arguments i.e., 30000(full time salary) and 6000(overtime salary). Inside the setter function, you are adding both the salaries and assigning the total to the salary variable which is private.
The getSalary() function returns the value of the salary variable.
This is the output of the example.
Now, understand the benefits of encapsulation in C++.
Benefits of Encapsulation
- Encapsulation restricts direct access to the class members by hiding the data and providing security for the information.
- Encapsulation makes the code well-encapsulated, so it is easy to maintain and read the code, which further helps in understanding the code.
- Encapsulation helps reduce the code's complexity as the encapsulated classes are easy to manage and straightforward.
Difference Between Encapsulation and Abstraction
Encapsulation is a mechanism of hiding the code and data together into a single unit to secure them.
Abstraction is a mechanism to hide the implementation details and show the functionality to the user.
Encapsulation focuses on the internal working i.e how it should be done.
It doesn’t focus on the internal workings but focuses on what should be done.
Encapsulation is implemented with the help of access modifiers.
Abstraction can be achieved using the interface and abstract class.
Encapsulation works on the problems that arise at the implementation stage.
Abstraction works on the problems that arise at the design stage.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
After reading this tutorial on Encapsulation in C++, you would have understood what is data hiding and encapsulation in C++?. You understood why you need encapsulation in C++. You also learned about the access modifiers and how you can access the private data members with the help of some examples. In this tutorial, you also looked at the difference between encapsulation and abstraction.
If you are looking to build a career in software development, then you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can prove to be the perfect solution to help you build your career in the right direction.
Do you have any questions regarding this Encapsulation in C++ tutorial? If you do, then you can put them in the comments section. We’ll help you solve your queries. To learn more about Encapsulation in C++, click on the following link: Encapsulation in C++.