#include<iostream> #include<swap.h> intmain() { int a = 10, b = 20; std::cout << "a = " << a << " b = " << b << std::endl; swap(a, b); std::cout << "a = " << a << " b = " << b << std::endl; std::cout << "Hello World!" << std::endl; return0; }
swap.h的代码如下
1 2 3 4
#ifndef SWAP_H #define SWAP_H voidswap(int &a, int &b); #endif
swap.cpp的代码如下
1 2 3 4 5 6 7
#include"swap.h" voidswap(int &a, int &b) { int temp = a; a = b; b = temp; }