完整word版C编程思想答案第七章其他章节请点击用户名找thinkinginC_第1页
完整word版C编程思想答案第七章其他章节请点击用户名找thinkinginC_第2页
完整word版C编程思想答案第七章其他章节请点击用户名找thinkinginC_第3页
完整word版C编程思想答案第七章其他章节请点击用户名找thinkinginC_第4页
完整word版C编程思想答案第七章其他章节请点击用户名找thinkinginC_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、mkViewing Hints Book Home Page Free Newsletter Seminars Seminars on CD ROM Consulting Annotated Solution GuideRevision 1.0for Thinking in C+, 2nd edition, Volume 1by Chuck Allison? 2001 MindView, Inc. All Rights Reserved.Previous Chapter Table of Contents Next Chapter Cha pter 77-1Create a Text clas

2、s that contains a string object to hold the text of a file. Give it two construetors: a default constructor and a constructor that takes a string argument that is the name of the file to open. When the sec ond con structor is used, open the file and read the contents into the stri ng member object.

3、Add a member function contents( ) to return the string so (for example) it can be printed. In main( ) , open a file using Text and print the contents.Solutio n:/: S07:Text.c pp #in elude #in elude #in elude using n ames pacestd;class Text stri ng text;p ublic:TextO Text(c onst stri ng& fn ame) ifstr

4、eam ifs(fname.c_str();stri ng line;while (getl in e(ifs, li ne)text += line + n;stri ng conten ts() return text;int main (i nt argc, char* argv) if (argc 1)Text t1;Text t2(argv1);cout t1 :n t1.c onten ts() en dl;cout t2 :n t2.c on te nts() endl;/:t1 followed by the stri ng:c_str( ) in argume nt, not

5、 aWhen creati ng a Text object, the comp iler guara ntees that the text data member has its default con structor (stri ng:stri ng( ) executed before either Text con structor runs, hence the default Text con structor just builds an empty stri ng. This p rogram prints an empty stri ng for contents of

6、the file n amed in the first comma nd-li ne argume nt. Note the use of the second constructor. That s IbfscausiB thenstructor takes a char* string .7-2Create a Message class with a constructor that takes a single string with a default value. Create a private member string , and in the constructor si

7、mply assign the argument string to your internal string . Create two overloaded member functions called print( ) : one that takes no arguments and simply prints the message stored in the object, and one that takes a string argument, which it prints in additi on to the internal message. Does it make

8、sense to use this app roach in stead of the one used for the con structor?Solutio n:/: S07:Message.c pp #in elude #in elude using n ames pacestd;class Message stri ng msg;p ublic:MessageCo nst stri ng& s = MSG) : msg(s) void prin t() cout msg en dl;void prin t(c onst stri ng& suffix) cout msg suffix

9、 en dl;int mai n() Message m1;Message m2(Error);m1. prin t();m2. prin t();m1. prin t(hello);m2.pnn t(goodbye);/* Out put:MSGErrorMSG helloError goodbye */ /:It s usually more flexible to allow optional arguments in the call to print, since the text of a message is fixed whe n it is created. A com mo

10、n tech nique allows an op ti onal p refix for messages, as the follow ing exa mple illustrates./: S07:MessageWith Prefix.c pp #in elude #in elude using n ames pacestd;class Message stri ng msg;p ublic:MessageC onst stri ng& s) : msg(s) voidprintO voidcout msg en dl;prin t(c onst stri ng& p refix) co

11、ut p refix : msg en dl;;int mai n() Message mThis is a message):m.prin t();m.prin t(Atte ntio n);/* Out put:This is a messageAtte nti on: This is a message */ /:7-3Determ ine how to gen erate assembly out put with your comp iler, and run exp erime nts to deduce the n ame-decoratio n scheme.(Left to

12、the reader)7-4Create a class that contains four member functions, with 0, 1,2, and 3 int arguments, respectively. Create a main( ) that makes an object of your class and calls each of the member functions. Now modify the class so it has in stead a sin gle member function with all the argume nts defa

13、ulted. Does this change your main( ) ?Solutio n:Here s the first version:/: S07:Ma ny Args.c pp #in elude using n ames pacestd;class Many Args p ublic:void f() cout n;void f(i nt i) cout i n;void f(int i, i nt j) cout i , j n;void f(int i, i nt j, i nt k) cout i , j , k n;int mai n() Many Args a;a.f

14、();a.f(1);a.f(1, 2);a.f(1, 2, 3);/* Out put: 1 1,2 1,2, 3 */ /:Now compare the out put above to that from this default-argume nt version:/: S07:DefaultArgs.c pp #in clude using n ames pacestd;class DefaultArgs p ublic:void f(int i = 0, i nt j = 0, int k = 0) cout i , j , k n;int mai n() DefaultArgs

15、a;a.f();a.f(1);a.f(1, 2);a.f(1, 2, 3);/* Out put: 0, 0, 0 1, 0, 0 1,2, 0 1,2, 3 */ /:Although it s true that the op eratioirisairm( ) did not cha nge, the resp ective out puts suggest whe n each feature is approp riate. Use default argume nts whe n there truly is a default value (like zero above). W

16、hen you want no value at all in certa in in sta nces, the n the fun ctio ns are differe nt eno ugh that you n eed the overloads.7-5Create a function with two arguments and call it from main( ) . Now make one of the arguments a “ placeholder” (no identifier) and see if your calh ain( ) changes.Soluti

17、o n:/: S07:NamelessArg.c pp #in elude using n ames pacestd;void twoArgs(i nt i, float x) cout twoArgs( i , x )n;void p laceHolder (int i, float) cout twoArgs( i ,)n;int mai n() twoArgs(1, 2);placeHolder(1,2);/* Out put:twoArgs(1,2) twoArgs(1,) */ /:Placeholders are useful in those rare occasi ons (o

18、fte n in maintaining code) whe n you n eed differe nt versions of a function, but only the type, not the value, of the differe ntiati ng p arameter is important.(Exercises 6 TO left to the reader)7-6Modify Stash3.h and Stash3.cpp to use default arguments in the constructor. Test the constructor by m

19、aking two different versions of a Stash object.(Left to the reader)7-7Create a new version of the Stack class (from Chap ter 6) that contains the default con structor as before, and a sec ond con structor that takes as its argume nts an array of poin ters to objects and the size of that array. This

20、con structor should move through the array and push each poin ter onto the Stack. Test your class with an array of string .(Left to the reader)7-8Modify Sup erVar so that there are #ifdef s around all the vartype code as described in the sect ion on enum. Make vartype a regular and public enumeration (with no instanee) and modify print() so that it requires a vartype argument to tell it what to do.(Left to the reader)7-9ImpIement Mem2.h and make sure that the mo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论