《C++大学教程第五版》课后习题答案Word格式文档下载.docx

上传人:b****1 文档编号:3312356 上传时间:2023-05-01 格式:DOCX 页数:82 大小:34.55KB
下载 相关 举报
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第1页
第1页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第2页
第2页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第3页
第3页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第4页
第4页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第5页
第5页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第6页
第6页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第7页
第7页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第8页
第8页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第9页
第9页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第10页
第10页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第11页
第11页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第12页
第12页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第13页
第13页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第14页
第14页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第15页
第15页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第16页
第16页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第17页
第17页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第18页
第18页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第19页
第19页 / 共82页
《C++大学教程第五版》课后习题答案Word格式文档下载.docx_第20页
第20页 / 共82页
亲,该文档总共82页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

《C++大学教程第五版》课后习题答案Word格式文档下载.docx

《《C++大学教程第五版》课后习题答案Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《《C++大学教程第五版》课后习题答案Word格式文档下载.docx(82页珍藏版)》请在冰点文库上搜索。

《C++大学教程第五版》课后习题答案Word格式文档下载.docx

};

//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()<

Quanti

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2