# c++引用举例

##### 题一
#include <iostream>
using namespace std;
class A{
public:
int x;
int getX() { return x; }
};
void swap(// 在此处补充你的代码)
{
int  tmp = a.x;
a.x = b.x;
b.x = tmp;
}
int main()
{
A a,b;
a.x = 3;
b.x = 5;
swap(a,b);
cout << a.getX() << "," << b.getX();
return 0;
}


【样例输入】无
【样例输出】5，3

#include <iostream>
using namespace std;
class A{
public:
int x;
int getX() { return x; }
};
void swap( A & a, A & b) //考察定义到类型的问题
{
int  tmp = a.x;
a.x = b.x;
b.x = tmp;
}
int main()
{
A a,b;
a.x = 3;
b.x = 5;
swap(a,b);
cout << a.getX() << "," << b.getX();
return 0;
}
##### 题二
#include <iostream>
using namespace std;
void swap(// 在此处补充你的代码)
{
int * tmp = a;
a = b;
b = tmp;
}
int main()
{
int a = 3,b = 5;
int * pa = & a;
int * pb = & b;
swap(pa,pb);
cout << *pa << "," << * pb;
return 0;
}

【样例输入】无
【样例输出】5，3

#include <iostream>
using namespace std;
void swap(int *& a,int *& b)
{
int * tmp = a;
a = b;
b = tmp;
}
int main()
{
int a = 3,b = 5;
int * pa = & a;
int * pb = & b;
swap(pa,pb);
cout << *pa << "," << * pb;
return 0;
}
##### 题三
#include <iostream>
using namespace std;
// 在此处补充你的代码
getElement(int * a, int i)
{
return a[i];
}
int main()
{
int a[] = {1,2,3};
getElement(a,1) = 10;
cout << a[1] ;
return 0;
}

【样例输入】无
【样例输出】10

#include <iostream>
using namespace std;
int & getElement(int * a, int i)
{
return a[i];
}
int main()
{
int a[] = {1,2,3};
getElement(a,1) = 10;
cout << a[1] ;
return 0;
}
11-11