闭门造车
1.自定义车车对象
2.修复相关bug
#include <iostream>
#include <windows.h>
using namespace std;
#define FULL_GAS 100
class Car
{
public:
string color;
string engine;
unsigned int gas_tank;
unsigned int wheel;
void setColor(string col);
void setEngine(string eng);
void setWheel(unsigned int whe);
void fill_tank(int liter);
int running(void);
void warning(void);
};
void Car::setColor(string col)
{
color=col;
}
void Car::setEngine(string eng)
{
engine=eng;
}
void Car::setWheel(unsigned int whe)
{
wheel=whe;
}
void Car::fill_tank(int liter)
{
gas_tank=FULL_GAS;
}
int Car::running(void)//返回车当前油量
{
cout<<"嘟嘟嘟,小汽车正在前进"<<endl;
gas_tank=gas_tank-1;
cout<<"还剩"<<gas_tank<<"的油哦~"<<endl;
return gas_tank;
}
void Car::warning(void)
{
cout<<"注意!!" <<"还剩"<<gas_tank<<"的油!"<<endl;
}
int main()
{
int number;//中间变量
int flag=1;
char i;
Car mycar, car1;
mycar.setColor("WHITE");
mycar.setEngine("V8");
mycar.setWheel(4);
mycar.gas_tank = FULL_GAS;//汽车参数设置
while(flag!=0)//判断当前是否有油剩余
{
number=mycar.running();
if(number<=10)
{
mycar.warning();//低油量触发警告
cout<<"要加满油吗?[Y/N]"<<endl;
cin>>i;
if('Y'==i||'y'==i)
{
mycar.fill_tank(FULL_GAS);
}
}
if(number<=0)
{
cout<<"没油了,溜了溜了"<<endl;
flag==0;
break;
}
}
return 0;
}