The difference between a struct and a class

Source: Internet
Author: User

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

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.