C++ Enumeration Type Detailed Explanation
Classification Programming Techniques
Definition of Enumeration Type: An enumeration type is a derived data type in C++ that consists of a collection of user-defined enumeration constants.
Definition format: The definition format of an enumeration type is as follows:
enum <Type Name> {<Enumeration Constant List>};
Format Description:
Keyword enum – Indicates that the identifier following it is the name of an enumeration type.
Enumeration Constant List – Composed of enumeration constants. "Enumeration constants" or "enumeration members" are integer quantities represented in the form of identifiers, indicating the values that an enumeration type can take. The enumeration constant list lists all the values of the enumeration type, with each enumeration constant separated by a comma and all must be unique. The value type is the same as that in conditional expressions.
Application Example:
enum color_set1 {RED, BLUE, WHITE, BLACK}; // Define enumeration type color_set1
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; // Define enumeration type week
Important Note:
Enumeration constants represent the possible values that a variable of this enumeration type can take. The compiler assigns an integer value to each enumeration constant, by default, this integer is the sequence number of the listed element, starting from 0. You can specify integer values for some or all enumeration constants when defining an enumeration type. Enumeration constants before the specified values still take values in the default manner, while those after the specified values take values according to the principle of incrementing by 1. The values of each enumeration constant can be duplicated. For example:
enum fruit_set {apple, orange, banana=1, peach, grape}
//Enumeration constants apple=0, orange=1, banana=1, peach=2, grape=3.
enum week {Sun=7, Mon=1, Tue, Wed, Thu, Fri, Sat};
//Enumeration constants Sun, Mon, Tue, Wed, Thu, Fri, Sat have values of 7, 1, 2, 3, 4, 5, 6 respectively.
Enumeration constants can only be represented in the form of identifiers and cannot be integer, character, or other literal constants. For example, the following definitions are illegal:
enum letter_set {'a','d','F','s','T'}; //Enumeration constants cannot be character constants
enum year_set{2000,2001,2002,2003,2004,2005}; //Enumeration constants cannot be integer constants
The following form can be used to define it legally:
enum letter_set {a, d, F, s, T};
enum year_set{y2000, y2001, y2002, y2003, y2004, y2005};
Use of Enumeration Variables
The main purpose of defining an enumeration type is to increase the readability of the program. One of the most common and meaningful uses of enumeration types is to describe state quantities, which will be seen in Chapter 9 Input Output Stream Classes.
Definition format: After defining an enumeration type, you can define variables of that enumeration type, such as:
color_set1 color1, color2;
You can also define the type and variable at the same time (even the type name can be omitted), the format is as follows:
enum {Sun,Mon,Tue,Wed,Thu,Fri,Sat} weekday1, weekday2;
Related Operations
The value of an enumeration variable can only be one of the values listed in the enumeration constant list, which is a subset of integers.
The memory occupied by an enumeration variable is the same as that of an integer.
Enumeration variables can only participate in assignment and relational operations as well as output operations, using their own integer values when participating in operations. For example, given the definition:
enum color_set1 {RED, BLUE, WHITE, BLACK} color1, color2;
enum color_set2 { GREEN, RED, YELLOW, WHITE} color3, color4;
The following assignment operations are allowed:
color3=RED; //Assign the enumeration constant value to the enumeration variable
color4=color3; //Assign the value of the same type enumeration variable, the value of color4 is RED
int i=color3; //Assign the enumeration variable to the integer variable, the value of i is 1
int j=GREEN; //Assign the enumeration constant to the integer variable, the value of j is 0
The allowed relational operations are: ==, <, >, <=, >=, !=, etc., for example:
``` //Compare whether the same type enumeration variables color3 and color