《C++大学教程第五版》课后习题答案 (作者 DEITEL)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C++大学基础教程课后答案(DEITEL)版3.11GradeBook类定义:#includestring//programusesC++standardstringclassusingstd::string;classGradeBook{public://constructorinitializescoursenameandinstructornameGradeBook(string,string);voidsetCourseName(string);//functiontosetthecoursenamestringgetCourseName();//functiontoretrievethecoursenamevoidsetInstructorName(string);//functiontosetinstructornamestringgetInstructorName();//functiontoretrieveinstructornamevoiddisplayMessage();//displaywelcomemessageandinstructornameprivate:stringcourseName;//coursenameforthisGradeBookstringinstructorName;//instructornameforthisGradeBook};//endclassGradeBook类成员函数:#includeiostreamusingstd::cout;usingstd::endl;#includeGradeBook.h//constructorinitializescourseNameandinstructorName//withstringssuppliedasargumentsGradeBook::GradeBook(stringcourse,stringinstructor){setCourseName(course);//initializescourseNamesetInstructorName(instructor);//initialiZesinstructorName}//endGradeBookconstructor//functiontosetthecoursenamevoidGradeBook::setCourseName(stringname){courseName=name;//storethecoursename}//endfunctionsetCourseName//functiontoretrievethecoursenamestringGradeBook::getCourseName(){returncourseName;}//endfunctiongetCourseName//functiontosettheinstructornamevoidGradeBook::setInstructorName(stringname){instructorName=name;//storetheinstructorname}//endfunctionsetInstructorName//functiontoretrievetheinstructornamestringGradeBook::getInstructorName(){returninstructorName;}//endfunctiongetInstructorName//displayawelcomemessageandtheinstructor'snamevoidGradeBook::displayMessage(){//displayawelcomemessagecontainingthecoursenamecoutWelcometothegradebookfor\ngetCourseName()!endl;//displaytheinstructor'snamecoutThiscourseispresentedby:getInstructorName()endl;}//endfunctiondisplayMessage测试文件:#includeiostreamusingstd::cout;usingstd::endl;//includedefinitionofclassGradeBookfromGradeBook.h#includeGradeBook.h//functionmainbeginsprogramexecutionintmain(){//createaGradeBookobject;passacoursenameandinstructornameGradeBookgradeBook(CS101IntroductiontoC++Programming,ProfessorSmith);//displayinitialvalueofinstructorNameofGradeBookobjectcoutgradeBookinstructornameis:gradeBook.getInstructorName()\n\n;//modifytheinstructorNameusingsetfunctiongradeBook.setInstructorName(AssistantProfessorBates);//displaynewvalueofinstructorNamecoutnewgradeBookinstructornameis:gradeBook.getInstructorName()\n\n;//displaywelcomemessageandinstructor'snamegradeBook.displayMessage();return0;//indicatesuccessfultermination}//endmain3.12类定义:classAccount{public:Account(int);//constructorinitializesbalancevoidcredit(int);//addanamounttotheaccountbalancevoiddebit(int);//subtractanamountfromtheaccountbalanceintgetBalance();//returntheaccountbalanceprivate:intbalance;//datamemberthatstoresthebalance};//endclassAccount类成员函数:#includeiostreamusingstd::cout;usingstd::endl;#includeAccount.h//includedefinitionofclassAccount//AccountconstructorinitializesdatamemberbalanceAccount::Account(intinitialBalance){balance=0;//assumethatthebalancebeginsat0//ifinitialBalanceisgreaterthan0,setthisvalueasthe//balanceoftheAccount;otherwise,balanceremains0if(initialBalance0)balance=initialBalance;//ifinitialBalanceisnegative,printerrormessageif(initialBalance0)coutError:Initialbalancecannotbenegative.\nendl;}//endAccountconstructor//credit(add)anamounttotheaccountbalancevoidAccount::credit(intamount){balance=balance+amount;//addamounttobalance}//endfunctioncredit//debit(subtract)anamountfromtheaccountbalancevoidAccount::debit(intamount){if(amountbalance)//debitamountexceedsbalancecoutDebitamountexceededaccountbalance.\nendl;if(amount=balance)//debitamountdoesnotexceedbalancebalance=balance-amount;}//endfunctiondebit//returntheaccountbalanceintAccount::getBalance(){returnbalance;//givesthevalueofbalancetothecallingfunction}//endfunctiongetBalance测试函数:#includeiostreamusingstd::cout;usingstd::cin;usingstd::endl;//includedefinitionofclassAccountfromAccount.h#includeAccount.h//functionmainbeginsprogramexecutionintmain(){Accountaccount1(50);//createAccountobjectAccountaccount2(25);//createAccountobject//displayinitialbalanceofeachobjectcoutaccount1balance:$account1.getBalance()endl;coutaccount2balance:$account2.getBalance()endl;intwithdrawalAmount;//storeswithdrawalamountreadfromusercout\nEnterwithdrawalamountforaccount1:;//promptcinwithdrawalAmount;//obtainuserinputcout\nattemptingtosubtractwithdrawalAmountfromaccount1balance\n\n;account1.debit(withdrawalAmount);//trytosubtractfromaccount1//displaybalancescoutaccount1balance:$account1.getBalance()endl;coutaccount2balance:$account2.getBalance()endl;cout\nEnterwithdrawalamountforaccount2:;//promptcinwithdrawalAmount;//obtainuserinputcout\nattemptingtosubtractwithdrawalAmountfromaccount2balance\n\n;account2.debit(withdrawalAmount);//trytosubtractfromaccount2//displaybalancescoutaccount1balance:$account1.getBalance()endl;coutaccount2balance:$account2.getBalance()endl;return0;//indicatesu

1 / 51
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功