Encapsulation is a process of combining member functions and data members in a single unit called a class. The purpose is to prevent access to the data directly. In this article, we will learn more about encapsulation, their advantages and disadvantages and how to use encapsulation in C++.
C++ is based on object-oriented programming(OOPs) concepts. One of the OOPs concepts is encapsulation. We have covered an article on OOPs concepts in C++. In this blog, we will be covering encapsulation in detail with real-life examples.
So let’s get started!!!
Table of contents
- What is encapsulation in C++
- Advantages/disadvantages of Encapsulation in C++
- Access specifiers in C++
- How to Use Encapsulation in C++?
- Example of Encapsulation
What is encapsulation in C++?
Encapsulation is a process of combining member functions and data members in a single unit called a class. The purpose is to prevent access to the data directly. Access to them is provided through the functions of the class. It is one of the popular features of Object-Oriented Programming(OOPs), which helps in data hiding.
Suppose you go to an automatic teller machine(ATM) and request money. The machine processes your request and gives you money.
Here, ATM is a class. It takes data from the user(money amount and PIN) and displays data as icons and options. It processes the request(functions). So, it contains both data and functions wrapped/integrated under a single ATM. This is called Encapsulation.
2. Suppose you have a profile on some social networking website, say Facebook. If your profile password is declared as a public variable, anyone can see your password and log in to your Facebook account. So, would you like it? Obviously No.
So, Facebook declares your password as private to make your account safe so that anyone can not log in to your account. The other details of your account or posts you share can be made public or private by you, which helps you see the data-hiding concept here.
Advantages/disadvantages of Encapsulation in C++
- The main advantage of using Encapsulation is to hide the data from other methods. By making the data private, these data are only used within the class but are not accessible outside the class.
- Protects data from unauthorized users
- This concept is applicable in the marketing and finance sector, where there is a high demand for security and restricted data access to various departments.
- Encapsulation helps us in binding the member functions and data of a class.
- Encapsulation also helps us make code flexible, which is easy to change and maintain.
- Private data cannot be accessed outside the class.
Access specifiers in C++
We can make our data public, private, and protected in class. This can be done with the help of access specifiers like public, private, and protected. We can change the access specifier according to our needs and data. Using these access specifiers, the programmer gets control over data visibility. That means a programmer can decide what functions or data should be hidden and what to show the user.
NOTE: By default(if not declared), all the items in a class are private.
- Public: The data Members/member functions declared as public can be accessed by the same class and other classes.
- Private: The data Members/member functions declared as private can be accessed by the same class only. Data cannot be accessed outside the class. If accessed, it will give an error.
- Protected: The data Members/member functions declared as private can be accessed by the same class and the derived class.
|Any other class
How to Use Encapsulation in C++?
- First, make all the data members private.
- Then getter(gets the value of data member) and setter (sets the value of data member)functions should be performed for each data member.
Example of Encapsulation
In this, we are adding an employee’s salary (full-time and overtime). We have a data member salary declared as private, as you can see. That means it can be used within that class only and not even in the main(). If we want to use this function in the main function, we should use the getter and setter functions.So here we have a getter function getSalary() and setter function setsalary().
Inside main(), we create an object of the company class. Now we can use the setSalary() method to set the value of the full-time and overtime salaries. Then we call the getSalary() method on the object to return the value.
Encapsulation is explained with a real-life analogy and a simple programming example. I hope you got an idea. If you liked the blog, then do share it with your friends.
Keep sharing information!!!