I have Googled and referred books but I did not get convincing difference between these two . What I have understood they both hide data from outer world but how exactly are they different ? And how are they implemented in c++ ?
The way I see it is that encapsulation is when you put data inside a class and let the user of the class manipulate the object state by using member functions. The data is made private to protect it from being manipulated directly from outside the class.
I think data abstraction is more about that the user of the class doesn't need to know exactly what kind of data the object state consist of. Like for instance if you have a Circle class
1 2 3 4 5 6 7 8 9
class Circle
{
public:
int getRadius();
int getDiameter();
int getArea();
private:
/* data members */
};
It is not necessary to store the radius, diameter and the area as three different variable. It's enough to store one of them and then calculate the others inside the functions. As a user of the class you don't need to care because you can simply use the interface (the public functions) and ignore the implementation details.
Data abstraction and encapsulation are two different concepts.
Think of data abstraction as the act of representing some object as a class. The object being represented can be some abstract concept (e.g. a shape). That class consists of a collection of public functions (including constructors) that act on that object in specific ways. These functions are the public "behaviors" of the object and are a contract with the user of the class of how the object will behave. Data abstraction by itself does not ensure encapsulation.
Encapsulation is the hiding of the data by making it private. Simply hiding data does not mean that a class is a good abstraction of the object it represents.
Well designed classes exhibit both encapsulation and abstraction.