- 28
- 0
- 约6.21千字
- 约 8页
- 2020-08-13 发布于浙江
- 举报
[ Viewing Hints ] [ Book Home Page ] [ Free Newsletter ] [ Seminars ] [ Seminars on CD ROM ] [ Consulting ]
Annotated Solution GuideRevision 1.0for 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
您可能关注的文档
- 《好习惯伴我成长》班会反思.doc
- 《好习惯成就大未来》活动课教案.doc
- 《好心分手》歌词 卢巧音.doc
- 《好性格成就更好的我》读书笔记.doc
- 《好一朵木槿花》赏析及答题技巧.doc
- 《呵护眼睛从小做起》五年级班会.doc
- 《喝牛奶-解决问题》教学设计.doc
- 《合唱艺术鉴赏》总结感想.doc
- 《合欢树》阅读练习及答案.doc
- 《合理运用多媒体技术优化小学英语课堂教学的研究》课题中期报告.doc
- 语文统编版八年级上册6国行公祭,为佑世界和平(大单元分层作业学生版).docx
- 中考总复习历史世界近代史第二单元资本主义制度的初步确立(课件).ppt
- 语文统编版八年级上册1消息二则(大单元分层作业教师版).docx
- 语文统编版八年级上册5一着惊海天——目击我国航母舰载战斗机首架次成功着舰(大单元分层作业学生版).docx
- 语文统编版八年级上册3首届诺贝尔奖颁发(大单元分层作业学生版).docx
- 语文统编版八年级上册4“飞天”凌空——跳水姑娘吕伟夺魁记(大单元分层作业学生版).docx
- 爆炸危险环境高海拔地区电力设计规范与修正系数.docx
- 爆炸危险环境仪表保温伴热设计规范与电伴热选型.docx
- 港口码头爆炸危险环境电力设计规范实务.docx
- 煤化工爆炸危险环境电力设计规范与气化炉.docx
原创力文档

文档评论(0)