基本的内置类型
下表列出了七种基本的 C++ 数据类型:
类型 | 关键字 |
---|---|
布尔型 | bool |
字符型 | char |
整型 | int |
浮点型 | float |
双浮点型 | double |
无类型 | void |
宽字符型 | wchar_t |
注:C语言没有 bool 类型
枚举类型
枚举类型 (enumeration) 是 C++ 中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。
如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。
所谓”枚举”是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内。
#include <iostream>
using namespace std;
int main() {
enum color {
red, green, blue // 默认值 { 0 1 2 }
};
color c = green;
cout << c << endl; // 1
enum Day { // 自定义枚举变量的值
Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};
Day d = Thursday;
cout << d << endl; // 4
return 0;
}