1. Enumeration
Enum types are declared as: enum enum type name {variable Value list};
The variable value list is all integer variables, and don't forget the trailing semicolon!
For example, enum weekday {Sun,mon,tue,wed,thu,fri,sat};
Here's the enumeration element Sun, Mon ... Do not specify a value, they use the default value, followed by 0, .... That is, Sun is 0,mon or 1,...sat is 6. You can also specify the value of an enumeration element when declaring an enumeration type, such as
Enum Weekday {sun=7, mon=1, Tue, Wed, Thu, Fri, Sat};
Here Sun is 7,mon is 1, the value behind the mon on the basis of adding 1, that is Tue equals 2, Wed 3...sat is 6.
Enumeration elements are treated as constants, and they cannot be assigned values except when declared, such as sun=0, which is illegal.
Enumeration types We declare that we can then use the types like int, float, and so on, to define the variables of the enumeration type . When an integer value needs to be assigned to an enumeration variable, the type conversion should be enforced.
Note: This simply defines an enumeration, similar to defining a class, which can then be used to define the enumeration object as a type.
2. Structural body
A struct is a combination of several data of different data types that can be of the same data type, declared as:
struct struct Name {data type member name 1; data type member name 2;: Data type Member name N;};
don't forget the last semicolon here, too.
The structure type variable is defined in the following format: struct type name struct body variable name;
You can declare struct types and then define struct variables later, or you can put them together.
The size of a struct variable that occupies memory can be computed with sizeof (struct type name or variable name) .
You can assign an initial value directly when defining a struct-body variable. Struct members are used in the following form: struct variable name. Member name.
struct Student//Student information structure
{
int num;//No.
Char name[20];//name
Char sex;//Sex
int age;//Age
}stu={97001, "Lin Lin", ' F ', 19}; When declaring a struct type, define a variable of the struct type and assign the initial value. can also be separated.
int _tmain (int argc, _tchar* argv[])
{
cout<<stu.num<< "<<stu.name<<" "<<stu.sex<<" "<<stu.age;
return 0;
}
3. Community Union
Sometimes it takes a couple of different types of variables to share a set of internal certificates , and then a union is required, and the syntax of the Union type declaration is:
Union Union name
{
Data type member name 1;
Data type member name 2;
:
Data type member name N;
};
The syntactic form of a union type variable declaration is: union type name Union variable name;. The reference form of a union type variable is a union variable name. Member name.
Union Uarea {Char c_data; Short S_data; Long L_data; }
How does a union type consume memory cells in memory? The amount of memory that a consortium occupies is the amount of memory it needs for the largest member of the memory, just like the Uarea on top, which occupies 4 bytes as long as the maximum member size. the value of a union in which only one member can be placed. Uarea put C_data when C_data occupy the first byte, put S_data S_data occupy the first two bytes, put L_data when l_data occupy all 4 bytes.
C + + Custom data type enumeration, struct, common body