C++编程思想答案第七章其他章节请点击用户名找thinkinginC++annotatedsolutionguide(charpter7).doc_第1页
C++编程思想答案第七章其他章节请点击用户名找thinkinginC++annotatedsolutionguide(charpter7).doc_第2页
C++编程思想答案第七章其他章节请点击用户名找thinkinginC++annotatedsolutionguide(charpter7).doc_第3页
C++编程思想答案第七章其他章节请点击用户名找thinkinginC++annotatedsolutionguide(charpter7).doc_第4页
C++编程思想答案第七章其他章节请点击用户名找thinkinginC++annotatedsolutionguide(charpter7).doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

Viewing 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 Allison2001 MindView, Inc. All Rights Reserved. Previous Chapter Table of Contents Next Chapter Chapter 77-1Create a Text class that contains a string object to hold the text of a file. Give it two constructors: a default constructor and a constructor that takes a string argument that is the name of the file to open. When the second constructor is used, open the file and read the contents into the string member object. 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.Solution:/: S07:Text.cpp#include #include #include using namespace std;class Text string text;public: Text() Text(const string& fname) ifstream ifs(fname.c_str(); string line; while (getline(ifs, line) text += line + n; string contents() return text; ;int main(int argc, char* argv) if (argc 1) Text t1; Text t2(argv1); cout t1 :n t1.contents() endl; cout t2 :n t2.contents() endl; /:When creating a Text object, the compiler guarantees that the text data member has its default constructor (string:string( ) executed before either Text constructor runs, hence the default Text constructor just builds an empty string. This program prints an empty string for t1 followed by the contents of the file named in the first command-line argument. Note the use of string:c_str( ) in the second constructor. Thats because the ifstream constructor takes a char* argument, not a 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 simply 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 addition to the internal message. Does it make sense to use this approach instead of the one used for the constructor?Solution:/: S07:Message.cpp#include #include using namespace std;class Message string msg;public: Message(const string& s = MSG) : msg(s) void print() cout msg endl; void print(const string& suffix) cout msg suffix endl; ;int main() Message m1; Message m2(Error); m1.print(); m2.print(); m1.print(hello); m2.print(goodbye);/* Output:MSGErrorMSG helloError goodbye*/:Its usually more flexible to allow optional arguments in the call to print, since the text of a message is fixed when it is created. A common technique allows an optional prefix for messages, as the following example illustrates./: S07:MessageWithPrefix.cpp#include #include using namespace std;class Message string msg;public: Message(const string& s) : msg(s) void print() cout msg endl; void print(const string& prefix) cout prefix : msg endl; ;int main() Message m(This is a message); m.print(); m.print(Attention);/* Output:This is a messageAttention: This is a message*/:7-3Determine how to generate assembly output with your compiler, and run experiments to deduce the name-decoration scheme.(Left to 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 instead a single member function with all the arguments defaulted. Does this change your main( )?Solution:Heres the first version:/: S07:ManyArgs.cpp#include using namespace std;class ManyArgs public: void f() cout n; void f(int i) cout i n; void f(int i, int j) cout i , j n; void f(int i, int j, int k) cout i , j , k n; ;int main() ManyArgs a; a.f(); a.f(1); a.f(1, 2); a.f(1, 2, 3);/* Output:11, 21, 2, 3*/:Now compare the output above to that from this default-argument version:/: S07:DefaultArgs.cpp#include using namespace std;class DefaultArgs public: void f(int i = 0, int j = 0, int k = 0) cout i , j , k n; ;int main() DefaultArgs a; a.f(); a.f(1); a.f(1, 2); a.f(1, 2, 3);/* Output:0, 0, 01, 0, 01, 2, 01, 2, 3*/:Although its true that the operations in main( ) did not change, the respective outputs suggest when each feature is appropriate. Use default arguments when there truly is a default value (like zero above). When you want no value at all in certain instances, then the functions are different enough that you need 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 call in main( ) changes.Solution:/: S07:NamelessArg.cpp#include using namespace std;void twoArgs(int i, float x) cout twoArgs( i , x )n;void placeHolder(int i, float) cout twoArgs( i ,)n;int main() twoArgs(1, 2); placeHolder(1, 2);/* Output:twoArgs(1, 2)twoArgs(1,)*/:Placeholders are useful in those rare occasions (often in maintaining code) when you need different versions of a function, but only the type, not the value, of the differentiating parameter is important.(Exercises 6 10 left to the reader)7-6Modify Stash3.h and Stash3.cpp to use default arguments in the constructor. Test the constructor by making two different versions of a Stash object.(Left to the reader)7-7Create a new version of the Stack class (from Chapter 6) that contains the default constructor as before, and a second constructor that takes as its arguments an array of pointers to objects and the size of that array. This constructor should move through the array and push each pointer onto the Stack. Test your class with an array of string.(Left to the reader)7-8Modify SuperVar so that there are #ifdefs around all the vartype code as described in the section on enum. Make vartype a regular and publicenumeration (with no instance) and modify print( ) so that it requires a vartype argument to tell it what to do.(Left to the reader)7-9Implement Mem2.h and make sure that the modified cla

温馨提示

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

评论

0/150

提交评论