It is to protect the implementation.
The public functions are your interface to the object. You can change the internal (private) representation without affecting anything that uses the code because the external (public) representation does not change.
// First attempt at your code.
class Point
{
public: Point(int x,int y): m_x(x),m_y(y) {}
int getX() {return x;}
int getY() {return y;}
int getAngle() { return someAngleMaths(x,y);}
int getDist() { return someDistMaths(x,y);}
private:
int m_x;
int m_y;
};
This seems like a reasonable implementation for a point and you write a lot of code that uses it.
Now you go along for a while with no problems. You then realize that your code is not fast enough. You trace the problem back to the point class as a lot of work goes into getting the polar coordinates all the time.
Since you protected the internal representation from an external user you can change it to make the class more efficient without affecting any of the users.
class Point
{
public: Point(int x,int y): m_a(someAngleMaths(x,y)),m_d(someDistMaths(x,y)) {}
int getX() {return convertPolarToX(m_a,m_d);}
int getY() {return convertPolarToY(m_a,m_d);}
int getAngle() { return m_a;}
int getDist() { return m_d;}
private:
int m_a;
int m_d;
};
Daaa. Daa.
More efficient code. Nobody will even know you changed anything.