12.2 C-Style Enumerations
The declaration
enum Color {RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET};
creates a new type named Color whose values are the seven colors listed between the curly
braces. Because the valid values are explicitly listed or enumerated in the declaration, this kind of
type is called an enumeration.
ENUMERATION DECLARATIONS
The declaration of an enumeration must:
1. Provide a name for the enumeration, which becomes the name of a new type
2. Explicitly list all of the values (called enumerators) of this new type
In the example above, Color is the name of the enumeration, and its enumerators are the
identifiers
RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
When the compiler encounters such a declaration, it performs an object-to-integer mapping,
associating the integer 0 with the first identifier in this list, the integer 1 with the second, and so
on. Thus, for the preceding declaration, the compiler makes the following associations:
12.2 C-Style Enumerations 745
As another example, the declaration
enum Gender {FEMALE, MALE};
declares a new type Gender whose values are the identifiers FEMALE and MALE; the compiler will
associate the integer 0 with FEMALE and the integer 1 with MALE. Similarly, the declaration
enum HandTool {HAMMER, PLIERS, SAW, SCREWDRIVER};
constructs a new type HandTool whose values are HAMMER, PLIERS, SAW, and SCREWDRIVER, and
associates the integers 0, 1, 2, and 3 with these identifiers, respectively. By contrast, neither of
the declarations
enum Zipcodes {12531, 14405, 21724, 30081}; // ERROR!
enum LetterGrades {A, A-, B+, B, B-, C+, C, // ERROR!
C-, D+, D, D-, "FAIL" };
is a valid enumeration, because each contains items that are not valid identifiers.
C++ also allows the programmer to specify explicitly the values given to the enumerators.
For example, the declaration
enum NumberBase {BINARY = 2,
OCTAL = 8,
DECIMAL = 10,
HEX = 16, HEXADECIMAL = 16};
associates the identifiers BINARY, OCTAL, DECIMAL, HEX, and HEXADECIMAL with the values 2, 8,
10, 16, and 16, respectively. Similarly, if we wished to have the values 1, 2, . . . , 7 associated
with the seven colors given earlier (instead of 0 through 6), we could use the declaration
enum Color {RED = 1, ORANGE = 2, YELLOW = 3, GREEN = 4,
BLUE = 5, INDIGO = 6, VIOLET = 7};
or more compactly,
enum Color {RED = 1, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET};
because the integer associated with an enumerator is, by default, one more than the integer asso-
ciated with the preceding enumerator. The iostream library uses an enumeration declaration
something like
R
E
D O
R
ANG
E
Y
ELL
O
W
G
R
EE
N
B
L
U
E
I
ND
I
GO V
I
O
LET
01 23456
As another example, the declaration
enum Gender {FEMALE, MALE};
declares a new type Gender whose values are the identifiers FEMALE and MALE; the compiler will
associate the integer 0 with FEMALE and the integer 1 with MALE. Similarly, the declaration
enum HandTool {HAMMER, PLIERS, SAW, SCREWDRIVER};
constructs a new type HandTool whose values are HAMMER, PLIERS, SAW, and SCREWDRIVER,
and associates the integers 0, 1, 2, and 3 with these identifiers, respectively. By contrast, neither
of the declarations
enum Zipcodes {12531, 14405, 21724, 30081}; // ERROR!
enum LetterGrades {A, A-, B+, B, B-, C+, C, // ERROR!
C-, D+, D, D-, "FAIL" };
is a valid enumeration, because each contains items that are not valid identifiers.
C++ also allows the programmer to specify explicitly the values given to the enumerators.
For example, the declaration