编写程序以定义虚拟汽车。汽车具有以下特征:车轮数量,最大速度,当前速度,运动,行驶距离。当程序员决定加速时,汽车可以移动。每次加速时,当前速度增加5。每次速度达到10的倍数时,都应在控制台中显示当前速度。
我需要用成员变量和函数编写适当的类。如果适用,还可以更新变量的状态。
它一直引用car1作为未声明的标识符,这是为什么呢?
#include <iostream>
using namespace std;
class car{
private:
int numOfWheels, maxSpeed, currentSpeed, distanceTravelled;
bool isMoving;
public:
car(int _numOfWheels, int _maxSpeed) : numOfWheels(_numOfWheels), maxSpeed(_maxSpeed),
currentSpeed(0),
distanceTravelled(0), isMoving(false){}
int getCurrentSpeed() const{
return currentSpeed;
}
int getDistanceTravelled() const{
return distanceTravelled;
}
bool getlsMoving() const{
return isMoving;
}
void accelerate() {
currentSpeed += 5;// Increase speed by 5 feet per second
distanceTravelled += 5;
isMoving = true;
speedCheck();// Check vehicle speed
}
void speedCheck() const{
if(currentSpeed % 10 == 0)
cout << "Current Speed: " << currentSpeed << endl;
}
};
int main(){
car car1(4,100);
carl.accelerate();
carl.accelerate();
carl.accelerate();
carl.accelerate();
carl.accelerate();
carl.accelerate();
return 0;
}