Data Hiding and Encapsulation using Access Specifiers in C++

Encapsulation Definition:

Encapsulation is a process of capsulation of data and methods into a combined single unit. In C++, encapsulation is used along with the classes concept.

Bundling of data and methods (functions)  as a single unit is known as encapsulation.

Encapsulation represents the information of variables (attributes) in terms of data and, methods (functions) and its operations in terms of purpose.

  • Encapsulation is the process of combining data and function into a single unit called class.
  • Encapsulation is a powerful feature that leads to information hiding and abstract data type.
  • They encapsulate all the essential properties of the object that are to be created.
  • Using the method of encapsulation the programmer cannot access the class directly.

encapsulation and data hiding

Data Hiding Definition:

Data hiding is a technique especially practised in object-oriented programming (OOP).Data hiding is hiding the details of internal data members of an object.

  • Data hiding is also known as Information hiding.
  • Sometimes Data Hiding includes Encapsulation. Thus Data Hiding is heavily related to Abstraction and Encapsulation.
  • Data Hiding is the one most important OOP mechanism. Which is hide the details of the class from outside of the class.
  • The Class used by only a limited set of variables and functions, others are hidden by the class.

Access Specifiers in C++

Access specifiers define how a member's variables and member's functions of a class can be accessed from outside the class. However, all members of a class can be accessed from within the class without any restriction. 
Class members can be declared as public, protected or private access specifiers, which is used to build the encapsulation capabilities of the class.
There are three access specifiers.

Access Specifiers In C++

  • Private members: These can be accessed only from within the members of the same class.
  • Protected members: These can be accessed only from within other members of the same class and its derived classes.
  • Public members: These can be accessed from anywhere where the object is accessible.

By declaring the member variables and functions as a private in a class, the members are hidden from outside the class.Those private members and data cannot be accessed by the object directly.

Encapsulation General Form:

class class_name {
    datatype member_variables;
    datatype member_functions;
    datatype member_variables;
    datatype member_functions;

main() {
    class_name objectname1, objectname;

Encapsulation Example:

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

void main() {
    Square Obj;

In the above example, the variable “Num” is private. Hence this variable can be accessed only by the members of the same class and is not accessible anywhere else. Hence outside the classes will be unable to access this variable Which is called data hiding.

At the same time, “Square” class contains two other methods namely “Get” and “Display” which has public members. Here “Get” method just prints the value while “Display” method prints the square of the value in the variable “Num”. Here the class “Square” implements Data Encapsulation concept by capsulation of the value in the variable “Num” and thus allowing the user only to perform a restricted set of operations on the hidden variable “Num”.

Features and Advantages of Data Encapsulation:

  • Encapsulation is used to reduce the complexity and improve reusability.
  • Encapsulation defines the interface clearly which improves the readability and understandability.
  • Encapsulation helps to hide important data and ensures enhanced Security.
  • The member variables and members are bundled into a single unit as a class which makes the maintenance easy.
  • The encapsulated classes are straightforward and are easy to manage and improves the future development of the application.