c++基础篇(一)——输入输出

前言:
   想起当年在图书馆自学c++,奈何毕业七年了,工作中并不需要使用c++。最近想搞几个开源项目,甚至将来做独立游戏开发,遂将书本拾起,重温旧梦。
   博文将假定阅读者已有一定c基础,在此基础上学习c++。

1 第一个c++程序

  用经典的 Hello World! 输出开局。

1
2
3
4
5
6
7
8
#include <iostream>

int main(void)
{
std::cout << "Hello World!" << std::endl;

return 0;
}

  输出结果为。

1
Hello World!

   cout 是c++的打印输出,std:: 表示其所处的命名空间, endl 表示输出换行。

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main(void)
{
int number = 0;

std::cout << "Please input a number" << std::endl;
std::cin >> number;
std::cout << "The number is " << number << std::endl;

return 0;
}

   cin 是c++的输入,这里等待用户输如一串数字,然后将数字原样输出。程序运行结果如下。

1
2
3
Please input a number
100
The number is 100

  如果习惯使用c的 printf ,c++也同样支持。