本节大意
对上节的程序进行了扩展,增加了头文件和输入输出功能。使例子程序像真正的C++程序靠拢。
细节摘录
1. C++用“流”的思想处理输入输出,“流”是指要从某种设备上读出或写入的字符序列。
2. 可以连续使用<<或>>的理论依据 -- 输入输出操作的返回是流本身。
3. endl叫做操纵符,除了可以换行,还有刷新缓冲区的功能。
4. 定义变量前一定要初始化。
课后习题
1. 仿照课本例子,代码:
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
运行结果:
2. 仿照示例程序,代码如下:
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The Result of " << v1 << " * " << v2
<< " is " << v1 * v2 << std::endl;
return 0;
}
运行结果:
3. 按照题目要求再次改写程序代码,结果如下:
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The Result of " << v1 << " * " << v2;
std::cout << " is " << v1 * v2 << std::endl;
return 0;
}
执行结果同2.
4. 不合法,因为第二行及后面几行的<<没有左操作数,编译不可能通过。