C++编程思想 答案 第六章 其他章节请点击用户名找 thinking in C++ annotated solution guide(charpter 6).docVIP

  • 28
  • 0
  • 约6.21千字
  • 约 8页
  • 2020-08-13 发布于浙江
  • 举报

C++编程思想 答案 第六章 其他章节请点击用户名找 thinking in C++ annotated solution guide(charpter 6).doc

[ 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 1 by Chuck Allison ?2001 MindView, Inc. All Rights Reserved. [ Previous Chapter ] [ Table of Contents ] [ Next Chapter ] Chapter 6 6-1 Write a simple class called Simplewith a constructor that prints something to tell you that it’s been called. In main( ) make an object of your class. (see Exercise 3) 6-2 Add a destructor to Exercise 1 that prints out a message to tell you that it’s been called. (see Exercise 3) 6-3 Modify Exercise 2 so that the class contains an int member. Modify the constructor so that it takes an int argument that it stores in the class member. Both the constructor and destructor should print out the int value as part of their message, so you can see the objects as they are created and destroyed. Solution: //: S06:Announce.cpp #include iostream using namespace std; class Announce { int x; public: Announce(int x) { this-x = x; cout Announce( x )\n; } ~Announce() { cout ~Announce( x )\n; } }; int main() { using namespace std; Announce a1(1); Announce a2(2); { Announce a3(3); Announce a4(4); goto jump; Announce a5(5); } jump: Announce a6(6); cout After jumping!\n; } /* Output: Announce(1) Announce(2) Announce(3) Announce(4) ~Announce(4) ~Announce(3) Announce(6) ~Announce(6) ~Announce(2) ~Announce(1) */ ///:~ The objects are created in the order they appear, but a4 and a3 (in that order) are the first to be destroyed since their scope terminates after the block nested inside of main( ). They are destroyed in spite of the fact that I jump out of the block prematurely (but a5 is not created, of course). This fact solves problem 4 also. 6-4 Demonstrate that destructors are still called even when goto is used to jump out of a loop. (Left to the read

文档评论(0)

1亿VIP精品文档

相关文档