Updated July 4, 2023
Introduction to Encapsulation in C++
Encapsulation means the process of wrapping up the data and functions in a single capsule. It also safeguards the data from other classes by limiting access.
It hides the data. If we take a real-world example of college, we have different departments like Physics, Chemistry, and Biology, etc. A situation may arise where the Head of the Physics department needs some information from the Biology department; he can’t access the data from that department directly. So first, he should contact the Head of the Biology department, then request him to give the data. This is how encapsulation works.
How to Use Encapsulation in C++?
To achieve this, we have to follow the following steps:
- First, we need to make all the data members private.
- Then public getter and setter functions should be created for each data member so that the get function gets the value of the data member and the set function sets the value of the data member.
Encapsulation and data hiding can be achieved in C++ using user-defined Classes types. The access specifiers in classes can be private, protected, or public. By default, all the items in a class are private. However, according to the need, we can change the access levels.
Three levels of access specifiers are as below:
- Private: Members of the same class can access the data.
- Public: All the classes can access the data.
- Protected: Access to data is permitted to members of the same class or derived classes.
The best use of encapsulation is done only when we use either private or protected. When using public, we must ensure we know its proper need in the code.
To explain this, we will take a look at the below class.
Here physics, chemistry, and biology are of the type double and are private variables. GetTotalMarks ( ) is a public method to retrieve the total marks of all three subjects. We can’t access each subject in another class because of its protection level. But we can access the method to retrieve the total marks by passing individual subject marks. We can set the marks of each subject by using the setter method, which we will look at in the next example.
Example of Encapsulation in C++ with Steps
Below is the step-by-step instruction to implement the encapsulation.
1. Let us consider the scenario where we need to calculate the total marks of the Student by calculating the sum in three subjects, i.e., Physics, Chemistry, and Biology. But the condition is such that another class.
2. First, include the iostream for the input and output functionality.
3. Use the namespace std.
4. Write a class to hold all the data and functions. Let it be, Student.
5. Declare the private variables physics, chemistry, and biology. This tells us we can’t access the variables outside this Student class. But we can access them locally and modify the value if needed. We can write a public setter function and access it in another class to modify the value.
6. Write a function called SetMarks, which will be used to set the values of the private variables. Since we can’t access the private variables physics, chemistry, and biology outside the student class, we need this function to set these values from the external class. This method will be public as we must access it outside the class. We need three parameters to get the values for each subject. Inside the function, we will set the value of the private variables.
7. We need one more method to get the total marks from all three subjects by adding the marks. This method is also public. Just call this method outside the class, and we will get the sum of the marks for the three subjects.
8. Now, outside this class, we will write the main function.
9. Inside the main function, first, we will access the class student and create an object of the class.
10. In the next step, we will call the function to set the marks of the three subjects. Here we will pass the marks as parameters to the function. Below is the piece of code to achieve this.
11. Next, we will get the total marks and print the value. And return 0 at the end.
12. Can you guess the output of the code? You are right. The output is shown below.
Output: 240
13. Now, we will analyze how we got this output. Since we could not access the private variables, we wrote a Setter function that takes values from other classes and modifies the variables. The getter function fetches the variables, adds them, and sends the total marks of the Student. When we pass three different values to the setter function, we can see that the total marks we get will also vary accordingly.
14. From this, we can say that encapsulation helps us protect some of the needed data; also, setter and getter functions alter their values by doing this. Protection is also achieved without hampering our purpose.
Conclusion
In this article, we learned how encapsulation can be achieved in C++. By doing this, we can achieve the following benefits. Classes after encapsulation increase readability and reduce complexity. It helps in protecting the data. The data privacy in the class can be changed without modifying the whole code by using access modifiers.
Recommended Articles
This is a guide to Encapsulation in C++. Here we discuss the basic concept, how to use encapsulation in C++, and an example of encapsulation in C++ with steps. You may also look at the following articles to learn more –