1class wheel 2{ 3public: 4 wheel(std::string n,int l) 5 :name(n),loadCapacity(l) 6 {} 7 wheel( ) 8 :name(""),loadCapacity(0) 9 {}10void show();11 wheel * Clone();12private:13 std::string name;14int loadCapacity;15};16void wheel::show( )17{18 std::cout<<"The wheel name is "<<name<<std::endl;19 std::cout<<"The wheel loadCapacity is "<<loadCapacity<<std::endl;20}2122 wheel * wheel::Clone()23{24returnnew wheel(name,loadCapacity);25}2627class Car28{29public:30 Car(std::string color,wheel* w,std::string engine)3132 :_color(color),_wheel(w),_engine(engine)33 {}34virtual Car* Clone( )=0;35virtualvoid Show( )=0;36virtualvoid setColor(std::string color )=0;37virtual ~Car( ){}38protected:39 std::string _color;40//有一个资源41 wheel* _wheel;42 std::string _engine;43};4445class BenzCar:public Car46{47public:48 BenzCar(std::string color,wheel *w,std::string engine)49 :Car(color,w,engine)50 { }51 ~BenzCar( );52void Show( );53 Car * Clone( );54void setColor( std::string color );55};5657 BenzCar::~BenzCar( )58{59 delete _wheel;60}61 Car* BenzCar::Clone( )62{63 wheel* l=_wheel.Clone( ); 64 Car * pc=new BenzCar(_color,l, _engine );65return pc;66}67void BenzCar::Show( )68{69 std::cout<<"The car's color is"<<_color<<std::endl;70 _wheel.show( );71 std::cout<<"The car's engine is"<<_engine<<std::endl;72}7374void BenzCar::setColor(std::string color )75{76 _color=color;77}7879int main(int argc,char ** argv)80{81 wheel *l=new wheel("michelin",160);82 BenzCar bc("red",l,"made in China");83 std::auto_ptr<Car> pc(bc.Clone( ));84 pc-set("white");85 pc->Show( );86 bc->Show( );87return0;88 }