- 程序来源:C++ primer plus
- 程序名称:typecast.cpp
- 章 节:3.14
- 作 者:tiaya@qq.com
- 运行测试:通过
/********************************
* 程序来源:C++ primer plus
* 程序名称:typecast.cpp
* 章 节:3.14
* 作 者:tiaya@qq.com
* 运行测试:通过
*******************************/
//#include <bits/stdc++.h> //万能头文件,不建议使用
#include <iostream>
int main() {
using namespace std;
int auks, bats, coots;
auks = 19.99 + 11.99;
/***************************************
* 1、c 和 C++语法的类型转换
***************************************/
bats = (int)19.969 + (int)11.99; //c syntax
coots = int(19.99) + int(11.99); //c++ syntax\
cout << "auks=" << auks << ",bats=" << bats;
cout << ",coots=" << coots <<endl;
char ch = 'Z';
cout << "The code for "<< ch <<" is ";
cout << int(ch) << endl;
cout << "Yes, the code is ";
/***************************************
* 2、C++ static_cast的类型转换
***************************************/
cout << static_cast<int>(ch) << endl; //using static_cast
return 0;
}
输出结果:
auks=31,bats=30,coots=30
The code for Z is 90
Yes, the code is 90
--------------------------------
Process exited after 0.9626 seconds with return value 0
请按任意键继续. . .
网友评论