054函数-函数的分文件编写

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.在main函数中调用

#include <iostream>
using namespace std;
#include"swap.h"//包含头文件调用

//1.创建.h的头文件
//2.创建.cpp的源文件
//3.在头文件写函数声明
//4.在源文件写函数定义
int main()
{
    swap(500, 400);
    system("pause");
    return 0;
}

 

posted @ 2021-09-04 14:01  梦之心  阅读(84)  评论(0编辑  收藏  举报