Why are data members private by default in C++?

后端 未结 5 1207
挽巷
挽巷 2021-02-06 01:23

Is there any particular reason that all data members in a class are private by default in C++?

5条回答
  •  遇见更好的自我
    2021-02-06 02:15

    Because it's better to be properly encapsulated and only open up the things that are needed, as opposed to having everything open by default and having to close it.

    Encapsulation (information hiding) is a good thing and, like security (for example, the locking down of network services), the default should be towards good rather than bad.

提交回复
热议问题