Differences between C and C++ structures

  • 2020-04-02 01:46:04
  • OfStack

Structs in C differ from structs in C++ : structs in C can only customize data types, structs are not allowed to have functions, whereas structs in C++ can add member functions.

Similarities and differences between structs and classes in C++ :

I. similarities: The structure can contain functions; You can also define public, private, protected data members; After the struct is defined, you can create an object with the struct name. But the structure in C is not allowed to have functions; That is, in C++, you can have member variables, you can have member functions, you can inherit from other classes, you can be inherited from other classes, you can have virtual functions.

Ii. Differences: The default member in the struct definition is public, while the default member in the class definition is private. The non-static member function in the class has this pointer, (while there is no error in the struct, I have been misled, the member function of the tested struct also has this pointer), and the keyword of the class class can be used as the keyword of the template template, namely template < The class T > Class A, {}. A struct can't.

Structure of C, in fact, only to the data structure, and has nothing to do with algorithm, that is to say, the separation of data structures and algorithms in C is, by C + + class or a struct can contain function (this function in C + + we usually called member functions), structure and class in C + + embodies the combination of data structures and algorithms.


Related articles: