Articles‎ > ‎Technical Articles‎ > ‎

Data Hiding and Encapsulation

posted Apr 14, 2010, 1:29 AM by Thiyagaraaj M


  • Data Hiding is also known as Encapsulation.
  • Encapsulation is the process of combining data and function into a single unit called class.
  • Data Hiding is the mechanism where the details of the class are hidden from the user.
  • The user can perform only a restricted set of operations in the hidden member of the class.
  • Encapsulation is a powerful feature that leads to information hiding,abstract data type and friend function.
  • They encapsulate all the essential properties of the object that are to be created.
  • Using the method of encapsulation the programmer cannot directly access the class.

Access Specifier:

There are three types of access specifier.They are

  • Private :Within the block.
  • Public:Whole over the class.
  • Protected:Act as a public and then act as a private.

Within a class members can be declared as either public protected or private in order to explicitly enforce encapsulation.
The elements placed after the public keyword is accessible to all the user of the class.
The elements placed after the protected keyword is accessible only to the methods of the class.
The elements placed after the private keyword are accessible only to the methods of the class.
The data is hidden inside the class by declaring it as private inside the class. Thus private data cannot be directly accessed by the object.

For example,
In order to make the design and maintenance of a car reasonable the complex of equipment is divided into modules with particular interfaces hiding design decisions. 

General Form:

class class name
datatype data;
Member functions;

classname objectname1,objectname2……………;


class Square
int Num;

void Get()    {
    cout<<"Enter Number:";
void Display()    {
    cout<<"Square Is:"<<Num*Num;

void main()
Square Obj;

Features and Advantages of Data Encapsulation:

The advantage of data encapsulation comes when the implementation of the class changes but the interface remains the same.
It is used to reduce the human errors. The data and function are bundled inside the class that take total control of maintenance and thus human errors are reduced. 
Makes maintenance of application easier. 
Improves the understandability of the application. 
Enhanced Security. 

Note:Thus the concept of encapsulation shows that a non member function cannot access an object’s private or protected data.