《C++ primer plus》学习笔记——第三章

面向对象编程的本质是设计并扩展自己的数据类型。
设计自己的数据类型就是让类型与数据匹配。

3.1简单变量
3.1.1变量名

3.1.2整型

3.1.3整型short、int、long和long long

sizeof运算符可以用来检查类型的长度,sizeof运算符返回类型或者变量的长度,单位为字节。

字节的含义依赖于实现,在一个系统中,两字节的int可能是16位,而在另一个系统中可能是32位。其次,头文件climits中包含了关于整型限制的信息,eg:INT_MAX为int的最大取值,CHAR_BIT为字节的位数。

下面是64位的qindows 7系统跑出来的结果

// limits.cpp -- some integer limits
#include <iostream>
#include <climits>              // use limits.h for older systems
int main()
{
    using namespace std;
    int n_int = INT_MAX;        // initialize n_int to max int value
    short n_short = SHRT_MAX;   // symbols defined in climits file
    long n_long = LONG_MAX;
	long long n_llong = LLONG_MAX;

    // sizeof operator yields size of type or of variable
    cout << "int is " << sizeof (int) << " bytes." << endl;
    cout << "short is " << sizeof n_short << " bytes." << endl;
    cout << "long is " << sizeof n_long << " bytes." << endl;
    cout << "long long is " << sizeof n_llong << " bytes." << endl;
    cout << endl;

    cout << "Maximum values:" << endl;
    cout << "int: " << n_int << endl;
    cout << "short: " << n_short << endl;
    cout << "long: " << n_long << endl;
    cout << "long long: " << n_llong << endl << endl;

    cout << "Minimum int value = " << INT_MIN << endl;
    cout << "Bits per byte = " << CHAR_BIT << endl;
	// cin.get();
    return 0;
}

《C++ primer plus》学习笔记——第三章

1.运算符sizeof和头文件limits

可以对类型名或者变量名使用sizeof运算符;

头文件climits定义了符号常量,来表示类型的限制

《C++ primer plus》学习笔记——第三章
《C++ primer plus》学习笔记——第三章

符号常量——预处理器方式

climits文件中包含与下面类似的语句行

#define INT_MAX 32767

在C++编译过过程中,首先将源代码传递给预处理器,在这里,#define和#include一样,是一个预处理器编译指令,该指令告诉预处理器:在程序中查找INT_MAX,并将所有的INT_MAX都替换成32767,因此,#define编译指令的工作方式与文本编辑器或字处理器的全局搜索并替换命令相似。需要指出的是,有些头文件,尤其是被设计成可用于C和C++中的头文件,必须使用#define

C++有一种更好的创建符号常量的方法,使用关键字const

2.初始化

3.C++11初始化方式

除了=,还有大括号初始化器的使用
《C++ primer plus》学习笔记——第三章

3.1.4无符号类型
要创建无符号版本的基本整型,只需要使用关键字unsigned来修改声明即可
《C++ primer plus》学习笔记——第三章
主要关注下面的一张图片,C++符号整型超越限制的时候,出如何?
《C++ primer plus》学习笔记——第三章

3.1.5选择整型类型

《C++ primer plus》学习笔记——第三章

具体的eg如下:
《C++ primer plus》学习笔记——第三章

3.1.6整型字面值

C++使用前一(两)位来标识数字常量的基数;
第一位是1-9,则基数是10(十进制),eg:93是以10为基数;
第一位是0,第二位是1-7,基数是8(八进制),eg:042=34(十进制);
前两位是0x或0X,则基数为16(十六进制),eg:0x42为十六进制数=66(十进制);

eg:cout以十进制格式显示整数

// hexoct1.cpp -- shows hex and octal literals
#include <iostream>

#include<stdlib.h>
int main()
{
    using namespace std;
    int chest = 42;     // decimal integer literal
    int waist = 0x42;   // hexadecimal integer literal
    int inseam = 042;   // octal integer literal

    cout << "Monsieur cuts a striking figure!\n";
    cout << "chest = " << chest << " (42 in decimal)\n";
    cout << "waist = " << waist << " (0x42 in hex)\n";
    cout << "inseam = " << inseam << " (042 in octal)\n";
	// cin.get();

	system("pause");
    return 0; 
}

《C++ primer plus》学习笔记——第三章

eg:cout保持原进制数输出

主要是控制符dec、hex、oct的使用

// hexoct2.cpp -- display values in hex and octal
#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
{
    using namespace std;
    int chest = 42;
    int waist = 42; 
    int inseam = 42;

    cout << "Monsieur cuts a striking figure!"  << endl;
    cout << "chest = " << chest << " (decimal for 42)" << endl;
    cout << hex;      // manipulator for changing number base
    cout << "waist = " << waist << " (hexadecimal for 42)" << endl;
    cout << oct;      // manipulator for changing number base
    cout << "inseam = " << inseam << " (octal for 42)" << endl;
    // cin.get();

	system("pause");
    return 0; 
}

《C++ primer plus》学习笔记——第三章

3.1.7 C++确定常量的类型

例如,整型常量默认存储为int;
首先看后缀,接下来考察长度。

3.1.8 char类型:字符和小整数

char类型是另外一种整型,它足够长,能够表示目标计算机系统中的所有基本符号——所有的字母、数字、标点符号等等

C++对字符用单引号,对字符串使用双引号