Tutorial Study Image

Encapsulation in C++

June 9, 2023, Learn eTutorial

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.

What do you mean by encapsulation in C++?

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.

Consider a practical illustration of encapsulation: Example 1

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."

  • No function from the class can be accessed directly. In order to use the function, which uses a member variable from that class, we must access it through an object.
  • Encapsulation is only achieved when the function created within the class uses all member variables.
  • We do not call it encapsulation if we do not create a function within the class that uses the class's member variable.

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 {
      double getVolume(void) {
         return length * breadth * height;

      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:

Write a C++ program in order to understand the concept of encapsulation in C++

using namespace std;

class Encapsulation
  // data is hidden mainly from the outside world
  int x;
  // 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;
 return 0;




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.

Types Of 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.

Advantages of encapsulation in C++

  1. Encapsulation in C++ makes the code look cleaner and is simpler to follow by combining similar members into a single class.
  2. The user can only read and write using the get() and set() operations, respectively. As a result, the data must be kept secure.
  3. Data bundling makes development more manageable overall. These bundles can be tested, debugged, and executed independently of one another without affecting the other components.
  4. Hiding data from the public is an excellent strategy to keep it safe. As a result, the code's security has been enhanced even further.
  5. Because we may change one portion of the code without changing the other, encapsulation gives us effective control over the data.

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.

Differences between encapsulation and data abstraction

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.