Enum overview
Enum enum type is a type of data in C/s + +, which is a user-defined type like struct and class, which is characterized by the fact that the value of the enum type variable is limited and can be enumerated.
Enum definition
C + + Enum enumeration format:
Enum < enum type name > {< enumeration Table >};
Enum {< Enumeration table >}< variable Name table >;
The first C + + Enum enumeration value corresponds to an integer number, usually if the enumeration constant does not define a numeric value, then the first enumeration value corresponds to the constant value 0, and then increments, and if the first enumeration constant defines a value, then the value is incremented, with each constant separated by "," instead of ";", the last value does not use a symbol.
enum BOOL {false,true}; The bool type is a pre-defined enumeration of C + +
enum Example
- #include < iostream.h>
- void Main ()
- ... {
- Example 1
- Enum Day ... {sun,mon,tue,wed,thu,fri,sat};//defines an enumeration of day
- Day d = Sun;
D can only be equal to one of "Sun,mon,tue,wed,thu,fri,sat", no other value!
- cout< < "value is"< < D< < Endl;
- You can also define multiple variables
- Enum Day ... {Sun,mon,tue,wed,thu,fri,sat};
- Day d1,d2,d3;
- D1 = Thu; d2 = Sat; d3 = Tue;
- cout < < "d1 && d2 && d3 " < < d1 < < " " < < d2< < " " < < d3< < Endl ;
- Example 2
- Enum... {Sun,mon,tue,wed,thu,fri,sat} D;
- D = Mon;
- cout< < "value is"< < D< < Endl;
- Example 3
- Enum... {sun=10,mon,tue,wed,thu,fri,sat} d1,d2,d3,d4;
- D1 = Sun; d2 = Mon; d3 = Tue; d4 = Wed;
- cout<<D1< < "< < D2< < " "< < D3< < ""< < D4< < Endl;
Output ten
- Example 4
- Enum... {sun=,mon=1,tue,wed,thu,fri,sat} d1,d2,d3,d4;
- D1 = Sun; d2 = Mon; d3 = Tue; d4 = Wed;
- cout<<D1< < "< < D2< < " "< < D3< < ""< < D4< < Endl;
Output 1 2 3
- }
How enum is used in C + + (GO)