C++ Programming/Programming Languages/C++/Code/Keywords/enum

The   keyword is used to create an enumerated type named name that consists of the elements in name-list. The var-list argument is optional, and can be used to create instances of the type along with the declaration.


 * Syntax:

For example, the following code creates the desired data type:

The line of code creates a new data type " " that may take on only one of four possible values: " ", " ", " ", and " ". In general the   command takes the form:

While the second line of code creates a new variable with this data type and initializes it to value to ".  The other lines create new variables of this new type and show some initializations that are (and are not) possible.

Internally enumerated types are stored as integers, that begin with 0 and increment by 1 for each new possible value for the data type.

While enumerated types are not integers, they are in some case converted into integers. For example, when we try to send an enumerated type to standard output.

For example:

Will produce the output: My hair color is 0 My eye color is 2 My skin color is 1 I am seasick!

We could improve this example by introducing an array that holds the names of our enumerated type such as:

In this case hair is automatically converted to an integer when it is index arrays. This technique is intimately tied to the fact that the color Red is internally stored as "0", Green is internally stored as "1", and Blue is internally stored as "2". Be Careful! One may override these default choices for the internal values of the enumerated types.

This is done by simply setting the value in the   such as:

In fact it is not necessary to an integer for every value of an enumerated type. In the case the value, the compiler will simply increase the value of the previous possible value by one.

Consider the following example:

Here the internal value of " " is 2, " " is 3, " " is 6 and " is 7. Be careful to keep in mind when using this that the internal values do not need to be unique.

Enumerated types are also automatically converted into integers in arithmetic expressions. Which makes it useful to be able to choose particular integers for the internal representations of an enumerated type. One may have enumerated for the width and height of a standard computer screen. This may allow a program to do meaningful calculations, while still maintaining the benefits of an enumerated type.

It should be noted that the internal values used in an enumerated type are constant, and cannot be changed during the execution of the program.

It is perhaps useful to notice that while the enumerated types can be converted to integers for the purpose arithmetic, they cannot be iterated through.

For example: This will fail to compile. The problem is with the loop. The first two statements in the loop are fine. We may certainly create a new month variable and initialize it. We may also compare two months, where they will be compared as integers. We may not increment the cur_month variable. " " evaluates to an integer which may not be stored into a " " data type.

In the code above we might try to fix this by replacing the loop with:

This will work because we can increment the integer " ".