电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

完整版C++编程思想答案其他章节请点击用户名找thinkinginC+VIP免费

完整版C++编程思想答案其他章节请点击用户名找thinkinginC+_第1页
1/12
完整版C++编程思想答案其他章节请点击用户名找thinkinginC+_第2页
2/12
完整版C++编程思想答案其他章节请点击用户名找thinkinginC+_第3页
3/12
[ Viewing Hints ] [ Book Home Page ] [ Free Newsletter ] [ Seminars ] [ Seminars on CD ROM ] [ Consulting ] Annotated Solution Guide Revision 1.0 for Thinking in C++, 2nd edition, Volume 1by Chuck Allison ? 2001 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 stringmember 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(argv[1]); 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 Textconstructor 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. That’ s because the ifstr...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

完整版C++编程思想答案其他章节请点击用户名找thinkinginC+

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部