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. Basically 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 in, the Head of the Physics department need some information from the Biology department, he can’t access the data from that department directly. 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++?
In order to achieve this we have to follow the below steps:
- First, we need to make all the data members private.
- Then public getter and setter functions should be created for each data member in such a way that, get function gets the value of data member and the set function sets the value of data member.
Encapsulation and data hiding can be achieved in C++, by using user-defined types called Classes. The access specifiers in classes can be private, protected or public. By default all the items in a class are private. 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 of 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 have to make sure that, 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 used to retrieve the total marks of all the three subjects. We can’t access each subject in another class because of its protection level. But we can access the method and can be used 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 in the next example.
4.8 (3,282 ratings)
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 name space 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 that we can’t access the variables outside this Student class. But we can access them locally and modify the value if needed. In order to modify the value, we can write a public setter function and access it in another class.
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 need to 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 the 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 of 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.
13) Now we will analyze how we got this output. Since we could not access the private variables, we wrote a Setter function which takes values from other class and modifies the variables. Getter function fetches the variables, adds them and sends the total marks of the student. Each time when we pass three different values to setter function, we can see that total marks we get will also varies accordingly.
14) From this, we can say that encapsulation helps us to protect some of the needed data also setter and getter functions are used to alter their values. By doing this. Protection is also achieved without hampering our purpose.
In this article, we have learned about how encapsulation can be achieved in C++. By doing this we can achieve the following benefits.
- Classes after encapsulation increase the readability and reduce complexity.
- It helps in protecting the data.
- The privacy of the data in the class can be changed without modifying the whole code by using access modifiers.
This is a guide to the Encapsulation in C++. Here we discuss how to use encapsulation in C++ along with Example of Encapsulation in C++ with Steps. You may also look at the following articles to learn more –