1.创建.h后缀名的头文件
swap.h头文件
#include <iostream>
using namespace std;
// 函数声明
void swap(int a, int b);
2.创建.cpp后缀名的源文件
swap.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.在头文件中写函数的声明
4.在源文件中写函数的定义
5.在要使用该函数的文件中引入头文件
#include <iostream>
#include "swap.h"
using namespace std;
int main(void) {
int a = 10;
int b = 20;
swap(a, b);
// 指针所占内存空间
// 在32位操作系统下,指针是占4个字节空间大小,不管是什么数据类型
// 在64位操作系统下,指针是占用8个字节的空间大小
/*cout << sizeof(char*) << endl;*/
system("pasue");
return 0;
}