|
|
|
|
|
|
|
enum Starch {CORN, RICE, POTATO, BEAN};
enum Grain {WHEAT, CORN, RYE, BARLEY, SORGHUM}; // No |
|
|
|
|
|
|
|
|
type Starch and type Grain are legal by themselves, but together they are not. Identifiers in the same scope must be unique. CORN cannot be defined twice. |
|
|
|
|
|
|
|
|
Suppose you are writing a program for a veterinary office. The program must keep track of different kinds of animals. The following enumeration type might be used for this purpose. |
|
|
|
|
|
|
|
|
RODENT is a literal, one of the values in the data type Animals. Be sure you understand that RODENT is not a variable name. Instead, RODENT is one of the values that can be stored into the variables inPatient and outPatient. Let's look at the kinds of operations we might want to perform on variables of enumeration types. |
|
|
|
|
|
|
|
|
does not assign to inPatient the character string DOG, nor the contents of a variable named DOG. It assigns the value DOG, which is one of the values in the domain of the data type Animals. |
|
|
|
|
|
|
|
|
Assignment is a valid operation, as long as the value being stored is of type Animals. Both of the statements |
|
|
|
|
|
|
|
|
inPatient = DOG;
outPatient = inPatient; |
|
|
|
|
|
|
|
|
are acceptable. Each expression on the right-hand side is of type AnimalsDOG is a literal of type Animals, and inPatient is a variable of type Animals. Although we know that the underlying representation of DOG is the integer 2, the compiler prevents us from using this assignment: |
|
|
|
|
|