Encapsulation in c++


  • Encapsulation is a process of combining data members and functions in a single unit called class.
  • This is to prevent the access to the data directly, the access to them is provided through the functions of the class.
  • It is one of the popular feature of Object Oriented Programming(OOPs) that helps in data hiding.
  • Encapsulation is a programming mechanism that binds together code and the data it manipulates, and that keeps both safe from outside interference and misuse.
  • In an object-oriented language, code and data can be bound together in such a way that a self-contained black box is created.
  • Within the box are all necessary data and code. When code and data are linked together in this fashion, an object is created. In other words, an object is the device that supports encapsulation.
  • C++’s basic unit of encapsulation is the class. A class defines the form of an object. It specifies both the data and the code that will operate on that data.
  • C++ uses a class specification to construct objects. Objects are instances of a class. Thus, a class is essentially a set of plans that specifies how to build an object.
  • When you define a class, you also specify the private members, that is, the members thatare not available for external access, and the public members of that class.
  • An application program accesses objects by using the public methods of the class and thus activating its capacities.
  • Access to object data is rarely direct, that is, object data is normally declared as private and then read or modified by methods with public declarations to ensure correct access to the data.
  • An object is thus seen to encapsulate its private structure, protecting itself from external influences and managing itself by its own methods. This describes the concept of data encapsulation concisely.