What is Encapsulation In C++ programming

We have studied about basic design principles of object oriented programming. After explaining, Information hiding now we will move on to another design principle called Encapsulation.

Encapsulation is defined in two ways

Encapsulation


(1) The tight coupling of attributes and behavior with in an object is called encapsulation

(2) Separation of information structure and implementation details is also called     encapsulation.

Now we will first explain the First definition:

According to the first definition the coupling of data members and members function in one place is called encapsulation. An object has attributes and behavior encapsulate within it.

In procedural C++ programming data and functions are separated which cause a lots of problem in debugging and code management .Object oriented programming has provided the concept of encapsulation to use the data and functions, which operate on that data, effectively. Encapsulation thus avoids the complexity and help in code management.

Now let's take an example from real life about encapsulation, Recall the previous examples which I have given you about Information hiding. In information hiding i have told you that information is hidden from the outside world and to access them you need a member function. Now this is a classic example of encapsulation, you can see that to access the data you need a function, both data and function work together.

Let me give you another example to explain my point further. Consider yourself as an object you have all necessary gradient to be an object. You have many attributes but for sake of simplicity we only consider your one attribute which is position. You remain in certain position during your work or during your rest. Consider you have a behavior move now in order to manipulate the position you will use your function move. Behavior Move and attribute position both things are tightly coupled with in you(consider yourself an object).You  may not know that how you change position what is the force which drive you to move. This is because implementation detail about your behavior move is hidden. We will further study in detail about implementation and interface in our next lecture.


Now let's describe the encapsulation in term of second definition. According to this definition which some C++ book used, Encapsulation is the separation of information structure and implementation details.

Let’s take some example from the real world to elaborate this concept further. A Phone stores phone numbers in digital format and knows how to convert it into human-readable characters. Consider phone as a class and consider and suppose iphone is an object of class phone. Iphone object has behavior convert. We the outside world does not know at all how the mobile phone objects convert the machine code into human readable format.

This is the separation of information structure and implementation detail. We do not know that how mobile phone object implement its behavior convert.

These both definitions are used but I will refer you to use first one because it is more comprehensive and also most used. There are some points where programmer and researcher do not agree  and this is the example of it. So you do not need to worry about that.


Advantages of Encapsulation in C++


  (1) Encapsulation will bring simplicity to your program .You will not need to write separate data for every function.

(2)Encapsulation will avoid complexity as your program will be easier to understand and debug.

Note::

     If you have further queries about “concept of Encapsulation” you can email me @jawad1647@gmail.com, I will try to solve all your queries and also please don’t hesitate to report mistakes as you know "to err is a human". You can report the errors by sending me a mail at the email address given above.




Post a Comment

0 Comments