English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn to use enumerations (enum). In addition, you will learn C ++Places where enumerations are commonly used in programming.
Enumerations are user-defined data types composed of integer constants. You can use the keywordenumto define enumeration.
enum season { spring, summer, autumn, winter };
Here, the name of the enumeration is season.
And spring, summer, and winter are the values of the season type.
By default, spring is 0, summer is1and so on. You can change the default value of enumeration elements during declaration if necessary.
enum season { spring = 0, summer = 4, autumn = 8, winter = 12 };
When creating an enumeration type, only the blueprint of the variable is created. This is the method to create an enumeration type variable.
enum boolean { false, true }; // Internal function enum boolean check;
Here, aenum booleanThe variable check of type.
This is another way to declare the same check variable using different syntax.
enum boolean { false, true }
#include <iostream> using namespace std; enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; int main() { week today; today = Tuesday; cout << "What day of the week?" << today+1; return 0; }
Output Result
What day of the week?3
#include <iostream> using namespace std; enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32}; int main() { seasons s; s = summer; cout << "Summer = " << s << endl; return 0; }
Output Result
Summer = 4
Enumeration variables take only one of many possible values. The following example demonstrates it:
#include <iostream> using namespace std; enum suit { club = 0, diamonds = 10, hearts = 20, spades = 3 } card; int main() { card = club; cout << "enum variable size " << sizeof(card) << " bytes."; return 0; }
Output Result
enum variable size 4 bytes.
because the size of an integer is4bytes.
This makes enum a good choice for using flags.
You can useC ++structureto complete the same task. However, using enumeration can improve efficiency and flexibility.
Let's take an example,
enum designFlags { ITALICS = 1, BOLD = 2, UNDERLINE = 4 } button;
Suppose you are designing a button for a Windows application. You can set the flags ITALICS, BOLD, and UNDERLINE to handle text.
In the above pseudocode, all integral constants are2is for a reason.
// In binary ITALICS = 00000001 BOLD = 00000010 UNDERLINE = 00000100
because the integral constant is2so you can combine two or more flags at the same time without using the bitwise OR | operator for overlap. This allows you to select two or more flags simultaneously. For example,
#include <iostream> using namespace std; enum designFlags { BOLD = 1, ITALICS = 2, UNDERLINE = 4 }; int main() { int myDesign = BOLD | UNDERLINE; // 00000001 // | 00000100 // ___________ // 00000101 cout << myDesign; return 0; }
Output Result
5
When the output is5when, you always know that bold (bold) and (underline) underlines will be used.
Additionally, you can add flags to the requirements.
if (myDesign & ITALICS) { // italic code }
Here, we added italics in the design. Note that only code written in italicsin if statements.
You can use enums in C without ++In programming, we can complete almost all tasks. However, they may be very convenient in some cases. This may also be the difference between a good programmer and a great programmer.