Enumeration type enum (C ++ language)

I, on the enum

In practical problems, the values of some variables are defined within a limited range . For example, the outcome of a race only wins, losses, draws, the match was canceled four cases; only a bag of red, yellow, blue, white, black and five colors of balls; one week only Monday, Tuesday ...... Sunday 7 days. The above data is only a limited number of possible values, although you can use int, char and other types to represent them, but to check the legitimacy of data is a very troublesome thing. C ++, enumerated types designed to solve these problems.

  • Enum type declaration form as follows:

    Enumeration enum Type {name} variable value list;

  • E.g:

    Weekday enum {Sun, Mon, Tue, Wed, Thu, Fri, SAT};

[Note] enumerated type is a basic data type, rather than a constructed type

  • Description of enumeration

    With variables a, b, c is as described above Weekday, can be any of the following way:
    enum Weekday {the MON, the TUE, WEDs, the THU, the FRI, the SAT};
    enum a, b, c;
    or
    enum Weekday {MON, TUE, WED, THU , FRI, SAT} a, b, c;
    or
    enum {MON, TUE, WED, THU, FRI, SAT} a, b, c;


Second, the enumeration type of application notes

  • Enumeration element by constant process, not its assignment. E.g:
SUN=0;     //SUN是枚举元素,此语句非法  
  • Enumeration elements have a default value, in order to start from zero. For example, SUN a value of 0, MON value of 1 ...... SAT is 6.

  • You can assign it a value at the time of the statement, the subsequent variable is incremented by one. Such as

    enum Weekday {SUN, MON = 4, TUE, WED, THU, FRI, SAT}; // where MON = 4, then TUE is 5, WED 6 ......

  • Non-enumeration amount can not be assigned to enumerate the amount, but the amount can be assigned to non-enumeration enum. Such as:

    int a = MY;

  • Without forced conversion, enumeration defined as the amount can only be assigned to a variable of this type of enumeration. E.g:

    Weekday = MON; or Weekday = SUN; other values can be assigned to the enumeration,
    such as: Weekday = 10.


Third, the enumeration operation

  • It can not be arithmetic

    Weekday++;Weekday=MON+TUESDAY;
    Such operations are illegal, because it may result in a violation type restrictions.
    For example: the SUN = Weekday; Weekday ++;
    Weekday are first assigned SUN (SUN 6), then it is incremented, Weekday incremented to 7, and 7 are invalid.

  • You can engage in other types of variable operations

    A int;
    A = the MON +. 1;
    // This is allowed, because the compiler will automatically convert enum type int.


Fourth, enumerated types of applications

One of the purposes enumerated data type is a symbolic name helps make the program self-explanatory. However, because these names are not strings, they are used in the program.

Because the symbolic name of the enumerated data type, they can be associated with an integer value used in the switch statement, as shown in the following procedure, which also demonstrates the use enumerated data type, without actually creating this type of any variable.

#include <iostream>
using namespace std;

enum Roster{Tom = 1,Sharon,Bill,Teresa,John};

int main()
{
    int who;
    cin>>who;
    switch(who)
    {
    case Tom:
        cout<<"Tom's birthday is Junuary 3\n";
        break;
    case Sharon:
        cout<<"Sharon's birthday is April 22\n";
        break;
    case Bill:
        cout<<"Bill's birthday is December 19\n";
        break;
    case Teresa:
        cout<<"Teresa's birthday is February 2\n";
        break;
    case John:
        cout<<"John's birthday is June 17\n";
        break;
    default:
        cout<<"Invalid selection\n";
    }
    return 0;
}  

Guess you like

Origin www.cnblogs.com/xihuashi/p/11519450.html