Encapsulation

It is one of the most important features of oops, that is used to wrapping the data and function into a single unit. The data of class is not accessable to outside the class only those function access data which are wrappedin the class. It is used to prevent the direct accessibility of data member and member function and this is done by using access specifier public, private and protected.

#include< iostream.h >
class circle
{
private:
float area;// data member
float radius;// data member
public: //access specifier
void getinput() // default constructor
{
cout<<"Enter radius"<<"\n";
cin>>radius;
}
void findarea()
{
area=3.14*radius*radius;
cout<< "Area of circle ="<< area;
}
};
void main()
{
circle cir;
cir.getinput(); // calling function
cir.findarea(); // calling function
}

Output:

Enter radius
2.2
Area of circle = 15.197

For example : - Here we see a real life example. suppose there are two company, and they are building a software, The first team needs the data of the second team, but the first team cannot access that data directly because they do not have the ability to access the data. For this, the first team can access the data with permission by talking to the leader of the second team. This is called Encapsulation.