Learn Data Encapsulation in C++ with Example in just 3 min

Data encapsulation in C++ is nothing but a way to implement data abstraction. Although this concept is closely related to the data abstraction, it still has its own significance. Let us start exploring this topic from the beginning to the end.

Here we are going to deal with various of aspects of data encapsulation.

Data Encapsulation in C++

Keeping you updated with latest technology trends, Join DataFlair on Telegram

What is Data Encapsulation in C++?

Data encapsulation is a striking concept of object-oriented programming that enables the programmer to combine both data and functions that operate on that particular data under a single unit. In other words, C++ data encapsulation is a way of implementing the concept of data abstraction.

As discussed earlier, we can implement the concept of data encapsulation through classes and objects. In order to access data members and member functions in a class, we need to create objects in association with it, to create a block in the computer memory to store it.

We can access these data members and member functions with objects by reading and printing them onto the screen. We can even perform data manipulation operations with the help of objects. Unless we create an object, C++ does not grant access to data members and member functions. Therefore, the data is not visible to the outside world and it is absolutely safe from inadvertent alteration.

Example of Data Encapsulation

Let us consider a real-life problem in order to better understand the meaning of data encapsulation.

Suppose you are a student and you want to access the answer key to the test you had given last week from your teacher. You do not have direct authority to go into the teacher’s cabin and take away the answer key. You need to take permission from your teacher and then your teacher would go to her cabin herself and hand over the answer key to you.

This practice ensures that data is in safe hands and cannot be corrupted by unsuitable users.

It is safe is say that “The answer key (data) and the teacher (an object that helps you access the answer key) are encapsulated into a single unit. In a similar fashion, objects help you maintain the integrity of the program data.

Let us try to implement the concept of data encapsulation in C++ with the help of a simple program.

Consider a scenario where you want to secure the transaction ID from inadvertent users in the finance department. You can solve this problem by using the concept of data encapsulation.

It’s time to revise the concept of Class and Objects in C++

Example-

The C++ program given below shows how data is hidden from the outside world with reference to the above-stated scenario:

#include<iostream> 
using namespace std; 

class Encapsulation 
{ 
// Private by default // Data inaccessible to the outside world
long int ID; 

public:

void set_value(int value) 
{ 
ID = value;
} 
int get_value() 
{ 
return ID; 
} 
}; 

// main function 
int main() 
{ 

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

Encapsulation e; 

e.set_value(209718); 
cout<<"The ID is: "<<e.get_value()<<endl;
return 0; 
}

Code-

C++ Data Encapsulation with Example

Output-

Output of Example of Data Encapsulation in C++

Advantages

The major benefit of data encapsulation is the security of the data. It protects the data from unauthorized users that we inferred from the above stated real-real problem.

We can apply the concept of data encapsulation in the marketing and finance sector where there is a high demand for security and restricted access of data to various departments.

Do you know the unique features of C++?

Difference Between Data Abstraction and Encapsulation in C++

Data Abstraction and Encapsulation are complementary concepts and are not to be confused with. The primary focus of abstraction is upon the observable behavior of an object, whereas the primary focus of encapsulation is the implementation that gives birth to that particular behavior. Let’s discuss the difference between Data Abstraction and Data Encapsulation in C++

Here is a table that illustrates the difference between the two:

Data  Abstraction
Data  Encapsulation
It solves the issue by proposing a design.
It solves the issues by implementing the design proposed by data abstraction.
Helps in hiding the background details and explanation of how the data is derived.
Helps in hiding the data by combining it with functions and objects as a single unity to protect it from unauthorized users.
Shifts your focus on what the object does instead of what it does.
Encapsulation helps you hide the internal detail from the outside world by giving it restricted access.
They are abstract in nature.
They are known as ADT (abstract data types) as they are used to create objects of its own type.

Summary

We have finally completed this article on data encapsulation in C++. Initially, you might have encountered some difficulty going through this topic. But, after discussing data encapsulation through a real-life example, you would have found it easy to grasp.

After all, it has been proven by research scholars that any difficult concept can be simplified using real-life examples that help you relate to it. At the end of this tutorial, we laid emphasis on understanding the difference between Data abstraction and encapsulation.

If you have any queries regarding this topic, do let us know in the comment section below.

You can execute the code on Linux by Installing C++ using g++

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.