资源描述:
《C++编程思想 答案 第十四章 其他章节点击用户名找 thinking in C++ annotated solution guide(charpter 14).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、[ViewingHints][BookHomePage][FreeNewsletter][Seminars][SeminarsonCDROM][Consulting]AnnotatedSolutionGuideRevision1.0forThinkinginC++,2ndedition,Volume1byChuckAllison©2001MindView,Inc.AllRightsReserved.[PreviousChapter][TableofContents][NextChapter]Chapter1414-1ModifyC
2、ar.cppsothatitalsoinheritsfromaclasscalledVehicle,placingappropriatememberfunctionsinVehicle(thatis,makeupsomememberfunctions).AddanondefaultconstructortoVehicle,whichyoumustcallinsideCar’sconstructor.14-2Createtwoclasses,AandB,withdefaultconstructorsthatannouncethems
3、elves.InheritanewclasscalledCfromA,andcreateamemberobjectofBinC,butdonotcreateaconstructorforC.CreateanobjectofclassCandobservetheresults.14-3Createtwoclasses,AandB,withdefaultconstructorsthatannouncethemselves.InheritanewclasscalledCfromA,andcreateamemberobjectofBinC
4、,butdonotcreateaconstructorforC.CreateanobjectofclassCandobservetheresults.Solution://:S14:DefaultConstruction.cpp#includeusingnamespacestd;classA{public:A(){cout<<"A::A()";}};classB{public:B(){cout<<"B::B()";}};classC:publicA{Bb;};intmain(){Cc;}/*Output
5、:A::A()B::B()*////:~The“parts”ofaCobjectareautomaticallyconstructed,inheritedsub-objectsfirst,followedbycontainedobjects.14-4Createathree-levelhierarchyofclasseswithdefaultconstructors,alongwithdestructors,bothofwhichannouncethemselvestocout.Verifythatforanobjectofthe
6、mostderivedtype,allthreeconstructorsanddestructorsareautomaticallycalled.Explaintheorderinwhichthecallsaremade.Solution://:S14:HierarchyConstruction.cpp#includeusingnamespacestd;classA{public:A(){cout<<"A::A()";}~A(){cout<<"A::~A()";}};classB:publicA{pub
7、lic:B(){cout<<"B::B()";}~B(){cout<<"B::~B()";}};classC:publicB{public:C(){cout<<"C::C()";}~C(){cout<<"C::~C()";}};intmain(){Cc;}/*Output:A::A()B::B()C::C()C::~C()B::~B()A::~A()*////:~Anobject’s“parts”arealwaysconstructedbeforetheobjectitself.Applyingthisprinci
8、plerecursivelyrequiresthatthesub-objectatthetopofthehierarchymustbecreatedfirst.Destructionisalwaysthereverseorderofconstruc