C++ Encapsulation

C++ Encapsulation

C++ Encapsulation

Encapsulation is the process of combining data and functions into a single unit called class. Data is only accessible through the functions present inside the class.

Why Encapsulation

  • Hiding the implementation details and providing restrictive access leads to the concept abstract data type.
  • Data encapsulation is important led to the concept the data hiding.
  • It gives secure and consistent results. 
  • This means that it gives the user access to limited data and keeps our valuable data which can change our program or increase the possibilities of mistakes hidden from the user. 
  • We can understand this more by this example: Assumes we made a rectangle class containing four variables- length, width, area, and perimeter.
  • As we know that is private (cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.


Example – Program to Accessing a private attribute used to public get and set methods: