What is the difference between struct and class in C + +?

Source: Internet
Author: User
To discuss the difference between the structure (struct) and Class (class), what do you think the structure (struct) has to do with the class instead?
---------------------------------------------------------------

Ask too many people, simply tidy up a FAQ.
First of all, the question should be discussed only syntactically, and if there is a discussion of differences in programming styles between different people, there is no answer to this question. After all, different people have different preferences.

Syntactically, in C + + (only C + +). Class and struct are only two points apart when doing type definitions:
(i) Default inherited permissions. If unspecified, the inheritance from class is handled in private, and the inheritance from struct is processed by public inheritance;
(ii) Default access rights for members. The Member of class defaults to private permissions, and struct defaults to public permissions.
In addition to these two points, class and struct are basically one thing. There is no other difference in grammar.

Can not because of learning C always feel that even C + + struct and class are very different, the following list of instructions may be more boring, because struct and class is basically the same thing, needless to say. However, these instructions may help to clarify some common misconceptions about struct and class:
(1) can have member functions, including various constructors, destructors, overloaded operators, friend class, friend structure, friend function, virtual function, pure virtual function, static function;
(2) can have a large pile of public/private/protected modifiers in the inside;
(3) Although this style is no longer advocated, both grammars can be initialized using curly braces: A A = {1, 2, 3}; Whether a is a struct or a class, if the class/structure is simple enough, for example, all members are public, all members are simple types, Constructors that are not explicitly declared.
(4) Can carry out complex inheritance and even multiple inheritance, a struct can inherit from a class, vice versa; a struct can inherit 5 classes and 5 struct at the same time, although this is not good.
(5) If the class design needs to pay attention to OO principles and style, then there is no reason to say design struct need not pay attention to.
(6) Again, all of the above is referred to in C + + language, as in the case of C, C is not "class", and C struct fundamentally is just a packaging data grammar mechanism.
---------------------------------------------------------------

Finally, as the two keywords for the language, except for the difference in defining the type, there is a little more: "Class" is also used to define template parameters, like "TypeName". But the keyword "struct" is not used to define template parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.