C ++ enum type first job

C ++ enumerated type

As the name suggests, as long as the value of the variables that need to list them out, they constitute an enumerated type

Enumeration type definition: refers to the value of the variable to be listed, the value of the variable is limited within a range of values ​​enumerated.

Enumeration type declaration on the part of the form module, or standard module common module, enum defined by the statement.

teaching objectives

Skilled use of enumerated types of knowledge to solve related problems

Teaching process

## declaration form below ##

`enum 枚举类型名{变量值列表}`

Related enumerated type defined with default values 1, 2, 3, ... may be separately specified values in the enumeration element is sequentially declared enum Weekday {SUN=7,MON=1,TUE,WED,THU,FRI,SAT};

For example: Indicates seven days in a week, what type to represent it?
You would certainly say: The plastic ah, but if the user input is 9 it, which is both a valid integer, but not at the same time Monday to seven middle of the day, how to solve it?
Then you need to be solved through labor, then we can not pass data types to solve it?

Definition of an enumerated type, the requirements include the type of all possible values.
For example
enum Weekday {MON,TUE,WED,THU,FRI,SAT,SUN};
the keyword enum + type name Weekday

2 can be enumerated value definitions relational operation, the integer value can not be directly assigned to enumeration, to be cast, to define the value in an enumerated type, enumerated type is always given legal value

Example 2 results in a tournament there are four possible, WIN, LOSE, TIE, CANCEL, programming output these four cases

#include <iostream>
using namespace std;
enum GameResult{WIN ,LOSE ,TIE ,CANCEL};
int main()
{
GameResult result;
enum GameResult omit=CANCEL;
for(int count=WIN; count<=CANCEL;count++)
{
result=GameResult(count);
if(result==omit)
cout<<"The game was cancelled"<<endl;
else
{
cout<<"The game was played";
if(result==WIN)
cout<<"and we won!";
if(result==LOSE)
cout<<"and we lost.";
cout<<endl;
}
}
return 0;

Summary and Reflection

Enum data type to solve the very legitimacy has helped.

Guess you like

Origin www.cnblogs.com/nianshaomingtu/p/11519642.html