Encapsulation In C++


Encapsulation is a process of wrapping of data and methods in a single unit. Combining of state and behavior in a single container is known as encapsulation.

Thus the data gets hidden from being accessed directly from outside the class.

Encapsulation is necessary to keep the details about an object hidden from the users of that object. Details of an object are stored in its data members (member bables).

Provides abstraction between an object and its clients.

It is one of the popular feature of Object Oriented Programming(OOPs) that helps in data hiding.

How Encapsulation is achieved in a class?

1) Make all the data members private.

2) Create public setter and getter functions for each data member.

Example 1:

Output :


Example 2:

Output :

  Enter any two number: 
  Sum: 9