C++ Primer Plus P56 程序清单P3.14(运用数值类型转换运算,采用C++风格的转换)——中职

C++ Primer Plus P56 程序清单P3.14

运用数值类型转换运算,采用C++风格的转换
程序最后一部分演示了如何通过强制类型转换显示char的ASCII码

/*
C++ Primer Plus P56 程序清单P3.14
运用数值类型转换运算,采用C++风格的转换
程序最后一部分演示了如何通过强制类型转换显示char的ASCII码
*/

//头文件
#include<iostream>

//主函数
int main(void)
{
    
    
	using namespace std;																	//编译指令
	int auks, bats, coots;
	char ch = 'Z';

	auks = 19.99 + 11.99;																	//将双精度浮点型赋给整型
	bats = (int)19.99 + (int)11.99;															//C语言风格的写法
	coots = int(19.99) + int(11.99);														//C++风格的写法

	cout << "auks = " << auks << ", bats = " << bats << ", coots = " << coots << endl;		//输出结果

	cout << "The code for " << ch << " is " << int(ch) << endl;								//int(ch)将ch的字符转化为整型(即ASCII码)
	cout << "Yes, the code is ";
	cout << static_cast<int>(ch) << endl;													//新函数将在第15章讲解,同为C++新类型转换的函数

	return 0;
}

注意
C语言风格的写法

	bats = (int)19.99 + (int)11.99;															//C语言风格的写法

C++风格的写法

	coots = int(19.99) + int(11.99);														//C++风格的写法

本程序末尾有个新函数将在第15章讲解,同为C++新类型转换的函数(书讲解,我不讲解qwq)

	cout << static_cast<int>(ch) << endl;													//新函数将在第15章讲解,同为C++新类型转换的函数

第十五章提供了4种新的C++强制类型转换的函数,static_cast是其中的一种,用法差不多,是C++的风格

本程序运行结果为:

auks = 31, bats = 30, coots = 30
The code for Z is 90
Yes, the code is 90

感谢观看

再次感谢~

猜你喜欢

转载自blog.csdn.net/qq_51212951/article/details/113466504
今日推荐