【发布时间】:2019-05-11 08:51:09
【问题描述】:
我有一个Aircraft 课程。我想循环更新飞机对象的属性(每隔一秒)。
如何在不创建新对象的情况下更新这些属性?我应该使用指针吗?我的目标:每秒钟更新一次飞机信息
我的代码:
class Aircraft
{
public:
unsigned int altitude, speed, direction;
Aircraft(unsigned int aAltitude, unsigned int aSpeed, unsigned int aDirection)
{
altitude = aAltitude;
speed = aSpeed;
direction = aDirection;
}
};
int main()
{
//aircraft's initial values
Aircraft myAircraft(0, 10, 345);
//Initial values should are printed
cout << myAircraft.altitude + "," + myAircraft.speed + "," + myAircraft.direction << endl; //print initial values
//In this loop new values for altitude, speed and direction should be assigned
for (int i = 0; i < 10; i++)
{
//aircraft's new values
Aircraft myAircraft(new altitude, new speed, new direction);
//print updated attributes
cout << myAircraft.altitude + "," +
myAircraft.speed "," myAircraft.direction
<< endl << endl; //print new values
}
}
结果应如下所示:(值无关紧要)
0, 10, 345
0, 30, 345
0, 60, 345
0, 100, 345
0, 150, 345
300, 180, 345
700, 220, 345
2000, 250, 345
【问题讨论】:
标签: c++ algorithm class member