着手 C++ 的第一个代码
#include <iostream>
using namespace std;
int main(void) {
cout << "hello world" << endl;
return 0;
}
一、第一行 #include 是包含头文件
头文件的名字叫 iostream,以 #include< > 的形式包含。
在 C++ 中要完成输入输出操作会涉及到 iostream 文件中的多个定义,所以就要包含这个头文件。例如:代码中的 cout 是输出流对象,用来完成输出,使用 cout 就要包含头文件 iostream。
iostream 文件中的 io 指的是输入(进入程序的信息)和输出(从程序中输出信息)。
C++ 中的 cin 和 cout 与 C 语言中的 scanf 和 printf 作用相似,但是 cin cout 使用起来更加方便。
除了 iostream 以外,C++ 中还有很多头文件,每个头文件都包含特定的工具。如 cmath 就是数学头文件,跟 C 语言中的 math.h 功能一样。在代码中如果使用了某个头文件中的定义,就得包含对应的头文件,如使用了 sqrt(),那么就要在上方加上 cmath。
#include <iostream>
#include <cmath> // C++ 标准头文件
using namespace std;
int main() {
// 1. 计算 double 类型的平方根
double num1 = 16.0;
double res1 = sqrt(num1);
cout << "√" << num1 << " = " << res1 << endl; // 输出:√16 = 4
// 2. 计算 float 类型的平方根(C++ 重载特性)
num2 = ;
res2 = (num2);
cout << << num2 << << res2 << endl;
num3 = ;
res3 = (<>(num3));
cout << << num3 << << res3 << endl;
num4 = ;
res4 = (num4);
cout << << num4 << << res4 << endl;
;
}

