C++ Encapsulation in OOP | Advantage and Disadvantages of Encapsulation | Data Hiding vs Encapsulation

C++ Encapsulation in OOP | Encapsulation in C++ with Examples | Advantage and Disadvantages of Encapsulation | Data Hiding vs Encapsulation | OOP Tutorial in C++

In this tutorial, we are going to learn about the C++  Inheritance in OOP ,  Inheritance in C++, Types of Inheritance in OOP, Advantage and Disadvantages of using Inheritance, OOP Tutorial in C++

What is Encapsulation in C++?

Encapsulation means the process of wrapping up the data and functions in a single capsule.

Encapsulation also lead to data abstraction or hiding. As using encapsulation also hides the data.

The common example of encapsulation is Capsule. In capsule all medicine are encapsulated in side capsule.

How Encapsulation is achieved in a class:

To do this:

1) Make all the data members private.
2) Create public setter and getter functions for each data member in such a way that the set function set the value of data member and get function get the value of data member.

Why Encapsulation?

  1. Increased security of data.
  2. Encapsulation ensures better control of your data.
  3. Member variables are made private so that these cannot be directly accessed from outside the class .
  4. Encapsulation also helps us to make a flexible code which is easy to change and maintain .
  5. Encapsulation is also useful in hiding the data .
  6. Encapsulation helps us in binding the data .

Benefits of encapsulation:

There are various benefits of encapsulated classes:

  1. Provides abstraction between an object and its clients.
  2. Encapsulated classes reduce complexity.
  3. Help protect our data. A client cannot change an Account's balance if we encapsulate it.
  4. Encapsulated classes are easier to change. 
  5. Protects an object from unwanted access by clients.

Dis-Advantage of Encapsulation in C++

You can't access private date outside the class.

Example of Encapsulation in C++:

#include <iostream>
using namespace std;

class Employee {
    // Private attribute
    int salary;

    // Setter
    void setSalary(int s) {
      salary = s;
    // Getter
    int getSalary() {
      return salary;

int main() {
  Employee myObj;
  cout << myObj.getSalary();
  return 0;



The salary attribute is private, which have restricted access.

The public setSalary() method takes a parameter (s) and assigns it to the salary attribute (salary = s).

The public getSalary() method returns the value of the private salary attribute.

Inside main(), we create an object of the Employee class. Now we can use the setSalary() method to set the value of the private attribute to 50000. Then we call the getSalary() method on the object to return the value.

Difference Between Data Hiding and Encapsulation:

Data Hiding and Encapsulation are two concepts of OOP. 

Data hiding is the process of protecting the members of the class from unauthorized access while encapsulation is the process of wrapping the data members and methods into a single unit. This is the key difference between data hiding and encapsulation. 

Data hiding focus on securing the data while hiding the complexity of the system. Encapsulation mainly focuses on hiding the complexity of the system. Encapsulation is a way of achieving data hiding.

Next Post:

C++ Polymorphism in OOP | Types of Polymorphism in C++ with Examples |  Method Overloading vs Overriding in OOP

Previous Post: 

Inheritance in C++ ~ Types of Inheritance in C++ with Examples ~ OOP in C++

0 Response to C++ Encapsulation in OOP | Advantage and Disadvantages of Encapsulation | Data Hiding vs Encapsulation

Post a Comment