The difference between C + + structure body
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. But the structure in C does not allow functions;
That is, in C + +, the struct can have member variables, you can have member functions, you can inherit from other classes, you can inherit from other classes, you 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 . The non-static member function in the class has the this pointer (and the struct is not wrong, has been misled, has the this pointer as the member function of the test struct), and the class's keyword class can be used as the keyword for template templates. 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 + + The structure and class in this paper embody the combination of data structure and algorithm.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The difference between a struct and a class