欢迎来到天天文库
浏览记录
ID:2237047
大小:157.50 KB
页数:31页
时间:2017-11-15
《c++编程思想 答案 第十六章 其他章节点击用户名找 thinking in c++ annotated solution guide(charpter 16)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、[ViewingHints][BookHomePage][FreeNewsletter][Seminars][SeminarsonCDROM][Consulting]AnnotatedSolutionGuideRevision1.0forThinkinginC++,2ndedition,Volume1byChuckAllison©2001MindView,Inc.AllRightsReserved.[PreviousChapter][TableofContents]Chapter16(Exercises16-1through16-4arelefttothereader.)1
2、6-1ImplementtheinheritancehierarchyintheOShapediagraminthischapter.16-2ModifytheresultofExercise1fromChapter15tousetheStackanditeratorinTStack2.hinsteadofanarrayofShapepointers.AdddestructorstotheclasshierarchysoyoucanseethattheShapeobjectsaredestroyedwhentheStackgoesoutofscope.16-3ModifyT
3、PStash.hsothattheincrementvalueusedbyinflate()canbechangedthroughoutthelifetimeofaparticularcontainerobject.16-4ModifyTPStash.hsothattheincrementvalueusedbyinflate()automaticallyresizesitselftoreducethenumberoftimesitneedstobecalled.Forexample,eachtimeitiscalleditcoulddoubletheincrementval
4、ueforuseinthenextcall.Demonstratethisfunctionalitybyreportingwheneveraninflate()iscalled,andwritetestcodeinmain().16-5Templatizethefibonacci()functiononthetypeofvaluethatitproduces(soitcanproducelong,float,etc.insteadofjustint).Solution://:S16:fibonacci.cpp#include#include5、t>#include//Forsize_tusingnamespacestd;templateTFib(constT&n){constsize_tsz=100;staticTf[sz];//Initializedtozerostaticsize_tnFilled=2;f[0]=f[1]=1;//Tmustbeconvertiblefromintsize_tint_n=size_t(n);assert(n=nFilled){cout<<"growingto"<6、zeof(T)<<")";f[nFilled]=f[nFilled-1]+f[nFilled-2];nFilled++;}returnf[int_n];}intmain(){for(doublex=8.0;x>=0.0;--x)cout<<"Fib("<7、to8(sizeof(T)==8)growingto8(sizeof(T)==8)Fib(8)==34Fib(7)==21Fib(6)==13Fib(5)==8Fib(4)==5Fib(3)==3Fib(2)==2Fib(1)==1Fib(0)==1*////:~ForthisproblemtomakesensethetypeTneedstobeconvertibletoasize_t,sinceit’susedasanindex.Itmustalsobeconvertiblefromint,sowecanassi
5、t>#include//Forsize_tusingnamespacestd;templateTFib(constT&n){constsize_tsz=100;staticTf[sz];//Initializedtozerostaticsize_tnFilled=2;f[0]=f[1]=1;//Tmustbeconvertiblefromintsize_tint_n=size_t(n);assert(n=nFilled){cout<<"growingto"<6、zeof(T)<<")";f[nFilled]=f[nFilled-1]+f[nFilled-2];nFilled++;}returnf[int_n];}intmain(){for(doublex=8.0;x>=0.0;--x)cout<<"Fib("<7、to8(sizeof(T)==8)growingto8(sizeof(T)==8)Fib(8)==34Fib(7)==21Fib(6)==13Fib(5)==8Fib(4)==5Fib(3)==3Fib(2)==2Fib(1)==1Fib(0)==1*////:~ForthisproblemtomakesensethetypeTneedstobeconvertibletoasize_t,sinceit’susedasanindex.Itmustalsobeconvertiblefromint,sowecanassi
6、zeof(T)<<")";f[nFilled]=f[nFilled-1]+f[nFilled-2];nFilled++;}returnf[int_n];}intmain(){for(doublex=8.0;x>=0.0;--x)cout<<"Fib("<7、to8(sizeof(T)==8)growingto8(sizeof(T)==8)Fib(8)==34Fib(7)==21Fib(6)==13Fib(5)==8Fib(4)==5Fib(3)==3Fib(2)==2Fib(1)==1Fib(0)==1*////:~ForthisproblemtomakesensethetypeTneedstobeconvertibletoasize_t,sinceit’susedasanindex.Itmustalsobeconvertiblefromint,sowecanassi
7、to8(sizeof(T)==8)growingto8(sizeof(T)==8)Fib(8)==34Fib(7)==21Fib(6)==13Fib(5)==8Fib(4)==5Fib(3)==3Fib(2)==2Fib(1)==1Fib(0)==1*////:~ForthisproblemtomakesensethetypeTneedstobeconvertibletoasize_t,sinceit’susedasanindex.Itmustalsobeconvertiblefromint,sowecanassi
此文档下载收益归作者所有