Avatar ·

When to use class and when to use struct in C++? Rules and best practices

Hello everyone! I recently started learning C++ and got confused about choosing between class and struct. They seem almost identical in C++, but I see that experienced programmers use them differently. For example, I wrote this code for a point on a plane:\n\n```cpp\nstruct Point {\n int x;\n int y;\n};\n\nclass Circle {\nprivate:\n int radius;\npublic:\n Circle(int r) : radius(r) {}\n int getRadius() { return radius; }\n};\n```\n\nBut then I saw someone doing this:\n\n```cpp\nclass Point2 {\npublic:\n int x;\n int y;\n};\n```\n\nAnd I got confused — why use class if everything is public anyway? And conversely, why not make a struct with private fields and methods?\n\nI heard there is some "rule of thumb", but I can't find a clear explanation.