C + + composite type (struct)

Source: Internet
Author: User

In fact, C + + structure can be understood as a python-like dictionary, I personally understand, but there are differences

First look at the structure

#include <iostream> keyword          tags become the name of the new type struct          inflatable{    std::  String  mystr;                   struct member     char name[];     float volume;     Double Price ;};

C + + can omit the keyword struct when declaring a struct variable

Also pay attention to external declarations, and local declarations

#include <iostream>#include<string>#include<cstring>structinflatable{std::stringmystr; Charname[ -]; floatvolume; DoublePrice ;};intMainintargcChar Const*argv[]) {  using namespacestd; Inflatable Guest= {    "Hello",    "Glorious Gloria",    1.88,    29.99  }; Inflatable PAL= {    " World",    "Audacious Arthur",    3.12,    32.99  }; intA=12.40; Std::cout<< Guest.mystr <<'\ n'; Std::cout<< a <<'\ n'; Std::cout<<"Expand your guest list with <<"<< Guest.name <<">>"<<" and"<<"<<"<< Pal.name <<">>"<<'\ n'; Std::cout<<"You can have both for $"<< Guest.price + pal.price <<'\ n'; return 0;}
View Code

Other Structure Properties
#include <iostream>#include<string>#include<cstring>structinflatable{std::stringmystr; Charname[ -]; floatvolume; DoublePrice ;};intMainintargcChar Const*argv[]) {  using namespacestd; Inflatable Guest= {    "Hello",    "Glorious Gloria",    1.88,    29.99  }; Inflatable Choice=Guest; This method calls the member to assign a value
Or

Inflatable choice;
Choice = Guest;

Std::cout<<"Expand your guest list with <<"<< Guest.name <<">>"<<'\ n'; Std::cout<<"Choice CHOICE.MYSTR---->"<< Choice.mystr <<'\ n'; return 0;}

It's OK

struct inflatable{    std::string  mystr;     Char name[];     float volume;     Double  = {"hello""glorious"1.11  One};
Of course, you can also not assign a value

Array of structures

You can also create an array of elements as structs, and the methods are identical to creating arrays of primitive types. For example:

#include <iostream>#include<string>#include<cstring>structinflatable{std::stringmystr; Charname[ -]; floatvolume; DoublePrice ;} Mr_glitz= {"Hello","Glorious",1.11, One};intMainintargcChar Const*argv[]) {  using namespacestd; Inflatable guests[2] = {    {"Hello","Doman",2.1,2.22},    //{"World", "Corleone", 2.2, 3333}  }; Std::cout<<"Guests[0].mystr:"<< guests[0].mystr <<'\ n'; Std::cout<<"Guests[1].name:"<< guests[1].name <<'\ n'; return 0;}

Bit fields in the structure

  

struct torgle_register{  int SN:  4;   int :4;   BOOL 1 ;   BOOL 1  = {truefalse};

C + + composite type (struct)

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.