C++ Encapsulation

Binding of data and its function in a single unit. It performs the operation of data hiding. Some data are hidden behind the access methods and some are open to all. Access on data by defining the access modifier.

Access modifier mainly used are public, private and protected. By default, all the function declared in a class are private. Function declared as private are not visible outside the class. Function declared as protected are visible to its derived class also, but not outside the class. Function declared as public are visible from any part of your program.

Advantage of encapsulation

  • Encapsulation reduces the complexity of the program
  • Encapsulation secures the data from other function. It hides the data from the user that makes a protection layer that a user cannot access.

Example of Encapsulation