structs in C + + can define a function
Structs in C are different from structs in C + +: Structs in C can only customize data types, no functions are allowed in structs, and struct bodies in C + + may join member functions.
The similarities and differences of structs and classes in C + +:
One, the same: The structure can contain functions, you can also define public, private, protected data members, after the structure is defined, you can use the struct name to create the object. However, structs in C do not allow functions, that is, in C + +, struct can have member variables, can have member functions, can inherit from other classes, can also be inherited by other classes, can have virtual functions.
Two, the difference: struct definition The members are public by default, and the members in the class definition are private by default. A non-static member function in a class has the this pointer, and the class's keyword class can be used as the keyword of the template Template<class t> class a{}; And the struct is not.
In fact, the struct in C only involves the data structure, not the algorithm, that is, the data structure and the algorithm are separated in C, and a class or a struct in C + + can contain functions (this function is commonly referred to as member functions in C + +), C + + Structures and classes in the data structure and the combination of algorithms
The differences between structs in C and structs in C + + and classes in C + +