5.c++ 函数的份文件编写

  • 函数的文件编写一般有4个步骤:
    • 1.创建后缀名为.h的头文件
    • 2.创建后缀名为.cpp的源文件
    • 3.再头文件中写函数的声明
    • 4.在源文件中写函数的定义

  • 1.创建头文件swap.h
#include <iostream>
using namespace std;

//函数的声明
void swap(int a, int b);

2.创建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.创建main.cpp文件
#include <iostream>
#include "swap.h"	//包含头文件


int main()
{
	int a = 10;
	int b = 20;

	swap(a, b);

	system("pause");
	return 0;

}

posted @ 2025-06-26 23:04  little小新  阅读(6)  评论(0)    收藏  举报