- Structures are user-defined data types that group variables of different types into a single unit.
- They are particularly useful when managing related data.
- While structures originated from the C language, C++ enhances them by allowing member functions, constructors, and even inheritance.
- The
struct
keyword is used to define a structure.
- Default access level: public (structure), private (class).
- Usage preference: For simple data models (structure), For complex abstractions (class).
- Inheritance: public by default (structure), Private by default (class).
- Encapsulation: Less restrictive by design (structure), Promotes encapsulation (class).
- Except for the four differences mentioned above, the functions provided by structures are the same as those in classes.
- When using structures, you should treat them like in C code, not as classes.