C++20 sections · 1024 units
Open in Course

Class vs Struct Differences

Default access

The only difference between class and struct is default access: class members are private by default, struct members are public by default. struct Point { int x, y; }; means x and y are public without the public: label.

Use struct for simple data containers without behavior: struct Point { int x, y; }; Use class when you need encapsulation and methods: class BankAccount { private: double balance; public: void deposit(double amount); }; This is a convention, not a rule.

Structs can have private members and methods, classes can be all public. But following the convention makes your code's intent clearer to other developers.