C++与引用2

*/
 * Copyright (c) 2016,烟台大学计算机与控制工程学院
 * All rights reserved.
 * 文件名:text.cpp
 * 作者:常轩
 * 微信公众号:Worldhello
 * 完成日期:2016年5月29日
 * 版本号:V1.0
 * 问题描述:引用传递
 * 程序输入:无
 * 程序输出:见运行结果
 */
#include<iostream>
using namespace std;
void swap(int&,int&);
int main()
{
	int a=5,b=10;
	cout<<"Before swaping"<<endl;
	cout<<"a="<<a<<",b="<<b<<endl;
	swap(a,b);
    cout<<"After swaping"<<endl;
	cout<<"a="<<a<<",b="<<b<<endl;
	return 0;
}
void swap(int&m,int&n)
{
   int temp=m;
   m=n;
   n=temp;
}


运行结果:


#include<iostream>
using namespace std;
int maxab;
int & maxRef(int x,int y)            //函数的返回类型是引用
{
	if(x>y)
		maxab=x;
	else
		maxab=y;
	return maxab;
}

int main()
{
    int a,b;
	cout<<"Input a and b:";
	cin>>a>>b;
	cout<<maxRef(a,b)<<endl;
	return 0;
}

运行结果:



posted @ 2016-05-29 22:17  壹言  阅读(105)  评论(0编辑  收藏  举报