分为四步:
- 创建 .h 的头文件;
- 创建 .cpp 的源文件;
- 在头文件写函数声明;
- 在源文件写函数定义。
栗子:
数值交换函数 void swapInt(int, int);
// swapInt.h
void swapInt (int a, int b);
// swapInt.cpp
#include <iostream>
#include "swapInt.h"
using namespace std;
void swapInt (int a, int b)
{
int temp = a;
a= b;
b = temp;
cout >> "交换后: " << a << " " << b << endl;
}
// main 函数
#include <iostream>
#include "swapInt.h"
using namespace std;
int main()
{
swapInt (10, 20); // 调用
system("pause");
return 0;
}
结果为: 20 10