ImageVerifierCode 换一换
格式:DOCX , 页数:8 ,大小:38.31KB ,
资源ID:8443365      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-8443365.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++编程思想 答案 第七章 其他章节请点击用户名找 thinking in C++ annotated solution guidecharpter 7Word格式文档下载.docx)为本站会员(b****5)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

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

1、Text.cpp#include fstreamstringusing 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); cou

2、t t1 :n t1.contents() endl;t2 : t2.contents() /: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 prin

3、ts 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 si

4、ngle 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 tak

5、es 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?Message.cppclass Message string msg; Message(const string& s = MSG) : msg(s) void print() msg void print(const string& suffix) suffix int mai

6、n() 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 option

7、al prefix for messages, as the following example illustrates.MessageWithPrefix.cpp s) : prefix) prefix Message m(This is a message m.print(); m.print(AttentionThis is a messageAttention: This is a message7-3Determine how to generate assembly output with your compiler, and run experiments to deduce t

8、he 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 fun

9、ction with all the arguments defaulted. Does this change your main( )?Heres the first version:ManyArgs.cppclass ManyArgs void f() n void f(int i) i n void f(int i, int j) , j void f(int i, int j, int k) k ManyArgs a; a.f(); a.f(1); a.f(1, 2); a.f(1, 2, 3);11, 21, 2, 3Now compare the output above to

10、that from this default-argument version:DefaultArgs.cppclass DefaultArgs void f(int i = 0, int j = 0, int k = 0) DefaultArgs a;0, 0, 01, 0, 01, 2, 0Although its true that the operations in main( ) did not change, the respective outputs suggest when each feature is appropriate. Use default arguments

11、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 identifie

12、r) and see if your call in main( ) changes.NamelessArg.cppvoid twoArgs(int i, float x) twoArgs( x )nvoid placeHolder(int i, float) ,)n twoArgs(1, 2); placeHolder(1, 2);twoArgs(1, 2)twoArgs(1,)Placeholders are useful in those rare occasions (often in maintaining code) when you need different versions

13、 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.7-7Create a new ve

14、rsion 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 c

15、lass with an array of string.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.7-9Implement Mem

16、2.h and make sure that the modified class still works with MemTest.cpp.7-10Use class Mem to implement Stash. Note that because the implementation is private and thus hidden from the client programmer, the test code does not need to be modified.In class Mem, add a bool moved( ) member function that t

17、akes the result of a call to pointer( ) and tells you whether the pointer has moved (due to reallocation). Write a main( ) that tests your moved( ) member function. Does it make more sense to use something like moved( ) or to simply call pointer( ) every time you need to access the memory in Mem?Last Update:06/27/2002

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

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