博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C++中函数的分文件编写

Posted on 2025-09-27 16:40  steve.z  阅读(15)  评论(0)    收藏  举报

1、创建 .h 头文件

2、创建 .cpp 源文件

3、在 .h 头文件中,编写函数声明

4、在 .cpp 源文件中,编写函数定义

test.h

#include <iostream>
using namespace std;
void swap_my(int a, int b);

test.cpp


#include "test.h"
void swap_my(int a, int b) {
    cout << a << "----" << b << endl;
}

main.cpp

#include <iostream>
#include "test.h"

int main() {
    int x = 10, y = 20;
    swap_my(x, y);
    return 0;
}