< previous page page_536 next page >

Page 536
Enumeration Types
C++ allows the user to define a new simple type by listing (enumerating) the literal values that make up the domain of the type. These literal values must be identifiers, not numbers. The identifiers are separated by commas, and the list is enclosed in braces. Data types defined in this way are called enumeration types. Here's an example:
enum Days {SUN, MON, TUE, WED, THU, FRI, SAT};
This declaration creates a new data type named Days. Whereas Typedef merely creates a synonym for an existing type, an enumeration type like Days is a new type and is distinct from any existing type.
The values in the Days typeSUN, MON, TUE, and so forthare called enumerators. The enumerators are ordered, in the sense that SUN < MON < TUE  < FRI < SAT. Applying relational operators to enumerators is like applying them to characters: the relation that is tested is comes before or comes after in the ordering of the data type.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Enumeration Type A user-defined data type whose domain is an ordered set of literal values expressed as identifiers.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Enumerator One of the values in the domain of an enumeration type.
Earlier we saw that the internal representation of a char constant is a nonnegative integer. The 128 ASCII characters are represented in memory as the integers 0 through 127. Values in an enumeration type are also represented internally as integers. By default, the first enumerator has the integer value 0, the second has the value 1, and so forth. Our declaration of the Days enumeration type is similar to the following set of declarations:
typedef int Days;
const int SUN = 0;
const int MON = 1;
const int TUE = 2;
  .
  .
  .
const int SAT = 6;
If there is some reason that you want different internal representations for the enumerators, you can specify them explicitly like this:

 
< previous page page_536 next page >