C++ is a flexible programming language with a number of capabilities for object-oriented programming. We will study how to implement encapsulation in C++ in this tutorial.
Encapsulation is the term used to describe the packaging of data and information into a single unit. Encapsulation is described in Object Oriented Programming as the binding of data and the functions which manipulate it.
At a company, there are various divisions, such as the accounts division, the finance division, the sales division, etc. The finance department manages all financial transactions and maintains records of all financial data. Similarly to this, the sales division manages all actions linked to sales and keeps track of all sales. Now a circumstance could come up where a finance official requires all the sales information for a specific month for some reason. He is not permitted to view the sales section's data directly in this situation. To request specific information, he must first speak with another officer in the sales department. Encapsulation is what it is. Here, the data of the sales section and the people who can alter it are bundled under the term "sales section."
So, data abstraction is a method of exposing only the interfaces and shielding the user from the implementation details. Data encapsulation is a method of grouping the data and the functions that are using them.
Encapsulation and data hiding are supported by C++ by using classes, which are user-defined types. A class can have private, protected, as well as public members, as we have already studied. Every item defined in a class is private by default.
For example
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
private:
double length; // Length of the box
double breadth; // Breadth of the box
double height; // Height of the box
};
Explanation of the above code
Height, breadth, and length are private variables. This implies they can only be accessible by other Box class members and not by any other portion of your program. Encapsulation can be accomplished in this manner.
You must specify class components after the public keyword if you want them to be available to other parts of your program. Every other function in your application can access any variables or functions defined after the public specifier.
Making one class a buddy of another reveals implementation details and decreases encapsulation. The ideal situation is to keep as many of each class's details masked from all other classes as much as possible.
Data abstraction or hiding is also a result of encapsulation. Encapsulation will hide the data as well. In example 1, any other area cannot access any of the data from any of the sections, such as sales, finance, or accounts. Encapsulation in C++ can be implemented with the help of class and access modifiers.
Look at the program below:
#include<iostream>
using namespace std;
class Encapsulation
{
private:
// data is hidden mainly from the outside world
int x;
public:
// function to set the value of
// variable x
void set(int a)
{
x =a;
}
// function to return the value of
// variable x
int get()
{
return x;
}
};
// main function
int main()
{
Encapsulation obj;
obj.set(9);
cout<<obj.get();
return 0;
}
Output:
9
The variable x is set to private in the program above. This variable can only be accessed and modified using the class's get() as well as set() functions. Thus, the variable x and also the functions get() and set() are tied together in this case, which is nothing more than encapsulation.
There are mainly three different types of encapsulations:
Member Variable Encapsulation: All of the data members are designated as private in this sort of encapsulation.
Function Encapsulation: Some member functions are declared private under this form of encapsulation. Here the constructor will be public.
Class Encapsulation: All members of a class are declared private in this sort of encapsulation. This primarily takes place in nested classes.
As the titles indicate, all of the data variables or data members are made private in member variable encapsulation. In contrast, some of the functions in Function Encapsulation are declared private. Class Encapsulation declares the entire class as private.
Your ability to define data members as private will improve as a result of learning about encapsulation. We can protect our data from outsiders in this way. Although member functions can also use this technique, member variables are where it is most commonly used. Encapsulation can thus be used frequently in this manner.
Data Abstraction | Encapsulation |
---|---|
By putting forth a design, data abstraction resolves the issues. |
The proposed design is put into practice using encapsulation, which also fixes the issues. |
Data abstraction keeps code details hidden and only makes the user-required information visible |
A class is created by merging all of the data members, including variables and functions, through the use of encapsulation. |
The use of classes as well as objects in Abstraction improves the security of the code. |
Access specifiers are used in encapsulation to limit access to data from outside the world. |
Abstraction is used to hide the implementation. |
The data can be kept secret by using encapsulation. |
In this tutorial, we discussed encapsulation and its benefits using a practical example. We then discussed some actual encapsulation implementation examples.
With the aid of those examples, we were able to comprehend the idea of data hiding and how it differs from encapsulation. We also discussed the benefits of encapsulation and why it would be a good idea to include it in our codes.