自考新教材--p41

源程序:

#include <iostream>

using namespace std;

void SwapValue(int a, int b)

{

int tmp;

tmp = a;

a = b;

b = tmp;

cout << "在SwapValue()函数中:\t\ta=" << a << ",b=" << b << endl;

return;

}

void SwapRef(int &a, int &b)

{

int tmp;

tmp = a;

a = b;

b = tmp;

cout << "在SwapRef()函数中:\t\ta=" << a << ",b=" << b << endl;

return;

}

int main()

{

int a = 10, b = 20;

cout << "数据交换前:\t\ta=" << a << ",b=" << b << endl << endl;

SwapValue(a,b);

cout << "调用SwapValue()后:\t\ta=" << a << ",b=" << b << endl << endl;

a = 10;

b = 20;

SwapRef(a,b);

cout << "调用SwapRef()后:\t\ta=" << a << ",b=" << b << endl;

system("pause");

return 0;

}

运行结果:

posted @ 2019-12-02 11:38  bobo哥  阅读(249)  评论(0编辑  收藏  举报