使用引用得到类中数据成员的值

#include 

using namespace std;

class Test
{
private:
	int x, y;

public:
	//设置x的值
	void setX(int a)
	{
		x = a;
	}

	//设置y的值
	void setY(int b)
	{
		y = b;
	}

	//使用引用取得x,y的值
	void getXY(int &px, int &py)
	{
		px = x;
		py = y;
	}
};

void main()
{
	Test p1;

	p1.setX(3);//设置x的值
	p1.setY(5);//设置y的值

	int a,b;

	//取得x,y的值
	p1.getXY(a,b);

	cout<<"x="<


执行结果:

使用引用得到类中数据成员的值_第1张图片



你可能感兴趣的:(C++语言)