Enum type (enum)

Source: Internet
Author: User
Tags arithmetic

If you need several possible values for a variable, you can be defined as an enumeration type. An enumeration is called a possible value for a variable or an object that may be present, one by one cases are lifted out.

Let me give you an example to illustrate, to make it more clear, for example, there is a pen in a pencil box, but you do not know what it is before you open it, it may be a pencil or a pen, there are two possibilities, then you can define an enumeration type to represent it!

Enum box{pencil,pen};//Here you define an enumeration type variable called box, which contains two elements, also called enumeration elements here are pencil and pen, respectively, for pencils and pens.

Here's to say, if you want to define two variables with the same attribute enumeration type, then you can define it in two ways:

Enum Box{pencil,pen};

Enum Box box2;//or abbreviated as box Box2;

Another is defined at the time of the Declaration.

enum {pencil,pen}box,box2;//is defined at the same time as the declaration!

Enumeration element systems in enumeration variables are treated according to constants, so they are called enumerated constants, they cannot perform ordinary arithmetic assignments, (pencil=1;) Such writes are wrong, but you can assign them at the time of declaration!

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

But the special thing to note here is that if you do not perform element assignment then the element will be automatically incremented by the system from 0 automatically, when it comes to automatic assignment, if you only define the first one then the system will add the value of the previous element to the next element plus 1 operations, for example

Enum box{pencil=3,pen};//Here Pen is the 4 system will automatically pen=4 defined assignment operation!

In front of so many, the following gives a complete example of the following code to learn more complete learning!

#include <iostream>
using namespace Std;
void Main (void)
{
Enum Egg {a,b,c};
Enum Egg test; Here you can shorthand for egg test;
Test = C; Assigns the enumeration variable test to an element operation, the assignment operation is not called an assignment operation in order to make it clear that an enumeration variable cannot be directly assigned an arithmetic value, for example (test=1;) Such operations are not accepted by the compiler, and the correct way is to force type conversions, for example ( Test = (enum egg) 0;)!
if (test==c)
{
cout << "Enumeration variable judgment: Test enumeration corresponds to the enumeration element is C" << Endl;
}
if (test==2)
{
cout << "Enumeration variable judgment: Test enumeration element value is 2" << Endl;
}
cout << a << "|" << b << "|" << test <<endl;
Test = (enum egg) 0; Force type conversions
cout << "enum variable test value changed to:" << test <<endl;
Cin.get ();
}

The last thing you see here is that an enumeration element (or an enumerated constant) in an enumeration variable is automatically promoted to an arithmetic type in special cases!

#include <iostream>
using namespace Std;
void Main (void)
{
Enum Test {a,b};
int c=1+b; Automatically promoted 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.