Acces Modifiers define where your fields/methods can be accessed from.
Fields marked as public can be accessed by anyone.
Fields marked as private can only be accessed by functions that are part of that class.
Classes are private by default, this means that:
class Point {
double x, y;
};
is the same as:
class Point {
private:
double x, y;
};
Now, let's analyze this example:
class Robot {
public:
float getX() { return locX; }
float getY() { return locY; }
float getFacing() { return facing; }
void setFacing(float f) { facing = f; }
void setLocation(float x, float y);
private:
float locX;
float locY;
float facing;
};
Here is eveidenced that you can use getters to allow read-only access to private fields.
With protected, C++ allows users to create classes based on other classes: