1.首先在头文件创建一个 .h后缀名的文件,里面的代码表示函数的声明,示例如下:
#include<iostream>
using namespace std;
//函数的声明
void swap(int a, int b);
2.然后在源文件里面创建一个 .cpp后缀名的文件,写函数的具体代码,里面的代码如下:
#include "swap.h"
//函数的定义
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
3.最后在源文件创建一个 .cpp后缀名的的文件,该文件里相当于Java中的main函数,调用函数,执行函数功能:
#include<iostream>
using namespace std;
#include "swap.h"
int main() {
int a = 10;
int b= 88;
swap(a, b);
system("pause");
return 0;
}