C++枚舉定義

一、

#includeusing namespace std;

int main()

{

enum times

{

first,second,

third,forth,fifth

}time;

? ? time =fifth;

? ? if (time==fifth)

? ? {

? ? ? ? cout << "Succeed!";

? ? }

? ? return 0;

}

記錄1:定義了一個枚舉數據類型 :times ,同時定義了times類型的一個變量:time;

所以需要這樣給time這個枚舉類型的變量賦值:?time =fifth; 而且只能times枚舉類型定義的元素。

記錄2:錯誤的定義方法:time a=fifth; ?time已經是枚舉類型times定義的一個變量,不能想當然的再用time去定義一個枚舉類型a。

二、

#includeusing namespace std;

int main()

{

enum times

{

first,second,

third,forth,fifth

};

? ? enum times time =fifth;

? ? if (time==fifth)

? ? {

? ? ? ? cout << "Succeed!";

? ? }

? ? return 0;

}

記錄1:如果定義枚舉類型times的時候沒有定義變量,那么就要這樣:enum times time =fifth; 定義枚舉類型times的變量time;

三、

#includeusing namespace std;

int main()

{

????enum times

????????{

????????????????first,second,

????????????????third,forth,fifth

????????}day;

? ? enum times time =fifth;

? ? day = second;

? ? if (day==second)

? ? {

? ? ? ? cout << "Succeed!";

? ? }

? ? return 0;

}

記錄1:變量day,已經是枚舉類型tiems類型的變量,所以直接賦值即可;也可以用enum times再定義新的times枚舉類型變量。

四、

#includeusing namespace std;

enum time

{

? ? first,second,

? ? third,forth,fifth

};

int main()

{

? ? enum time a=fifth;

? ? if (a==fifth)

? ? {

? ? ? ? cout << "Succeed!";

? ? }

? ? return 0;

}

記錄1:枚舉類型的定義,可以在main函數之外,也不在任何函數體里面。

完畢。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容