11. enumerated type

Definition: enum type is a set of data can be customized by the user data type.

Note: bool type can be seen as a pre-defined C ++ language provides enumerated type.

1. The enumeration type definition

    enum <enumerated type name> {<enumeration value table>};

2. Initialize

     Each of the enumerated type enumeration value corresponds to an integer number, the default value of the first enumeration value is 0, followed by 1, it may be displayed in any number of shaping initialize a corresponding enumeration value , without the enumeration defined default value adding 1 to the integer corresponding to the enumeration of its previous value.

     enum {Sun = Day 7, Mon = 1, Tue, Wed, Thu, Fri, SAT}

3. Definition of enumeration

     <Enumeration type> <variable table>;

     Or <enumeration type> {<enumeration value table>} <variable table>;

4. Use of enumeration variables

    1) Assignment

 

        Day d1,d2;
        d1 = SUN; //true
        d2 = 3; // error, but int n = SUN; it is possible
        d2 = (Day) 3; // true but this insecurity, we must ensure that the integer part of the value of the enumeration type set, otherwise there is no meaning

    2) comparison operation

        MON <TUE result is true, when converted to integer arithmetic    

 

    3) arithmetic operations

         d2 = d1 + 1; // error, since it results d1 + 1 is an integer

         d2 = (Day)(d1 + 1);//true

    4) Other

         O: number of inputs can be int, using the switch, then the output copy or

         Access the Classmark: day (0) it corresponds to the first sun enumeration value

Guess you like

Origin www.cnblogs.com/tuzi140301/p/11415312.html