设计两个类 汽车类 属性:颜色,品牌,车轮个数 行为:行驶
修车厂类 属性:名称,地址 行为:修车
描述逻辑:汽车可以行驶,一旦汽车损害(轮胎爆胎,某个轮子不可用),可送至修理厂维修,修好后可正常行驶
#include<iostream>
#include<string>
using namespace std;
class Car {
string color;
string brand;
int wheelNum;
public:
Car(string c,string b,int n):color(c),brand(b),wheelNum(n){}
void drive(){
cout<<"The "<<color<<" "<<brand<<" car is driving."<<endl;
}
void breakdown(int num){
cout<<"The "<<num<<"th wheel of the "<<color<<" "<<brand<<" car is broken."<<endl;
this->wheelNum--;
}
bool isBroken(){
return (this->wheelNum<=0);
}
};
class RepairShop{
string name;
string address;
public:
RepairShop(string n,string a):name(n),address(a){}
void repair(Car& car){
cout<<"Repairing the "<<car.color<<" "<<car.brand<<" car..."<<endl;
//repair process
car.drive();
}
};
int main(){
Car myCar("red","BMW",4);
myCar.drive();
myCar.breakdown(2);
if(myCar.isBroken()){
RepairShop myRepairShop("AutoRepair","5th Ave, NY");
myRepairShop.repair(myCar);
}
return 0;
}