Encapsulation In C++

Share Your Love

Encapsulation is the process of encapsulating data and information into a single entity. Encapsulation is described in Object-Oriented Programming as the binding of data and the functions that manipulate it.

Consider a real-world example of encapsulation: in a firm, there are several sectors such as accounts, finance, and sales, among others. The finance department is in charge of all financial transactions and keeps track of all financial data. Similarly, the sales department is in charge of all sales-related activities and keeps track of all sales. Now and then, a circumstance may arise in which a finance official requires complete sales data for a specific month for some reason.

In this instance, he is not permitted to view the sales section’s data directly. He must first contact another officer in the sales department and request that he provide the requested information. Encapsulation is what it is. The data from the sales area, as well as the employees who can change it, are grouped together under the heading “sales section.”

Encapsulation In C++

Data abstraction or hiding is also a result of encapsulation. Because encapsulation hides data, it’s a good idea to use it. Any data from one of the sections, such as sales, finance, or accounting, is hidden from any other area in the above example.

Encapsulation in C++ can be achieved by the use of Class and access modifiers. Take a look at the following program:

// c++ program to explain
// Encapsulation

using namespace std;

class Encapsulation
		// data hidden from outside world
		int x;
		// function to set value of
		// variable x
		void set(int a)
			x =a;
		// function to return value of
		// variable x
		int get()
			return x;

// main function
int main()
	Encapsulation obj;
	return 0;



The variable x is declared private in the above program. Only the functions get() and set(), which are included in the class, can be used to access and alter this variable. As a result, we may conclude that the variable x, as well as the functions, get() and set(), are bound together, resulting in encapsulation.

Role of access specifiers in encapsulation:

As we saw in the last example, access specifiers are crucial in the implementation of encapsulation in C++. The implementation of encapsulation can be broken down into two steps:

Using the private access specifiers, the data members should be identified as private.

The public access specifier should be used to mark the member function that manipulates the data members as public.

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter