C + + Common Body enumeration type all

Source: Internet
Author: User

One. Common body Type

1. The concept of a shared body.

Sometimes several different types of variables need to be stored in the same memory unit. For example, there are three variables, their number of bytes is different, but all start from the same address to store. That is, with the overlay technique, several variables cover each other. This structure, which allows several different variables to share the same memory, is called a shared body type structure.

Its general form:

Union Common body Type name

{

Member List

};

Defines the general form of a shared body variable:

Common body type name common body variable name;

For example:

Union data

{

int A;

Char ch;

Double D;

}

Note: The memory length of a struct variable is the sum of the memory lengths of each member and each member has its own memory unit.

The memory length of a shared body variable equals the length of the longest member.

2. How to access the community variables.

You cannot reference a community variable, but only members in a community variable. The following is the correct way to refer:

(1) a.i (integer member I in the reference community variable)

(2) a.ch (reference to the character member in the Community variable CH)

(3) A.F (reference to double type member D in a community variable)

It is not possible to refer to a community variable, such as cout<<a;, which should be written as cout<<a.i; or cout<<a.ch;

Two. Enum type

1. Simply enumerate the variables, and the values of the variables can only be within the range of the values enumerated.

Declares that an enumeration type begins with an enum, for example:

Enum Weekday{sun,mon,tue,wed,thu,fri,sat};

The elements in parentheses are called enumeration elements or enumeration constants. The value that represents a variable of this type can only be one of the values in the element.

The general form of declaring enum types is:

Enum Enum type name {enumeration constant list};

After declaring the enumeration type, it can be used to define variables, such as: Weekday workday,week_end;

Note: 1. The enumeration element is called an enumeration constant when it is processed. They are not variables and cannot be assigned to them. Such as:

Sum=0;mon=0; These are all errors and cannot be assigned to enumeration constants.

2. Enumeration elements as constants, they are valued, C + + compiled in the order they were defined to assign them to 0,1,2,3,4, .... In the above declaration, Sun has a value of 0,mon of 1, .... Like, Workday=mon; This value is output,cout<<workday<<endl; at this point the output value is 0.

You can also set the value of an enumeration element separately when declaring an enumeration type. Such as:

Enum Weekday{sun=7,mon=1,tue,wed,thu,fri,sat}; Specify Sun=7,mon=1, and then add 1,sat=6 sequentially.

3. Enumeration values can be used to determine comparisons.

if (Workday==mon) ...

if (Workday>sun) ...

The comparison rules for enumeration values are compared by their sequential numbers when declaring enum types. If the definition is not specified, the value of the first enumeration element is considered 0. So Mon>sun,sat>fri.

4. An integer cannot be assigned directly to an enumeration variable,

Workday = 2; This is not correct, you should cast the coercion type to be able to assign the value.

Workday= (weekday) 2; or Workday=weekday (2); it's all right. equivalent to Workday=tue;

5. The method for declaring a new type name is:

1. First write the definition statement (such as int i) by defining the variable name;

2. Replace the variable name with a new type name (e.g. change I to count);

3. Add a typedef at the front (e.g. tydedef int COUNT);

4. You can then define the variable with the new type name.

6. Then declare the array type name described above as an example:

1. Write by definition array first: int n[100];

2. Change the variable name n to its own specified type name: int num[100];

3. Add typedef in front, get typedef int NUM[100];

4. Used to define the variable: NUM n; (n is an array containing 100 integer elements).

C + + Common Body enumeration type all

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.