Hello, C + + (14) How to describe "a man named Chen Liangjio, age 33, height 173 cm, weight 61.5 kg"--3.8 describe complex things with struct types

Source: Internet
Author: User

3.8 describing complex things with struct types

Using the variables defined by the basic data types provided by the C + + itself, you can only express something simple. For example, we can define the nage variable with the int type to represent the person's age, and the string type to define the strname variable to represent the person's name. However, the real world is complex, and simply using basic data types is not enough to describe this complex real world. For example, we can't use a basic data type to describe a person's complexity, because he has not only a name, but also attributes such as height, age, and gender to describe. But we notice that complex things are also made up of simple things. Since we can use basic data types to describe simple things, then combine these basic data types to form a structured data type that can describe multiple different properties of the same thing, and not be able to describe complex things? Based on this idea, C + + provides a structure mechanism.

Definition of 3.8.1 structure

In C + +, we use the struct keyword to package multiple base data types into structs, with the following syntax:

struct struct body name {    data type 1    member name 1;    Data type 2    member name 2;    // ...     data Type N    member name N;};   

Where the struct keyword indicates that a struct is to be created; the struct name is the name of the struct to be created, usually using the thing described by the struct as the name of the struct. For example, the structure that we are creating is used to describe the complex object of a person, so the name of the struct can be used human, and within the structure definition, variables of different data types are used to represent the various properties of complex things. For example, we can use the string type of m_strname variable to describe the person's name, with the bool type of m_bmale variable to describe the human gender and so on. These variables are also referred to as member variables of the struct because they collectively make up the struct body. With this in view, we can define human structures that represent the complex things of man in the following ways:

Define structure body Human Describe man this complex thing struct human{    string   m_strname;          /           /////////weight};

After the structure has packaged multiple variables describing a complex object's multiple properties, we can use this struct to define variables to describe this complex thing:

define a human structure variable description Chen Liangjio this person // This structure contains his name, gender and age information human CHENLQ;  

Figure 3-5 Packaging complex things into a structure

The use of 3.8.2 structural body

A struct is a combination of several basic data types, and a natural structure variable is a combination of multiple member variables in a struct, as long as there is a struct variable, which is equal to the fact that we have more than one member variable at the same time. In turn, we can pass the "." Symbols to refer to individual member variables in a struct variable, using them to describe the various properties of a complex thing. For example, we can use the human structure defined above to describe "a man named Chen Liangjio, age 33, height 173 cm, weight 61.5 kg" This complex thing:

//Define a human type variable CHENLQ,//To describe the complex things of man.human CHENLQ; // This person's name is "Chen Liangjio" chenlq.m_strname =  " Chen Liangjio " // age 23 years Chenlq.m_nage = 33// height 173chenlq.m_nheight = 173;  weight 61.5chenlq.m_fweight = 61.5; // man Chenlq.m_bmale =                 

Here we notice that if you want to use struct variables to describe a complex thing, just use "." The symbol refers to each member variable in the struct variable, then assigns each property of the complex object to the member variable accordingly. For example, we want to say that this person's name is "Chen Liangjio", only need to assign this string data to "." The M_strname member variable that represents the name in the CHENLQ struct variable. Other properties, and so on.

It is also necessary to mention that the use of the "." The struct member variable referenced is not much different from the direct use of an independent variable, and we can read and write it as a normal variable. For example:

read/write access to the M_strname member variable in the struct //" Chen Liangjio ";  Read access: Output member variable cout<<" This person's name is:" <<chenlq.m_strName<<endl;     

With structural mechanisms, we can describe more complex things and solve more complex problems. In the previous example, we created an array of arrsalary to hold the employee's salary, if we want the program to also handle the employee's name, gender, age and other additional personnel information. Without the help of structs, we had to create multiple arrays to hold these additional information separately, as the code should be:

 // Define the number of employee data const int NUM = 100000;//string Arrname[num]; //int< Span style= "color: #000000;" > Arrage[num]; // Define an array of Save gender data bool< Span style= "color: #000000;" > Arrmale[num]; // Define an array to hold payroll data int Arrsalary[num];                

This kind of code can solve the problem, but it has a significant disadvantage, that is, the various data describing the same thing is separated from each other, lost the close connection between them. If the individual arrays do not store the employee's data in the same order, it is impossible to know the salary of the employee named Chen Liangqiao.

With structs, we can package all relevant information that describes the same employee, including name, gender, age, and salary, into a single struct type, and then use this struct type to define an array of employee data, which is not only simple in code, And the above impossible task can also be done easily:

//Package variables that describe the multiple attributes of the "employee" complex as a structstructemployee{String M_strname;//NameBOOL M_bmale;//Genderint m_nage;//Ageint m_nsalary;//Wages};//...//Number of Employee payroll dataConstint NUM =100000;//Define an array to hold employee dataEmployee Arremp[num];//Iterate through the array with a for loop structure to find the wages of an employee named "Chen Liangqiao"//int i = 0;i < NUM; + + i) {//if ( "chen liangqiao" = = Arremp[i].m_strname) {//  output this element's name and salary Cout<<arremp[i].m_strname <<  " salary is " <<arremp [I].m_nsalary<<endl;}}         

In this code, through the use of structs, we package a number of variables describing employee information into an employee structure, and then simply define an array of employee types instead of the original four arrays, which is a reflection of the structure's role in C + +- It is easy to package a number of simple things into a single complex thing and make it simple.

Original address: http://www.cnblogs.com/nihaoCPP/p/4010610.html

Hello, C + + (14) How to describe "a man named Chen Liangjio, age 33, height 173 cm, weight 61.5 kg"--3.8 describe complex things with struct types

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.