Usage of Enum in C/C ++

Source: Internet
Author: User

If a variable requires several possible values, it can be defined as an enumeration type. The reason for enumeration is to enumerate the possible values of variables or objects.

For example, to make everyone better understand, for example, there is a pen in a pencil box, but you don't know what it is before you open it, it may be a pencil or a pen. There are two possibilities, so you can define an enumeration type to represent it!

Enum box {penpencil, pen}; // here you define a variable of Enumeration type called box, the enumerated variable contains two elements, the pencil and pen, respectively.

If you want to define two variables with the same Enumeration type, you can use the following two methods to define them!

Enum box {penpencil, pen };

Enum box box2; // or abbreviated to box box2;

Another way is to define the statement at the same time.

Enum {penpencil, pen} box, box2; // define the statement at the same time!

The enumeration Element System in enumeration variables is processed according to constants, so it is called enumeration constants. They cannot assign values to ordinary arithmetic values (penpencil = 1 ;) this statement is incorrect, but you can assign values when declaring it!

Enum box {penpencil = 1, pen = 2 };

However, if you do not assign values to elements, the system automatically increments the values from 0, if you only define the first element, the system will add 1 to the value of the previous element to the next element,

For example:
Enum box {penpencil = 3, pen}; // here pen is 4. The system will automatically assign values to pen = 4!

As mentioned above, the following is a complete example. You can learn more completely through the following code!

# Include <iostream>
Using namespace STD;

Void main (void)
{
Enum egg {a, B, c };
Enum egg test; // here you can abbreviated it as egg test;

Test = C; // assign the element operation to the enumerated variable test. Here, the element assignment operation is not called the value assignment operation to make it clear that the enumerated variable cannot be directly assigned a value, for example, operations such as (test = 1;) are not accepted by the compiler. The correct method is to force type conversion first, for example (test = (Enum egg) 0 ;)!

If (test = C)
{
Cout <"enumeration variable judgment: The enumerated element corresponding to the test enumeration is C" <Endl;
}

If (test = 2)
{
Cout <"enumeration variable judgment: the value of the test enumeration element is 2" <Endl;
}

Cout <A <"|" <B <"|" <test <Endl;

Test = (Enum egg) 0; // force type conversion
Cout <"the test value of the enumerated variable is changed to:" <test <Endl;
Cin. Get ();
}

The last problem here is that the enumerated elements (or enumeration constants) in the enumerated variables are automatically upgraded to the arithmetic type in special circumstances!

# Include <iostream>
Using namespace STD;

Void main (void)
{
Enum test {a, B };
Int c = 1 + B; // automatically upgraded to arithmetic type
Cout <C <Endl;
Cin. Get ();
}

 

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.