Summary of struct and class in C # and C + +

Source: Internet
Author: User

In C #, structs can actually encapsulate methods and data just like class. Please refer to the following code.

1 usingSystem;2 3 namespaceTestdiffinstructclass4 {5      Public structFather6     {7         //private string _name = "Father";8         //private int _age = +;9         Private string_name;Ten         Private int_age; One  A         //Public father () -         //{ -         //Console.WriteLine ("Default ctor is called"); the         //} -          PublicFatherstringNameintAge ) -         { -_name =name; +_age =Age ; -Console.WriteLine ("ctor with Param is called"); +         } A          Public Override stringToString () at         { -             return string. Format ("father ' s name is:"+ _name +"And of age :"+_age); -         } -     } -  -     //Public struct Son:public father in     //{ -     //} to  +     class Program -     { the         Static voidMain (string[] args) *         { $Father F =NewFather"Neo", -);Panax Notoginseng Console.WriteLine (f.tostring ()); -         } the     } +}

But there are a few points to note, which are the comments above:

1. There can be no instance field initial value in the structure;

2. The structure cannot contain explicit parameterless constructors;

3. Structs cannot inherit.

But in C + +, it shows a different nature. In C + +, struct and class are not very different. In most cases, it can be swapped, but there are some differences, specifically described below.

1. the literal difference

The literal struct is the abbreviation of structure, usually called "struct", which is used in C language to organize many kinds of data, multiple variables together, it is easy to express more complex data types in C + + in order to be compatible with C language, and retains all the functions of C language.

and class, called "Class," is a keyword added by C + + to support the concept of "class" in object-oriented thinking concepts, and has a more powerful function than structs, not only to organize the data together like a struct in C, but also to organize data-related methods together. It also adds features such as virtual functions and inheritance to support object-oriented programming.

While the literal struct differs from class, it is essentially the same in C + +, where the struct in C + + can contain not only data members, but also the new object-oriented features that are supported by class, only slightly different in the following details.

Since the two are literally different, in order to make better use of this, it is recommended that when using structs in C + +, only the features in C are used to represent complex data without encapsulation of methods, which can also improve the readability of the software.

For an introduction to the following differences, please refer to the reference code.

1#include <iostream>2 using namespacestd;3 4 structSC5 {6     voidfunc () {}7 };8 9 structSB:SCTen { One }; A  - classCA - { the     voidfunc () {} - }; -  - classCB:CA + { - }; +  A intMain () at { -     //Test the default access - SC SC; -Sc.func ();//No error - CA CA; -Ca.func ();//error C2248: "Ca::func": unable to access private member (declared in "CA" Class) in     //test the Inherit - SB SB; toSb.func ();//No error + CB CB; -Cb.func ();//error C2248: "Ca::func": unable to access private member (declared in "CA" Class) the}

2. Default member permissions differences

The member default permission for a struct is public, and the member default permission for class is private.

3. default inheritance mode

The default inheritance for structs is public, and the default inheritance for class is private.

4. used to define template parameters

Templates are new to the C + + language, and C does not, only class can be used to define parameters, and structs are not allowed, for example:

1 Template2 classTValue {3 Private: 4 T _v;5  Public: 6 TValue (T v): _v (v) {}7T Get (void )8     {9         return_v;Ten     } One};

Only class can be used here, and structs cannot be used. Of course, it is also possible to use TypeName instead of Class,class and TypeName to be interchangeable only when defining template parameters, and it is recommended to use TypeName at this point because it is more readable than human language.

Reference article:


Summary of struct and class in C # and 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: 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.