《C++大学教程第五版》课后习题答案文档格式.docx
《《C++大学教程第五版》课后习题答案文档格式.docx》由会员分享,可在线阅读,更多相关《《C++大学教程第五版》课后习题答案文档格式.docx(70页珍藏版)》请在冰豆网上搜索。
//instructornameforthisGradeBook
};
//endclassGradeBook
类成员函数:
iostream>
cout;
endl;
#include"
GradeBook.h"
//constructorinitializescourseNameandinstructorName
//withstringssuppliedasarguments
GradeBook:
GradeBook(stringcourse,stringinstructor)
setCourseName(course);
//initializescourseName
setInstructorName(instructor);
//initialiZesinstructorName
}//endGradeBookconstructor
//functiontosetthecoursename
voidGradeBook:
setCourseName(stringname)
courseName=name;
//storethecoursename
}//endfunctionsetCourseName
//functiontoretrievethecoursename
stringGradeBook:
getCourseName()
returncourseName;
}//endfunctiongetCourseName
//functiontosettheinstructorname
setInstructorName(stringname)
instructorName=name;
//storetheinstructorname
}//endfunctionsetInstructorName
//functiontoretrievetheinstructorname
getInstructorName()
returninstructorName;
}//endfunctiongetInstructorName
//displayawelcomemessageandtheinstructor'
sname
displayMessage()
//displayawelcomemessagecontainingthecoursename
cout<
<
"
Welcometothegradebookfor\n"
<
getCourseName()<
!
"
endl;
//displaytheinstructor'
Thiscourseispresentedby:
getInstructorName()<
}//endfunctiondisplayMessage
测试文件:
//includedefinitionofclassGradeBookfromGradeBook.h
//functionmainbeginsprogramexecution
intmain()
//createaGradeBookobject;
passacoursenameandinstructorname
GradeBookgradeBook(
CS101IntroductiontoC++Programming"
"
ProfessorSmith"
);
//displayinitialvalueofinstructorNameofGradeBookobject
gradeBookinstructornameis:
gradeBook.getInstructorName()<
\n\n"
;
//modifytheinstructorNameusingsetfunction
gradeBook.setInstructorName("
AssistantProfessorBates"
//displaynewvalueofinstructorName
newgradeBookinstructornameis:
//displaywelcomemessageandinstructor'
gradeBook.displayMessage();
return0;
//indicatesuccessfultermination
}//endmain
3.12
类定义:
classAccount
Account(int);
//constructorinitializesbalance
voidcredit(int);
//addanamounttotheaccountbalance
voiddebit(int);
//subtractanamountfromtheaccountbalance
intgetBalance();
//returntheaccountbalance
intbalance;
//datamemberthatstoresthebalance
//endclassAccount
Account.h"
//includedefinitionofclassAccount
//Accountconstructorinitializesdatamemberbalance
Account:
Account(intinitialBalance)
balance=0;
//assumethatthebalancebeginsat0
//ifinitialBalanceisgreaterthan0,setthisvalueasthe
//balanceoftheAccount;
otherwise,balanceremains0
if(initialBalance>
0)
balance=initialBalance;
//ifinitialBalanceisnegative,printerrormessage
if(initialBalance<
Error:
Initialbalancecannotbenegative.\n"
}//endAccountconstructor
//credit(add)anamounttotheaccountbalance
voidAccount:
credit(intamount)
balance=balance+amount;
//addamounttobalance
}//endfunctioncredit
//debit(subtract)anamountfromtheaccountbalance
debit(intamount)
if(amount>
balance)//debitamountexceedsbalance
Debitamountexceededaccountbalance.\n"
if(amount<
=balance)//debitamountdoesnotexceedbalance
balance=balance-amount;
}//endfunctiondebit
//returntheaccountbalance
intAccount:
getBalance()
returnbalance;
//givesthevalueofbalancetothecallingfunction
}//endfunctiongetBalance
测试函数:
cin;
//includedefinitionofclassAccountfromAccount.h
Accountaccount1(50);
//createAccountobject
Accountaccount2(25);
//displayinitialbalanceofeachobject
account1balance:
$"
account1.getBalance()<
account2balance:
account2.getBalance()<
intwithdrawalAmount;
//storeswithdrawalamountreadfromuser
\nEnterwithdrawalamountforaccount1:
//prompt
cin>
>
withdrawalAmount;
//obtainuserinput
\nattemptingtosubtract"
withdrawalAmount
fromaccount1balance\n\n"
account1.debit(withdrawalAmount);
//trytosubtractfromaccount1
//displaybalances
\nEnterwithdrawalamountforaccount2:
fromaccount2balance\n\n"
account2.debit(withdrawalAmount);
//trytosubtractfromaccount2
3.13
//Invoiceclassdefinition
classInvoice
//constructorinitializesthefourdatamembers
Invoice(string,string,int,int);
//setandgetfunctionsforthefourdatamembers
voidsetPartNumber(string);
//partnumber
stringgetPartNumber();
voidsetPartDescription(string);
//partdescription
stringgetPartDescription();
voidsetQuantity(int);
//quantity
intgetQuantity();
voidsetPricePerItem(int);
//priceperitem
intgetPricePerItem();
//calculatesinvoiceamountbymultiplyingquantityxpriceperitem
intgetInvoiceAmount();
stringpartNumber;
//thenumberofthepartbeingsold
stringpartDescription;
//descriptionofthepartbeingsold
intquantity;
//howmanyoftheitemsarebeingsold
intpricePerItem;
//endclassInvoice
//includedefinitionofclassInvoicefromInvoice.h
Invoice.h"
//Invoiceconstructorinitializestheclass'
sfourdatamembers
Invoice:
Invoice(stringnumber,stringdescription,intcount,
intprice)
setPartNumber(number);
//storepartNumber
setPartDescription(description);
//storepartDescription
setQuantity(count);
//validateandstorequantity
setPricePerItem(price);
//validateandstorepricePerItem
}//endInvoiceconstructor
//setpartnumber
voidInvoice:
setPartNumber(stringnumber)
partNumber=number;
//novalidationneeded
}//endfunctionsetPartNumber
//getpartnumber
stringInvoice:
getPartNumber()
returnpartNumber;
}//endfunctiongetPartNumber
//setpartdescription
setPartDescription(stringdescription)
partDescription=description;
}//endfunctionsetPartDescription
//getpartdescription
getPartDescription()
returnpartDescription;
}//endfunctiongetPartDescription
//setquantity;
ifnotpositive,setto0
setQuantity(intcount)
if(count>
0)//ifquantityispositive
quantity=count;
//setquantitytocount
if(count<
=0)//ifquantityisnotpositive
{
quantity=0;
//setquantityto0
\nquantitycannotbenegative.quantitysetto0.\n"
}//endif
}//endfunctionsetQuantity
//getquantity
intInvoice:
getQuantity()
returnquantity;
}//endfunctiongetQuantity
//setpriceperitem;
setPricePerItem(intprice)
if(price>
0)//ifpriceispositive
pricePerItem=price;
//setpricePerItemtoprice
if(price<
=0)//ifpriceisnotpositive
pricePerItem=0;
//setpricePerItemto0
\npricePerItemcannotbenegative."
pricePerItemsetto0.\n"
}//endfunctionsetPricePerItem
//getpriceperitem
getPricePerItem()
returnpricePerItem;
}//endfunctiongetPricePerItem
//calulatesinvoiceamountbymultiplyingquantityxpriceperitem
getInvoiceAmount()
returngetQuantity()*getPricePerItem();
}//endfunctiongetInvoiceAmount
//createanInvoiceobject
Invoiceinvoice("
12345"
Hammer"
100,5);
//displaytheinvoicedatamembersandcalculatetheamount
Partnumber:
invoice.getPartNumber()<
Partdescription:
invoice.getPartDescription()<
Qua