Data Encapsulation in C++

    Encapsulation is one of the properties of Object-Oriented Programming, and as its name suggests, it deals with wrapping up data and information within a single entity. In C++ OOP’s concept, we have a class data structure which is a perfect example for the implementation of the Encapsulation concept. Encapsulation is defined as when we grouped all the data members and member functions in a single unit. In other words, wrapping the data and information together is known as encapsulation. The concept of encapsulation leads to data hiding or Data Abstraction. Data Encapsulation and Data Abstraction are resemblances to each other but there is a difference, Encapsulation deal with wrapping the data whereas Abstraction deal with Hiding the data.

    Encapsulation Real-world example

    If there are two teams A and B in a company which work on different projects and for some reason, A wants some data of B, but A does not have direct access to the B’s data. So, first, the A needs to send the request to B’s members for the data access, and only if B gives the permission then only A will be able to access Bs data.

    Access Specifiers in C++

    Access specifiers are used inside the class and they decide which data member and member function can be called outside the class. There are three access Specifiers.

    • private
    • public
    • protected

    Private Access Specifiers

    By default, if we do not mention any access specifier inside a class then all the members of the class become private which means no one would be able to access the class members outside the class. And during inheritance no matter which visibility mode we use we can not inherit the private members of the class. Example:

    class Access_Specifiers
    {
     private:
         int a;
    };

    Public Access Specifier

    If the data members and member functions of the class are public then they can be accessed by everyone. Example:

    class Access_Specifiers
    {
       public:
         int a;
    };

    Protected Access Specifier

    The protected Access specifier act as private access specifier if we want to access the class members from outside the class, but it acts as public access specifier when the class is inherited by another class. Example

    class Access_Specifiers
    {
    private:
      int a;
    };

    Encapsulation Example

    #include <iostream>
    using namespace std;
    
    class Encaptulation_Example
    {
        private:
            int x;  // using private access modifer
    
        public:  
            void put(int a)
            {
                x = a;
            }
          
            void show()
            {
               cout<<"The value of x is: "<<x;
            }
    };
    
    int main() 
    {
        Encaptulation_Example e_c;
        e_c.put(10);
        e_c.show();
        return 0;
    }

    Output

    The value of x is: 10

    Behind the code: In this above example, we have created a class Encaptulation_Example which has a private data member x and we cannot directly access the value of x because it is private. But the data members of Encaptulation_Example put() and show() are public and using these member functions we can manipulate and display the value of x. That’s how encapsulation work, here we can say that data member x is wrapped up with member functions put() and show().

    Encapsulation Quick Summary:

    • Encapsulation deals with wrapping data and information in a single unit.
    • Encapsulation deal with Data Abstraction
    • To achieve encapsulation, we use class and access specifiers.
    • There are three access specifiers in C++ private, public and protected.

    People are also reading: