C++ Tutorial Home Page

C++ OOPs Concepts

Introduction
Class
Objects
Abstraction
Encapsulation
Polymorphism
Inheritance
Dynamic Binding
Message Passing

C++ OOPs Concepts 6

Encapsulation

Encapsulation refers to wrapping up data and associated functions into one single unit. Now let us see how C++ supports these features.

 

The class of C++ binds the data and associated functions together into as single-type (unit) which is encapsulation.

The class groups its members (data and functions) into three sections : private , protected and public. Where private and protected members remain hidden from outside world and thereby support data hiding.

Data encapsulation and its advantages

The wrapping up of the data and function into single unit is known as encapsulation.

Data encapsulation is the most important feature of class; as data is not accessible to the outside world/class and only those functions which are wrapped in the class can access it.

This insulation of data from direct access by the program from outside the class is called data hiding or information hiding.

Consider a real-life example of encapsulation,

In a company, there are different sections like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keeps records of all the data related to finance.

Similarly, the sales section handles all the sales-related activities and keeps records of all the sales.

Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month.

In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name “sales section”.