- 作用:让代码结构更加清晰
- 函数份文件编写一般有4个步骤
- 创建后缀名为.h的头文件
- 创建后缀名为.cpp的源文件
- 在头文件中写函数的声明
- 在源文件中写函数的定义 ```cpp
// main.cpp // Created by zhoukai on 2022/1/10. //
include
include “swap.h”
using namespace std; // 函数定义 void swap(int a, int b) { int temp = a; a = b; b = temp; cout << a << endl; cout << b << endl; }
```cpp// swap.h//// Created by zhoukai on 2022/1/10.//#ifndef DEMO02_SWAP_H#define DEMO02_SWAP_H// 函数声明void swap(int a, int b);#endif //DEMO02_SWAP_H
// swap.cpp//// Created by zhoukai on 2022/1/10.//#include <iostream>#include "swap.h"using namespace std;// 函数定义void swap(int a, int b) {int temp = a;a = b;b = temp;cout << a << endl;cout << b << endl;}
