The struct keyword of the C language is retained in C++ and expanded. In C language, struct can only contain member variables, not member functions. In C++, struct is similar to class, which can contain both member variables and member functions.
The struct and class in C++ are basically the same, only a few details are different:
-
When using class, the members in the class are all private attributes by default; while using struct, the members in the structure are all public attributes by default.
-
Class inheritance defaults to private inheritance, while struct inheritance defaults to public inheritance (the chapter "C++ Inheritance and Derivation" will explain inheritance).
-
A class can use templates, but a struct cannot (templates are covered in the chapter "Templates, Strings, and Exceptions").
C++ did not abandon the struct keyword in C language, and its significance is to give C language program developers a sense of belonging, and to make the C++ compiler compatible with projects previously developed in C language.
When writing C++ code, I strongly recommend using class to define classes and struct to define structures, which makes the semantics clearer.