面向对象编程(OOP)的本质是设计并扩展自己的数据类型。设计自己的数据类型就是让类型与数据匹配。
C++内置数据类型分为基本类型和复合类型。基本类型包括整数、浮点数,以及多种变体;复合类型包括数组、字符串、指针和结构。
变量一种标识存储的数据的方法
算术运算
类型转换
3.1 简单变量
信息存储的3个基本属性: 信息将存储在哪里;存储什么值;存储何种类型的信息;
int braincount; // 整数;变量名(内存单元标记):braincount;// 可通过braincount访问该内存单元;&braincount访问该内存单元地址;
braincount = 5; // 将5复制在内存单元中,
3.1.1 变量名
变量命名规则
- 在名称中只能使用字母字符、数字和下划线(_)。
- 名称的第一个字符不能是数字。
- 区分大写字符与小写字符。
- 不能将C++关键字(C++ key word)用作名称。
- 以两个下划线或下划线和大写字母打头的名称被保留给实现(编译器及其使用的资源)使用。以一个下划线开头的名称被保留给实现,用作全局标识符。
- C++对于名称的长度没有限制,名称中所有的字符都有意义,但有些平台有长度限制。
3.1.2 整型
short 至少16位 或2B
int 至少和short一样长
long 至少32位,至少和int一样长
long long 至少64位,至少和long一样长
3.1.3 无符号整型
unsigned short change; // unsigned short type
unsigned int rovert; // unsigned int type
unsigned quarterback; // also unsigned int,unsigned 本身是 unsigned int 的缩写。
unsigned long gone; // unsigned long type
unsigned long long lang_lang; // unsigned long long type
3.1.4 运算符sizeof
计算机内存的基本单元是位(bit);
字节(byte)指得的8位的内存单元,是描述计算机内存量的度量单位。
sizeof(类型名) e.g. sizeof(int)
sizeof 变量名 e.g. sizeof apple
3.1.5 初始化与溢出
初始化
- 先定义,再初始化
int year;year=2024;
- 定义的同时初始化
int year=2024; // int year(2024); int apple = { 5 };int pie ={ };
- 初始化的传递
int apple =5;
int pie =10;
int applepie = apple +pie; // applepie =15;
溢出
3.1.6 整型字面值
int chest = 42; // decimal integer literal 十进制
int inseam = 042; // octal integer literal 八进制;42的基数是8,它相当于十进制数34
int waist = 0x42; // hexadecimal integer literal 十六进制
整型字面值(常量)是显式地书写的常量,如212或1776。与C相同,C++使用前一(两)位来标识数字常量的基数,以三种不同的计数方式:
基数为10:第一位为1 ~ 9,则基数为10(十进制);
基数为8(老式 UNIX版本):第一位是0,第二位为1~7,则基数为8;
基数为16(硬件黑客的最爱):前两位为0x或0X,则基数为16(十六进制);
按照十进制输出:
cout << "Monsieur cuts a striking figure!\n";
cout << "chest = " << chest " (42 in decimal)\n"; //chest = 42 (42 in decimal)
cout << "waist = " << waist << " (0x42 in hex)\n"; //waist = 66 (0x42 in hex)
cout << "inseam = "<< inseam << " (042 in octal)\n";//inseam = 34 (042 in octal)
按不同进制输出:
cout << "Monsieur cuts a striking figure!" << endl;
cout << " chest = " << chest << "(decimal for 42)" << endl; //chest = 42 (decimal for 42)cout << hex; // manipulator for changing number base
cout << "waist = " << waist << "(hexadecimal for 42) " << endl; //waist = 2a (hexadecimal for 42)cout << oct; // manipulator for changing number base
cout << "inseam = " << inseam << "(octal for 42)" << endl; //inseam = 52 (octal for 42)
3.1.7 C++ 默认存储类型
cout << "key = " << 10086 << endl;
10086 默认存储为int;如果过大存储为long;或者long long;
对于不带后缀的十六进制或八进制整数,将使用下面几种类型中能够存储该数的最小类型来表示:int、unsigned int or long、 unsigned longlong long 或 unsigned long long.
u or U 表示unsigned int
l or L 表示long
ul or UL or uL or ul 表示unsigned long
ull or ULL or uLL or Ull 表示unsigned long long
在将40000表示为long 的计算机系统中,十六进制数0x9C40 (40000)将被表示为unsigned int。unsigned int 比long 更适合表示16位的地址,十六进制通常用来表示内存地址,而内存地址是无符号的。
3.1.8 char
char 用于存储字符(字母或数字)而设计的。
char 类型是另一种整型。它足够长,能够表示目标计算机系统中的所有基本符号一一所有的字母、数字、标点符号等。通常一个字节就可以表示所有的符号。因此,char最常被用来处理字符,也可以将它用做比short更小的整型。
char ch = 'M'; // assign ASCII code for M to ch
int i = ch; // store same code in an int
cout << "The ASCII code for " << ch <<" is" << i << endl; //The ASCII code for M is 77cout << "Add one to the character code:" << endl;
ch = ch + 1; // change character code in ch
i = ch; // save new character code in i
cout << "The ASCII code for " << ch << " is" << i << endl; // using the cout.put() member function to display a char//The ASCII code for N is 78cout << "Displaying char ch using cout.put (ch): ";
cout.put (ch); // using cout.put() to display a char constant
cout.put('!');//Displaying char ch using cout.put (ch): N!
直接输出字符; 输出字符对应的ASCII 值
#include <iostream>int main() {char ch = 'A';std::cout << "字符: " << ch << std::endl; // 输出字符Astd::cout << "ASCII值: " << static_cast<int>(ch) << std::endl; // 输出ASCII值65 使用static_cast<int>(ch)是将字符转换为它的整数值return 0;
}
3.1.9 bool 类型
bool is_ready true;
布尔变量只有true或false;
任何数值都可以被隐式转换为bool值,非零值被抓换为true,零被转换为false;
3.2 const 限定符
const int Month =12; // Month 为符号常量
首字母大写or 整个名称均大写 or 字母k开头,提示为常量
建议创建常量的同时进行初始化;
建议用const 而非#define 定义常量