C ++ community

C language is successfully developed in 1972 by the US Bell Labs, at the time considered a high-level language, many of the new features are so assembly language programmers envy, like today's Go language, at birth being sought. C language is "fashionable" language, later a lot of software using C language development, including Windows, Linux and so on. But with the rapid improvement of computer performance, the hardware configuration and has a far cry from a few decades ago, the software size has continued to grow, a lot of volume software more than 1G, such as PhotoShop, Visual Studio and other software development using C language it is very difficult, and this time C ++ came into being. Well today we are speaking about the C ++ community.

It is sometimes necessary to make several different types of variables stored in the memory unit in the same segment. For example, to an integer variable, a variable character, a variable double words of memory in the same address.
Here Insert Picture Description
Different number of bytes than three variables accounted for in memory, but are beginning to be stored at the same address. That is, covering the use of technology, several variables overwrite each other. This makes several different variables accounted structure with a region of memory called union (Union) type of structure (Some translated joint).

Statement of the type common general form is:

union {union type name} tabular member;

The general form of the union variables defined as follows:

The union type name union variable name;

Of course, also define a union variable declaration while a union type, may not share the name of the type of union variables defined directly. See for example, "union" and "structure" is defined similar form. But their meanings are different. Share memory structure variable length members occupy memory and length. Each member respectively occupies its own memory unit. Union variable memory occupied by a length equal to the length of the longest member.

Features common type of data

The purpose of using a union variable is desirable to store several different types of data in a memory segment. But please note: In each instant can only store one, rather than several at the same time deposit.

It is able to access a shared member body variable was last assigned, after the assignment of a new member of the original members of the lost role.

Address Union variables and addresses of its members are of the same address.

Body can not be assigned to the common variable name; variable name can not attempt to obtain a reference value; it can not be initialized when defining a union variable; the variable name can not be used as a union function parameter.
It provided data for a number of personnel, including students and teachers. Student data include: name, number, gender, occupation and grade. Teachers include: name, number, gender, occupation, job. As can be seen, the data included students and teachers are different.

Now required to put them on the same table, if the job entry is s (student), the Item 5 to grade (grades). That Li is the third grade. If the job is a term t (teacher), then the item 5 position (position). Wang is prof (professor). Obviously Item 5 can be used to process the union (the class and position in the same memory section) pair. Personnel required input data, and then output. For simplicity, located only two people (a student, a teacher).

Procedures are as follows:

#include

#include

#include // because the use of control characters in the output stream setw

using namespace std;

struct

{

int num;

char name[10];

char sex;

char job;

union P // declare union type

{

int grade; // Year

char position [10]; // position

} Category; // category is the union member variables

} Person [2]; // define Union arrays person, comprising two elements

int main ()

{

int i;

for (i = 0; i <2; i ++) // two student input data

{

cin>>person[i].num>>person[i].name

person[i].sex>>person[i].job;

if(person[i].job==‘s’)

cin >> person [i] .category.grade; // if students enter Grade

else

if (person[i].job==‘t’)

cin >> person [i] .category.position; // if teachers enter post

}

cout<<endl<<“No. Name sex job grade/position”<<endl;

for(i=0;i<2;i++)

{

if (person[i].job==‘s’) cout<<person[i].num<<setw(6)

<<person[i].name<<" “<<person[i].sex <<” "

<<person[i].job<<setw(10)<<person[i].category.grade<<endl;

else cout<<person[i].num<<setw(6)<<person[i].name

<<" “<<person[i].sex <<” "<<person[i].job

<<setw(10)<<person[i].category.position<<endl;

}

return 0;

}

Operation is as follows:

101 Li fs 3↙ (note no spaces between the letters f and s input)

102 Wang mt prof↙ (note no spaces between letters of the input m and t)

No. Name sex job grade/position 101 Li f s 3 102 Wang m t prof

In order to output aligned vertically, in the statement using the cout setw control symbols and insert a space. Often need to test more than once.

Shared access mode variable A

You can not reference a union variable and can only refer to members of the union variables. For example, the following reference is correct:

AI (referring to a union member variable integer i)

A.ch (reference character union member variable ch)

AF (referring to a union variable double members d)

Union not only references variables, e.g.

cout<<a;

Is wrong, it should be written

cout<<a.i;

or

cout<<a.ch;

Published 239 original articles · won praise 3 · Views 3144

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105176977