Let we understand about encapsulation !
Encapsulation is binding of data and function in a single unit or block. Data inside the block is called member data and function inside the block is called member function.
Just like Capsules which bind medicine inside the capsule.We see the capsule but we can not see which medicine inside it.
There are lot of confusion with Data Abstraction & Data Encapsulation in both the cases one thing is common that is hiding of data.
I would like to clear first we need to identify entities like capsule , properties like medicine and behaviours like their effect (effect from humidity) this is abstraction in other words abstraction is simply a thought process to identify entity , properties and behaviours which is followed by encapsulation.
Now we can say that encapsulation is the process of providing physical structure to implement the outcome ( entity , property , behaviour) of abstraction that protect the property from accidental or unauthorised access.
In other words abstraction tells what to do? and encapsulation tells how to do?
Example of Encapsulation:
#include<iostream.h>
#include<conio.h>
class add
{
private: int a,b,c;
public:
void sum()
{
cout<<"Enter any two numbers: ";
cin>>a>>b;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
clrscr();
sum s1;
s1.add();
getch();
}
It is a simple explanation and implementation of encapsulation. It is simple concept.
No comments:
Post a Comment