面向对象的C++程序设计 第六版 课后习题答案第三章.doc

面向对象的C++程序设计 第六版 课后习题答案第三章.doc

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
面向对象的C程序设计第六版课后习题答案第三章

Chapter 3 MORE FLOW OF CONTROL 1. Solutions for and Remarks on Selected Programming Problems In order to preserve flexibility of the text, the author has not dealt with classes in this chapter at all. To help those who really want to do things with classes, some of the Programming Projects will be carried out using one or several classes. 1. Rock Scissors Paper Here each of two players types in one of strings Rock, Scissors, or Paper. After the second player has typed in the string, the winner of this game is determined and announced. Rules: Rock breaks Scissors Scissors cuts Paper Paper covers Rock If both players give the same answer, then there is no winner. A nice touch would be to keep and report total wins for each player as play proceeds. To find the classes, we note first that there are 2 identical players. These players record the character entered, and keep track of the wins. There is a constructor that sets the total wins to 0. There is a play() member function that prompts for and gets from the keyboard returns one of the characters R, P, or S for rock, paper and scissors. There is an accessor member function for ch and an incrementor member function for accumulated wins so that a stand alone function int win(ch, ch); can determine who wins and increment the accumulated wins variable. (A better solution would make the wins function a friend of class player so that wins can have access to the each players private data to determine who wins and update the accumulated wins without the accessor function ch() and incrementWins() function.) Note that I have left a bit of debugging code in the program. class player { public function members constructors() play(); // prompts for and gets this players move char ch(); // accessor int accumulatedWins(); // accessor incrWins(); // increments win count private data character typed in accumulatedWins }; int wins(player user1, player user2); //player1s character is compared to player2s char

文档评论(0)

asd522513656 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档