C++引用私有成员变量

#include<iostream>
using namespace std;
class point
{
private:
	int x;
	int y;
public:
	int getx()//如何引用私有类的成员
	{
		return x;
	}
	void setx(int xx)
	{
		 x = xx;
	}
	int gety()//如何引用私有类的成员
	{
		return y;
	}
	void sety(int yy)
	{
		y = yy;
	}
	void input()
	{
		cin >> x >> y;
	}
	void output()
	{
		cout << "(" << x << "," << y << ")" << endl;
	}
};
class line
{
private:
	point p1, p2;
public:
	void input()
	{
		p1.input();
		p2.input();
	}
	void output()
	{
		p1.output();
		p2.output();
	}
};
int main()
{
	point p;
	p.setx(1);//可以用封装的方法
	p.sety(2);
	p.output();
	point o;
	o.input();//也可以用直接赋值的方法
	o.output();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43312665/article/details/88201273