C++自学第三十七篇

/*
* 函数的分文件编写
* 步骤:
* 1、创建后缀名为.h的头文件
* 2、创建后缀名为.cpp的源文件
* 3、在头文件中写函数的声明
* 4、在原文件中写函数的定义
*/
#include<iostream>
#include "swap.h"  //用""表示我们自己写的头文件
using namespace std;

int main()
{
	int a = 10;
	int b = 20;
	swap(a,b);
	system("pause");
	return 0;
}
//swap.h文件
#include<iostream>
using namespace std;

//实现两个数字交换的函数声明
void swap(int a, int b);
#include "swap.h"//函数定义需要包括头文件

//函数的定义
void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_47358937/article/details/121545826