2023年8月24日,周四凌晨
#include<iostream>class CarType{
public:virtual std::string getType()=0;
};class MiniCar:public CarType{
public:std::string getType() override{return "小型车";};
};class MidSizeCar:public CarType{
public:std::string getType() override{return "中型车";};
};class HeavyCar:public CarType{
public:std::string getType() override{return "重型车";};
};class CarTypeFactory{
public:CarType* createCarType(int weight){if(weight<5){return new MiniCar();}else if(weight<10){return new MidSizeCar();}else{return new HeavyCar();}return nullptr;}
};int main(){int weight;CarType *carType;CarTypeFactory *factory=new CarTypeFactory();while(1){std::cout<<"请输入汽车的重量(吨):";std::cin>>weight;carType=factory->createCarType(weight);std::cout<<carType->getType()<<std::endl;}}