C++ enum Keyword
Example
Create an enumerated type:
enum Level {
LOW = 25,
MEDIUM = 50,
HIGH = 75
};
int main() {
enum Level myVar = MEDIUM;
cout << myVar;
return 0;
}
Definition and Usage
The enum
keyword declares an enumeration, which is a special data type that represents a group of constants (unchangeable values).
To create an enum, use the enum
keyword, followed by the name of the enum, and separate the enum items with a comma.
An enum acts as a data type for a variable. A variable of that type can only contain one of the values specified by the enum.
Syntax
One of the following:
enum enumName {
ITEM1,
ITEM2,
...
}
enum enumName {
ITEM1 = value1,
ITEM2 = value2,
...
}
enumName specifies the name of the data type. Each option is specified as a comma-separated list (ITEM1, ITEM2, ...) within the block. By default each option is assigned a different whole number starting from zero. Optionally, you can assign an integer to each of the items in the enumeration (shown as value1, value2 ...).