Encapsulation In C++

Encapsulation?

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 :

  100
  A

Example 2:



Output :

  Enter any two number: 
  4
  5
  Sum: 9