Enumerated Constants
Enumerated constants enable you to create new types and then to define variables of those types whose values are restricted to a set of possible values. For example, you can declare COLOR to be an enumeration, and you can define five values for COLOR: RED, BLUE, GREEN, WHITE, and BLACK.
The syntax for enumerated constants is to write the keyword enum, followed by the type name, an open brace, each of the legal values separated by a comma, and finally, a closing brace and a semicolon. Here's an example:
enum COLOR { RED, BLUE, GREEN, WHITE, BLACK };
This statement performs two tasks:
-
It makes COLOR the name of an enumeration; that is, a new type.
-
It makes RED a symbolic constant with the value 0, BLUE a symbolic constant with the value 1, GREEN a symbolic constant with the value 2, and so forth.
Every enumerated constant has an integer value. If you don't specify otherwise, the first constant will have the value 0, and the rest will count up from there. Any one of the constants can be initialized with a particular value, however, and those that are not initialized will count upward from the ones before them. Thus, if you write
enum Color { RED=100, BLUE, GREEN=500, WHITE, BLACK=700 };
then RED will have the value 100; BLUE, the value 101; GREEN, the value 500; WHITE, the value 501; and BLACK, the value 700.
You can define variables of type COLOR, but they can be assigned only one of the enumerated values (in this case, RED, BLUE, GREEN, WHITE, or BLACK, or else 100, 101, 500, 501, or 700). You can assign any color value to your COLOR variable. In fact, you can assign any integer value, even if it is not a legal color, although a good compiler will issue a warning if you do. It is important to realize that enumerator variables actually are of type unsigned int, and that the enumerated constants equate to integer variables. It is, however, very convenient to be able to name these values when working with colors, days of the week, or similar sets of values. Listing 3.7 presents a program that uses an enumerated type.
Listing 3.7 A Demonstration of Enumerated Constants
0: #include <iostream> 1: int main() 2: { 3: enum Days { Sunday, Monday, Tuesday, 4: Wednesday, Thursday, Friday, Saturday }; 5: 6: Days today; 7: today = Monday; 8: 9: if (today == Sunday || today == Saturday) 10: std::cout << "\nGotta' love the weekends!\n"; 11: else 12: std::cout << "\nBack to work.\n"; 13: 14: return 0; 15: }
Output
Back to work.
Analysis On line 3, the enumerated constant DAYS is defined, with seven values. Each of these evaluates to an integer, counting upward from 0; thus, Monday's value is 1.
We create a variable of type Daysthat is, the variable will contain a valid value from the list of enumerated constants. We assign the enumerated value Monday to that variable on line 7 and then we test that value on line 9.
The enumerated constant shown in line 7 could be replaced with a series of constant integers, as shown in Listing 3.8.
Listing 3.8 Same Program Using Constant Integers
0: #include <iostream> 1: int main() 2: { 3: const int Sunday = 0; 4: const int Monday = 1; 5: const int Tuesday = 2; 6: const int Wednesday = 3; 7: const int Thursday = 4; 8: const int Friday = 5; 9: const int Saturday = 6; 10: 11: int today; 12: today = Monday; 13: 14: if (today == Sunday || today == Saturday) 15: std::cout << "\nGotta' love the weekends!\n"; 16: else 17: std::cout << "\nBack to work.\n"; 18: 19: return 0; 20: }
Output
Back to work.
Analysis The output of this listing is identical to Listing 3.7. Here, each of the constants (Sunday, Monday, and so on) was explicitly defined, and no enumerated Days type exists. Enumerated constants have the advantage of being self-documentingthe intent of the Days enumerated type is immediately clear.