官术网_书友最值得收藏!

Enums

Enums are used for enumerating items in a list. When comparing items, it is easier to compare names rather than just numbers. For example, the days in a week are Monday to Sunday. In a program, we will assign Monday to 0, Tuesday to 1, and Sunday to 7, for example. To check whether today is Friday, you will have to count to and arrive at 5. However, wouldn't it be easier to just check if Today == Friday?

For this, we have enumerations, declared as follows:

enum name{ 
value1, 
value2, 
. 
. 
. 
}; 

So, in our example, it would be something like this:

#include <iostream> 
#include <conio.h> 
 
// Program prints out values to screen 
 
enum Weekdays { 
   Monday = 0, 
   Tuesday, 
   Wednesday, 
   Thursday, 
   Friday, 
   Saturday, 
   Sunday, 
}; 
 
int main() 
{ 
 
   Weekdays today; 
 
   today = Friday; 
 
   if (today == Friday) { 
         std::cout << "The weekend is here !!!!" << std::endl; 
   } 
 
   _getch(); 
   return 0; 
} 

The output of this is as follows:

Also note, here, Monday = 0. If we don't use initializers, the first item's value is set to 0. Each following item that does not use an initializer will use the value of the preceding item plus 1 for its value.

主站蜘蛛池模板: 临猗县| 安徽省| 策勒县| 白山市| 岐山县| 涿州市| 健康| 洛南县| 修武县| 建湖县| 石景山区| 开远市| 永修县| 台湾省| 当涂县| 丹凤县| 新疆| 双柏县| 上饶市| 仲巴县| 鄂尔多斯市| 饶平县| 玛纳斯县| 桐庐县| 全南县| 建阳市| 武强县| 大连市| 克什克腾旗| 砀山县| 武乡县| 榆树市| 万年县| 大城县| 仪征市| 亚东县| 泰顺县| 张家港市| 同心县| 随州市| 南陵县|