1.C++ 11将使用大括号的初始化称为列表初始化,因为这种初始化常用于给复杂的数据类型提供值列表。列表初始化不允许缩窄,即变量的类型可能无法表示赋给他们的值。例如,不允许将浮点型转化为整型。在不同的整型之间转换或将整型转换为浮点型可能被允许,条件是编译器知道目标变量能够正确存储赋给它的值。
比如说下面的这个程序:
#include
using namespace std ;
int main()
{
const int code = 66 ;
int x = 66 ;
char c1 {31325} ;
char c2 = {66} ;
char c3 {code} ;
char c4 = {x} ;
x = 31325 ;
char c5 = x ;
cout << "code = " << code << endl ;
cout << "x = " << x << endl ;
cout << "c1 = " << c1 << endl ;
cout << "c2 = " << c2 << endl ;
cout << "c3 = " << c3 << endl ;
cout << "c4 = " << c4 << endl ;
cout << "c5 = " << c5 << endl ;
return 0 ;
}
编译结果:
narrow.cpp: In function ‘int main()’:
narrow.cpp:8:16: error: narrowing conversion of ‘31325’ from ‘int’ to ‘char’ [-Wnarrowing]
8 | char c1 {31325} ;
| ^
narrow.cpp:11:13: warning: narrowing conversion of ‘x’ from ‘int’ to ‘char’ [-Wnarrowing]
11 | char c4 = {x} ;
| ^
注意: 初始化c4时,您知道x的值为66,但在编译器看来,x是一个变量,其值可能很大。编译器不会跟踪下述阶段可能发生的情况: 从x被初始化到它被用来初始化c4。
2.自动转换: 在计算表达式时,C++将bool、char、unsigned char、signed char和short值转化为int。具体来说,true被转化为1,false被转化为0。这些转换被称为整体提升。
3.通常将int类型选择为计算机最自然的类型,这意味着计算机使用int类型时,运算速度可能最快。
4.使用下面的方式进行强制类型转换时:
typeName (value) ;
value必须是一个值,而不是变量,否则就会导致类似于下面的后果:
change.cpp: In function ‘int main()’:
change.cpp:9:7: error: conflicting declaration ‘long int thorn’
9 | long(thorn) ;
| ^~~~~
change.cpp:6:6: note: previous declaration as ‘int thorn’
6 | int thorn = 987 ;
| ^~~~~
第三章编程题:
(1)
#include
using namespace std ;
int main()
{
double height ;
cout << "Please report your height in inchs: " << "________\b\b\b\b\b\b\b\b";
cin >> height ;
const double change = 12 ;
cout << "Your height is " << height << " in. " << endl;
cout << "Your height is " << height/change << " ft. " << endl;
return 0 ;
}
(2)
#include
#include
using namespace std ;
int main()
{
double feet, inch, pounds ;
cout << "Report your height like ...ft...in: " << endl;
cin >> feet ;
cin >> inch ;
cout << "Report your weight in pounds: " ;
cin >> pounds ;
const double FT_IN = 12 ;
const double IN_METER = 0.0254 ;
const double KG_LBS = 2.2 ;
cout << "Your height = " << feet*FT_IN+inch << " in. " << endl ;
cout << "Your BMI = " << pounds/KG_LBS/pow((feet*FT_IN+inch)*IN_METER, 2) << endl ;
}
(3)
#include
using namespace std ;
int main()
{
double degrees, minutes, seconds ;
cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
cout << "First, enter the degrees: " ;
cin >> degrees ;
cout << "Next, enter the minutes of arc: " ;
cin >> minutes ;
cout << "Finally, enter the seconds of arc: " ;
cin >> seconds ;
const double change = 60 ;
cout << degrees << " degrees " << minutes << " minutes " << seconds << " seconds " ;
cout << " = " << ( seconds/change + minutes ) / change + degrees << " degrees " << endl ;
return 0 ;
}
(4)
#include
using namespace std ;
int main()
{
long seconds ;
cout << "Enter the number of seconds: " ;
cin >> seconds ;
const int DAY_HOUR = 24 ;
const int HOUR_MINUTE = 60 ;
const int MINUTE_SECOND = 60 ;
cout << seconds << " seconds = " << seconds/MINUTE_SECOND/HOUR_MINUTE/DAY_HOUR << " days, " ;
cout << seconds/MINUTE_SECOND/HOUR_MINUTE%DAY_HOUR<< " hours, " ;
cout << seconds/MINUTE_SECOND%HOUR_MINUTE << " minutes, " ;
cout << seconds%MINUTE_SECOND << " seconds" ;
cout << endl ;
return 0 ;
}
可能在取余运算方面,C++不会自动进行类型转换,所以出现了这样的错误:
error: invalid operands of types ‘long int’ and ‘const double’ to binary ‘operator%’
17 | cout << seconds%MINUTE_SECOND << " seconds" ;
| ~~~~~~~^~~~~~~~~~~~~~
| | |
| | const double
| long int
(5)
#include
using namespace std ;
int main()
{
long long world, us ;
cout << "Enter the world's population: " ;
cin >> world ;
cout << "Enter the population of the US: " ;
cin >> us ;
cout << "The population of the US is " ;
cout << ((double)us)/((double)world)*(double(100)) ;
cout << "% of the world population." << endl ;
return 0 ;
}
(6)
#include
using namespace std ;
int main()
{
double length, gasoline ;
cout << "Enter the journey your car have gone in miles: " ;
cin >> length ;
cout << "Enter the gasoline your car have spent in gallons: " ;
cin >> gasoline ;
cout << "1 Gal can drive your car to go " ;
cout << length/gasoline << " m." << endl ;
cout << "Enter the journey your car have gone in kilometers: " ;
cin >> length ;
cout << "Enter the gasoline your car have spent in litres: " ;
cin >> gasoline ;
cout << "100 kilometers can force your car to spend " ;
cout << gasoline/(length/double(100)) << " litres gasoline" << endl ;
return 0 ;
}
(7)
#include
using namespace std ;
int main()
{
double europ ;
cout << "100 kilometers spent gasoline: " ;
cin >> europ ;
cout << "This is equal to " << 62.14/(europ/3.875) << " mpg." << endl ;
return 0 ;
}