CPP Course


Go back

The visibility is defining who can access, what with call members (attributes + methods) of our structure. We got 3 modifiers of the visibility

  • public (default for structures): everyone
  • protected: inheritors, the structure itself
  • private (default for classes): only the structure itself

It means that public > protected > private, if we are sorting them by their exposition of the internal structure to the outside.

A good practice 📚? Set everything private, especially attributes, and make some methods public.


You will use the syntax modifier:, such as public: (default). Every member declared after the : will be public/... according to the modifier you used.

struct my_structure {
    int attribute;
    void my_method() {};

You can add many times the same modifier if you want to sort things.

struct my_structure {
public: // public members
    int attribute;
    void my_method() {};
private: // bunch of private members I
    void my_method1() {};
private: // bunch of private members II
    void my_method2() {};


my_structure object;
// public: ok
object.attribute = 5;
// private: KO = DO NOT WORK