Site icon DataFlair

Learn Data Encapsulation in C++ with Example in just 3 min

Data Encapsulation in C++

Free C++ course with real-time projects Start Now!!

Data encapsulation in C++ is nothing but a way to implement data abstraction. Although this concept is closely related to data abstraction, it still has its own significance. Let us start exploring this topic from the beginning to the end.

Here we are going to deal with variety of aspects of data encapsulation.

What is Data Encapsulation in C++?

Data encapsulation is a striking concept of object-oriented programming that enables the programmer to combine both data and functions that operate on that particular data under a single unit. In other words, C++ data encapsulation is a way of implementing the concept of data abstraction.

As discussed earlier, we can implement the concept of data encapsulation through classes and objects. In order to access data members and member functions in a class, we need to create objects in association with it, to create a block in the computer memory to store it.

We can access these data members and member functions with objects by reading and printing them onto the screen. We can even perform data manipulation operations with the help of objects. Unless we create an object, C++ does not grant access to data members and member functions. Therefore, the data is not visible to the outside world and it is absolutely safe from inadvertent alteration.

Example of Data Encapsulation

Let us consider a real-life problem in order to better understand the meaning of data encapsulation.

Suppose you are a student and you want to access the answer key to the test you had given last week from your teacher. You do not have direct authority to go into the teacher’s cabin and take away the answer key. You need to take permission from your teacher and then your teacher would go to her cabin herself and hand over the answer key to you.

This practice ensures that data is in safe hands and cannot be corrupted by unsuitable users.

It is safe is say that “The answer key (data) and the teacher (an object that helps you access the answer key) are encapsulated into a single unit. In a similar fashion, objects help you maintain the integrity of the program data.

Let us try to implement the concept of data encapsulation in C++ with the help of a simple program.

Consider a scenario where you want to secure the transaction ID from inadvertent users in the finance department. You can solve this problem by using the concept of data encapsulation.

Example-

The C++ program given below shows how data is hidden from the outside world with reference to the above-stated scenario:

#include<iostream> 
using namespace std; 

class Encapsulation 
{ 
// Private by default // Data inaccessible to the outside world
long int ID; 

public:

void set_value(int value) 
{ 
ID = value;
} 
int get_value() 
{ 
return ID; 
} 
}; 

// main function 
int main() 
{ 

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

Encapsulation e; 

e.set_value(209718); 
cout<<"The ID is: "<<e.get_value()<<endl;
return 0; 
}

 

Output-

Advantages of Data Encapsulation in C++

The major benefit of data encapsulation is the security of the data. It protects the data from unauthorized users that we inferred from the above stated real-real problem.

We can apply the concept of data encapsulation in the marketing and finance sector where there is a high demand for security and restricted access of data to various departments.

Difference Between Data Abstraction and Encapsulation in C++

Data Abstraction and Encapsulation are complementary concepts and are not to be confused with. The primary focus of abstraction is upon the observable behavior of an object, whereas the primary focus of encapsulation is the implementation that gives birth to that particular behavior. Let’s discuss the difference between Data Abstraction and Data Encapsulation in C++

Here is a table that illustrates the difference between the two:

Data  Abstraction
Data  Encapsulation
It solves the issue by proposing a design.
It solves the issues by implementing the design proposed by data abstraction.
Helps in hiding the background details and explanation of how the data is derived.
Helps in hiding the data by combining it with functions and objects as a single unity to protect it from unauthorized users.
Shifts your focus on what the object does instead of what it does.
Encapsulation helps you hide the internal detail from the outside world by giving it restricted access.
They are abstract in nature.
They are known as ADT (abstract data types) as they are used to create objects of its own type.

Quiz on Data Encapsulation in C++

Time limit: 0

Quiz Summary

0 of 14 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 14 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 14
    1. Question

    What is the output of following program?

    #include<iostream>

    using namespace std;

      

    class Enc

    {

        private:

            int x;

              

        public:

            void set(int a)

            {

                x =a;

            }

            int print()

            {

                cout<<x;

            }

    };

     

    int main()

    {

        Enc obj;

        obj.set(5);

        obj.print();

        return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 14
    2. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class Add {

       public:

          Add(int i=0) {

             num = i;

          }

     

          void addNum(int number) {

             num += number;

          }

     

          int print() {

             cout<<num;

          };

       

       private:

          int num;

    };

     

    int main() {

       Add a;

       

       a.addNum(10);

       a.addNum(30);

     

       a.print();

       return 0;

    }

    Correct
    Incorrect
  3. Question 3 of 14
    3. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class area {

      public:

        int length;

        int breadth;

     

        area(int l, int b){

            length=l;

            breadth=b;

        }

     

        int Area() {

          return length * breadth;

        }

    };

     

    int main() {

      area A1(2, 3);

      cout << A1.Area();

     

      return 0;

    }

    Correct
    Incorrect
  4. Question 4 of 14
    4. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class area {

      public:

        int side;

     

        area(int s): side(s){}

     

        int Area() {

          return side*side;

        }

    };

     

    int main() {

      area A1(2);

      cout << A1.Area();

     

      return 0;

    }

    Correct
    Incorrect
  5. Question 5 of 14
    5. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class sort {

      public:

        int a[3];

     

        sort(){

            for(int i=0;i<3;i++)

            {

                a[i]=i+1;

            }

        }

        int print()

        {

            for(int i=0; i<3; i++)

            {

                cout<< a[i];

            }

        }

    };

     

    int main() {

      sort s1;

      s1.print();

      return 0;

    }

     

    Correct
    Incorrect
  6. Question 6 of 14
    6. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class sort {

      public:

        int a[3];

     

        sort(){

            for(int i=0;i<3;i++)

            {

                a[i]=i+1;

            }

        }

     

        int Shuffle() {

            for(int i=0; i<3; i++)

            {

                if(a[i]<a[i+1])

                swap(a[i],a[i+1]);

                else

                i++;

            }

        }

        int print()

        {

            for(int i=0; i<3; i++)

            {

                cout<< a[i];

            }

        }

    };

     

    int main() {

      sort s1;

      s1.Shuffle();

      s1.print();

      return 0;

    }

    Correct
    Incorrect
  7. Question 7 of 14
    7. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class A {

      public:

        int i=10,z;

        int j =20;

        A()

        {

            z=i+j;

        }

        int print()

        {

            cout<<z;

        }

    };

     

    int main() {

      A obj;

      obj.print();

      return 0;

    }

    Correct
    Incorrect
  8. Question 8 of 14
    8. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class A {

      public:

        int i=10,z;

        int j =20;

        A()

        {

            z=i+j;

        }

        int print()

        {

            cout<<z;

        }

        int show_i()

        {

            cout<<i;

        }

    };

     

    int main() {

      A obj;

      obj.show_i();

      return 0;

    }

    Correct
    Incorrect
  9. Question 9 of 14
    9. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class A {

      public:

        int i=10,z;

        int j =20;

        A()

        {

            z=i+j;

        }

        int show_j()

        {

            cout<<z;

        }

    };

     

    int main() {

      A obj;

      obj.show_j();

      return 0;

    }

    Correct
    Incorrect
  10. Question 10 of 14
    10. Question

    What is the output of following program?

    #include <iostream>

    using namespace std;

     

    class A {

      public:

        A()

        {

            int i=1, j=2, z;

            z=i+j;

            cout<<“hidden data “<< z;

        }

    };

     

    int main() {

      A obj;

      return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 14
    11. Question

    Mechanism of bundling data and the functions that uses them 

    Correct
    Incorrect
  12. Question 12 of 14
    12. Question

    What all access specifiers can be used to achieve encapsulation

    Correct
    Incorrect
  13. Question 13 of 14
    13. Question

    What is not true with respect to Encapsulation?

    Correct
    Incorrect
  14. Question 14 of 14
    14. Question

    Which among the following is an Advantage of Encapsulation

    Correct
    Incorrect

Summary

We have finally completed this article on data encapsulation in C++. Initially, you might have encountered some difficulty going through this topic. But, after discussing data encapsulation through a real-life example, you would have found it easy to grasp.

After all, it has been proven by research scholars that any difficult concept can be simplified using real-life examples that help you relate to it. At the end of this tutorial, we laid emphasis on understanding the difference between Data abstraction and encapsulation.

If you have any queries regarding this topic, do let us know in the comment section below.

Exit mobile version