Enumerations in C++. Keyword enum. The advantages of using enumerations. Examples
Contents
- 1. General information about enumerations. Advantages of using enumerations in programs
- 2. The general form of enumeration. Keyword enum. Features of the work of enumeration
- 3. How can the enumeration constants be set in the enumeration? Example
- 4. What types of constant values are allowed to be used in enumerations?
- 5. Can enumerations use negative values of enumerated constants?
- 6. Examples of declaring different types of enumerations with different initializations
- 7. Examples of declaring and using enumerations in programs
- 8. What is the scope of the constant declared in the enumeration?
- 9. Declaration of enumeration using the typedef tool. General form. Example
- Related topics
Search other websites:
1. General information about enumerations. Advantages of using enumerations in programs
The data type “enumeration” is used to create named identifiers that represent some integer constant value.
Enumerations allow you to combine groups of constant values into a coherent whole. An enumeration type is formed based on which a variable can be declared, which takes a value from the set that is described in the enumeration.
When using enumerations in programs, the following interrelated advantages can be distinguished:
- you can create different types of enumerations, each of which contains groups of constants that are formed according to a certain attribute;
- constants in the enumeration are grouped. If the program has large sets of constants, then this facilitates readability and further program maintenance.
⇑
2. The general form of enumeration. Keyword enum. Features of the work of enumeration
To declare an enumeration in the C++ language, the enum keyword is used. The general form of enumeration is as follows:
enum TypeName
{
name1 [=const1],
name2 [=const2],
...
nameN [=constN]
};
here
- TypeName – the name of enumeration type (template name);
- name1, name2, …, nameN – the names of constant identifiers that take the values const1, const2, …, constN, respectively. It is not necessary that constant identifiers take a value. Brackets [ ] mean that a constant value may be missing;
- const1, const2, …, constN – optional values that give values to constants that are recalculated in an enumeration.
If the value of the constant in the enumeration is not specified, then it takes a value that is 1 greater than the previous constant value. For example, if the previous constant had the value 5, then the next would have the value 6. In the case of the char type, the next constant is the next character in the symbol table.
After the declaration of the type (template) of the enumeration, you can declare variables of this type, for example
TypeName varName;
here
- TypeName – the name of the previously declared type (template) of the enumeration;
- varName – the name of variable of type TypeName.
If a variable is declared, then this variable in the program can take the value of the name from the enumeration, for example
varName = name2;
⇑
3. How can the enumeration constants be set in the enumeration? Example
The value of constants of enumeration can be set in one of two ways:
- using explicit initialization. In this case, after the name of the constant, the sign is set to = and the value that this constant takes;
- by default. In this case, the first constant is set to 0, and each subsequent constant takes a value 1 greater.
Example.
// explicit initialization enum DAYS_IN_MONTHS { January = 31, February = 28, March = 31, April = 30, May = 31, June = 30, July = 31, August = 31, September = 30, October = 31, November = 30, December = 31 }; // initialization by defaule enum DIGITS { Zero, // Zero=0 - by default One, // One=1 Two, Three, Four, Five, Six, // Six = 6 Seven, Eight, Nine // Nine = 9 };
⇑
4. What types of constant values are allowed to be used in enumerations?
In enumerations it is allowed to use the constant values of the following types:
- integer types (int, long int, …);
- character type char;
- logical type bool.
It is forbidden to use constant values of types with floating point float, double. When trying to declare a constant value of type double or float
...
enum DBL { x = 2.65 };
...
the compiler will give an error message
Error 1 : constant expression is not integral
⇑
5. Can enumerations use negative values of enumerated constants?
Yes, it can. For example
enum VALUES { val1 = 2, val2 = 'c', val3 = true, val4 = -9 };
A negative constant is used in the VALUES enumeration.
val4 = -9
⇑
6. Examples of declaring different types of enumerations with different initializations
Example 1. An enumeration is declared that implements the months of the year.
// an enumeration that implements constants // that correspond to the months of the year enum MONTHS { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
The first month of the year corresponds to a constant with the name Jan. The value of constant is 1. The next constant is the name Feb, the value of the constant is 2. By the same principle constants are formed, which follow Feb. The value of each subsequent constant is greater by 1.
Example 2. An enumeration ‘Vegetables’ is declared
// an enumeration describing vegetables enum Vegetables { tomato, potato, carrot, cabbage, pepper, onion, radish };
⇑
7. Examples of declaring and using enumerations in programs
Example 1. Using an enumeration in conjunction with the switch statement. Develop a program that, by the value of a constant from the MONTHS enumeration, displays the name of the corresponding month.
#include <iostream> using namespace std; void main(void) { // an enumeration that implements constants that correspond to the months of the year enum MONTHS { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; // variable mn of type enum MONTHS MONTHS mn; // assign the value to the variable mn mn = Mar; // mn = 3 // use mn in switch statement // for mn, display the name of the corresponding month switch (mn) { case Jan: cout << "January" << endl; break; case Feb: cout << "February" << endl; break; case Mar: cout << "March" << endl; break; case Apr: cout << "April" << endl; break; case May: cout << "May" << endl; break; case Jun: cout << "June" << endl; break; case Jul: cout << "July" << endl; break; case Aug: cout << "August" << endl; break; case Sep: cout << "September" << endl; break; case Oct: cout << "October" << endl; break; case Nov: cout << "November" << endl; break; case Dec: cout << "December" << endl; break; } }
If you try to display the value mn, then its integer numeric value will be displayed. For example, in the code
mn = Oct;
cout << mn; // 10
the number 10 will be displayed. The identifier ‘Oct’ will not be displayed.
Example 2. Using enums with the if statement. The implementation of an enumeration that describes the seasons. In the program, by the value of the enumeration type variable, the name of the corresponding season is displayed.
#include <iostream> using namespace std; void main() { enum SEASON { winter, spring, summer, autumn }; int i; SEASON sz; sz = spring; // by sz value, display the name of the corresponding season if (sz == winter) cout << "Winter" << endl; if (sz == spring) cout << "Spring" << endl; if (sz == summer) cout << "Summer" << endl; if (sz == autumn) cout << "Autumn" << endl; }
⇑
8. What is the scope of the constant declared in the enumeration?
A constant declared in an enumeration has a scope within the brackets in which the enumeration template (type) is declared.
For example.
An NUMBERS enumeration is given, in which 3 constants with the names num1, num2, num3 are declared. If in the same block of brackets { } declare a variable with the name num2
... { // the scope num1, num2, num3 enum NUMBERS { num1 = 15, num2 = 77, num3 = -1232 }; // attempt to declare num2 int num2 = 122; // error!!! }
the compiler will generate an error
Error: 'num2' : redefinition; previous definition was 'enumerator'
⇑
9. Declaration of enumeration using the typedef tool. General form. Example
An enumeration can also be declared using the typedef keyword. In this case, the general form of such declaration is as follows.
typedef enum TypeName { // list of constants and constant values // ... };
here TypeName – the name of the new enumeration type that is being created.
Example. With the typedef keyword, the TDAYS type is declared, which is an enumeration. Then a TDAYS variable is declared.
// declare a new type TDAYS, which is an enum type typedef enum TDAYS { Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat }; // declare a variable of type TDAYS TDAYS td; // assign the value of the TDAYS type to the td variable td = TDAYS::Sun;
⇑
Related topics
- Composite data types. The template of structure. Structural variable. Structures in the CLR. Declaring and initializing a structured variable
- Memory allocation for the structure. Nested structures. Arrays of native structures
- Working with managed-structures in CLR. Qualifiers ref and value. Declaring structural variables. Arrays of managed-structured variables. Initializing of managed-structures
- Structures and functions. Passing a structure to a function in the CLR. Returning a structure from a function
- Unions. The keyword ‘union’. Examples of declaring and using unions
⇑