C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx

上传人:b****5 文档编号:8443365 上传时间:2023-05-11 格式:DOCX 页数:8 大小:38.31KB
下载 相关 举报
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第1页
第1页 / 共8页
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第2页
第2页 / 共8页
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第3页
第3页 / 共8页
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第4页
第4页 / 共8页
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第5页
第5页 / 共8页
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第6页
第6页 / 共8页
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第7页
第7页 / 共8页
C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx

《C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx(8页珍藏版)》请在冰点文库上搜索。

C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx

Text.cpp

#include<

iostream>

fstream>

string>

usingnamespacestd;

classText{

stringtext;

public:

Text(){}

Text(conststring&

fname){

ifstreamifs(fname.c_str());

stringline;

while(getline(ifs,line))

text+=line+'

\n'

;

}

stringcontents(){

returntext;

};

intmain(intargc,char*argv[]){

if(argc>

1)

{

Textt1;

Textt2(argv[1]);

cout<

<

"

t1:

\n"

<

t1.contents()<

endl;

t2:

t2.contents()<

}

///:

~

WhencreatingaTextobject,thecompilerguaranteesthatthetextdatamemberhasitsdefaultconstructor(string:

:

string())executedbeforeeitherTextconstructorruns,hencethedefaultTextconstructorjustbuildsanemptystring.Thisprogramprintsanemptystringfort1followedbythecontentsofthefilenamedinthefirstcommand-lineargument.Notetheuseofstring:

c_str()inthesecondconstructor.That’sbecausetheifstreamconstructortakesachar*argument,notastring.

7-2

CreateaMessageclasswithaconstructorthattakesasinglestringwithadefaultvalue.Createaprivatememberstring,andintheconstructorsimplyassigntheargumentstringtoyourinternalstring.Createtwooverloadedmemberfunctionscalledprint():

onethattakesnoargumentsandsimplyprintsthemessagestoredintheobject,andonethattakesastringargument,whichitprintsinadditiontotheinternalmessage.Doesitmakesensetousethisapproachinsteadoftheoneusedfortheconstructor?

Message.cpp

classMessage{

stringmsg;

Message(conststring&

s="

MSG"

):

msg(s){}

voidprint(){

msg<

voidprint(conststring&

suffix){

'

suffix<

 

intmain(){

Messagem1;

Messagem2("

Error"

);

m1.print();

m2.print();

m1.print("

hello"

m2.print("

goodbye"

/*Output:

MSG

Error

MSGhello

Errorgoodbye

*/

It’susuallymoreflexibletoallowoptionalargumentsinthecalltoprint,sincethetextofamessageisfixedwhenitiscreated.Acommontechniqueallowsanoptionalprefixformessages,asthefollowingexampleillustrates.

MessageWithPrefix.cpp

s):

prefix){

prefix<

Messagem("

Thisisamessage"

m.print();

m.print("

Attention"

Thisisamessage

Attention:

Thisisamessage

7-3

Determinehowtogenerateassemblyoutputwithyourcompiler,andrunexperimentstodeducethename-decorationscheme.

(Lefttothereader)

7-4

Createaclassthatcontainsfourmemberfunctions,with0,1,2,and3intarguments,respectively.Createamain()thatmakesanobjectofyourclassandcallseachofthememberfunctions.Nowmodifytheclasssoithasinsteadasinglememberfunctionwithalltheargumentsdefaulted.Doesthischangeyourmain()?

Here’sthefirstversion:

ManyArgs.cpp

classManyArgs{

voidf(){

{}\n"

voidf(inti){

{"

i<

}\n"

voidf(inti,intj){

"

j<

voidf(inti,intj,intk){

k<

ManyArgsa;

a.f();

a.f

(1);

a.f(1,2);

a.f(1,2,3);

{}

{1}

{1,2}

{1,2,3}

Nowcomparetheoutputabovetothatfromthisdefault-argumentversion:

DefaultArgs.cpp

classDefaultArgs{

voidf(inti=0,intj=0,intk=0){

DefaultArgsa;

{0,0,0}

{1,0,0}

{1,2,0}

Althoughit’struethattheoperationsinmain()didnotchange,therespectiveoutputssuggestwheneachfeatureisappropriate.Usedefaultargumentswhentheretrulyisadefaultvalue(likezeroabove).Whenyouwantnovalueatallincertaininstances,thenthefunctionsaredifferentenoughthatyouneedtheoverloads.

7-5

Createafunctionwithtwoargumentsandcallitfrommain().Nowmakeoneoftheargumentsa“placeholder”(noidentifier)andseeifyourcallinmain()changes.

NamelessArg.cpp

voidtwoArgs(inti,floatx){

twoArgs("

x<

)\n"

voidplaceHolder(inti,float){

)\n"

twoArgs(1,2);

placeHolder(1,2);

twoArgs(1,2)

twoArgs(1,)

Placeholdersareusefulinthoserareoccasions(ofteninmaintainingcode)whenyouneeddifferentversionsofafunction,butonlythetype,notthevalue,ofthedifferentiatingparameterisimportant.

(Exercises6–10lefttothereader)

7-6

ModifyStash3.handStash3.cpptousedefaultargumentsintheconstructor.TesttheconstructorbymakingtwodifferentversionsofaStashobject.

7-7

CreateanewversionoftheStackclass(fromChapter6)thatcontainsthedefaultconstructorasbefore,andasecondconstructorthattakesasitsargumentsanarrayofpointerstoobjectsandthesizeofthatarray.ThisconstructorshouldmovethroughthearrayandpusheachpointerontotheStack.Testyourclasswithanarrayofstring.

7-8

ModifySuperVarsothatthereare#ifdefsaroundallthevartypecodeasdescribedinthesectiononenum.Makevartypearegularandpublicenumeration(withnoinstance)andmodifyprint()sothatitrequiresavartypeargumenttotellitwhattodo.

7-9

ImplementMem2.handmakesurethatthemodifiedclassstillworkswithMemTest.cpp.

7-10

UseclassMemtoimplementStash.Notethatbecausetheimplementationisprivateandthushiddenfromtheclientprogrammer,thetestcodedoesnotneedtobemodified.

InclassMem,addaboolmoved()memberfunctionthattakestheresultofacalltopointer()andtellsyouwhetherthepointerhasmoved(duetoreallocation).Writeamain()thattestsyourmoved()memberfunction.Doesitmakemoresensetousesomethinglikemoved()ortosimplycallpointer()everytimeyouneedtoaccessthememoryinMem?

LastUpdate:

06/27/2002

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2