#include <iostream>
using namespace std;
class Coordinate{
public:
Coordinate(int x, int y) :X(x), Y(y){}
void setX(int x) { X = x; }
void setY(int y) { Y = y; }
int getX(){
return X;
}
int getY(){
return Y;
}
void printInfo(void){ cout << getX() << ' ' << getY()<<'\n'; }
int Z;
private:
int X;
int Y;
};
int main(void)
{
Coordinate a(2, 3);
const Coordinate b(4, 5);
Coordinate &const c = a; //编译通过,引用的对象为普通对象,并且只能引用一次
c.setX(8); //可通过成员函数修改成员变量
c.getX();
c.Z = 5; //也可直接修改公共成员变量
c.printInfo(); //输出结果为8,3,这说明修改成功。
a.printInfo(); //输出结果为2,3?刚刚不是被c引用并且修改内容了吗?
c = b; //编译通过,引用还能换对象?
system("pause");
return 0;
}
a.printInfo(); //输出结果为2,3?刚刚不是被c引用并且修改内容了吗?