What is the difference between struct and class in C++?
Show answer and explanation
struct and class define the same kind of class type and can both have constructors, member functions, templates, inheritance, and virtual dispatch. The language differences are defaults: members and base classes are public in a struct, but private in a class. Conventionally, structs model simple value-like aggregates and classes emphasize hidden invariants, but that is a style choice rather than a language restriction.
struct and class define the same kind of class type and can both have constructors, member functions, templates, inheritance, and virtual dispatch. The language differences are defaults: members and base classes are public in a struct, but private in a class. Conventionally, structs model simple value-like aggregates and classes emphasize hidden invariants, but that is a style choice rather than a language restriction.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.