The differences between structs in C and structs in C + + and classes in C + +

Source: Internet
Author: User

   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 + +

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.