1. 每个枚举元素在声明时被分配一个整型值,默认从 0 开始,逐个加 1。
#include<iostream>
usingnamespace std;
int main()
{
enumWeekend{Zero,One,Two,Three,Four};
int a,b,c,d,e;
a=Zero;
b=One;
c=Two;
d=Three;
e=Four;
cout<<a<<","<<b<<","<<c<<","<<d<<","<<e<<endl;
return0;
}
2. 也可以在定义枚举类型时对枚举元素赋值,此时,赋值的枚举值为所赋的值,而其他没有赋值的枚举值在为前一个枚举值加 1。
#include<iostream>
usingnamespace std;
int main()
{
enumWeekend{Zero,One,Two=555,Three,Four};
int a,b,c,d,e;
a=Zero;
b=One;
c=Two;
d=Three;
e=Four;
cout<<a<<","<<b<<","<<c<<","<<d<<","<<e<<endl;
return0;
}