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

上传人:b****4 文档编号:27062562 上传时间:2023-06-26 格式:DOCX 页数:59 大小:30.97KB
下载 相关 举报
面向对象的C++程序设计 第六版 课后习题答案 第十章.docx_第1页
第1页 / 共59页
面向对象的C++程序设计 第六版 课后习题答案 第十章.docx_第2页
第2页 / 共59页
面向对象的C++程序设计 第六版 课后习题答案 第十章.docx_第3页
第3页 / 共59页
面向对象的C++程序设计 第六版 课后习题答案 第十章.docx_第4页
第4页 / 共59页
面向对象的C++程序设计 第六版 课后习题答案 第十章.docx_第5页
第5页 / 共59页
点击查看更多>>
下载资源
资源描述

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

《面向对象的C++程序设计 第六版 课后习题答案 第十章.docx》由会员分享,可在线阅读,更多相关《面向对象的C++程序设计 第六版 课后习题答案 第十章.docx(59页珍藏版)》请在冰豆网上搜索。

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

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

Chapter10

DEFININGCLASSESANDABSTRACTDATATYPES

1.SolutionsforandRemarksonSelectedProgrammingProblems

ThischaptercanbedoneafterChapter7,Arrays.However,Ihavenotusedanythingfromthatchapterinthesesolutions.Severalofthesesolutionscouldbesimplifiedingoodmeasureifarrayswereusedinsteadoftheextensiveswitchandnestedifelsestatements.

1.Classgradingprogram

Ihaveputstatementsofprogrammingstrategyandoftheproblemintheprogramcomments.

//ch10Prg1.cpp#includeusingnamespacestd;constintCLASS_SIZE=5;

//Theproblemsaysthisisforaclass,ratherthanonestudent.One

//programmingstratagemistodealwithasinglestudent,thenextend

//totreatanarrayofNstudents.

//GradingProgram

//Policies:

//

//Twoquizzes,10pointseach

//midtermandfinalexam,100pointseach

//Ofgrade,finalcounts50%,midterm25%,quizes25%

//

//LetterGradeisassigned:

//90ormoreA

//80ormoreB

//70ormoreC

//60ormoreD

//lessthan60,F

//

//Readastudent'sscores,

//outputrecord:

scores+numericaverage+assignedlettergrade

//

//Useastructtocontainstudentrecord.

structStudentRecord

{

intstudentNumber;

doublequiz1;

doublequiz2;

doublemidterm;

doublefinal;

doubleaverage;

chargrade;

};

voidinput(StudentRecord&student);

//promptsforinputforonestudent,setsthe

//structurevariablemembers.

voidcomputeGrade(StudentRecord&student);

//calculatesthenumericaverageandlettergrade.

voidoutput(constStudentRecordstudent);

//outputsthestudentrecord.

intmain()

{

StudentRecordstudent[CLASS_SIZE];

for(inti=0;i

input(student[i]);

//EnclosingblockfixesVC++"for"loopcontroldefinedoutsideloop

{for(inti=0;i

{

computeGrade(student[i]);

output(student[i]);

cout<

}

}

return0;

}

voidinput(StudentRecord&student)

{

cout<<"enterthestudentnumber:

";

cin>>student.studentNumber;

cout<

cout<<"entertwo10pointquizes"<

cin>>student.quiz1>>student.quiz2;

cout<

cout<<"enterthemidtermandfinalexamgrades."

<<"Theseare100pointtests\n";

cin>>student.midterm>>student.final;

cout<

<

}

voidcomputeGrade(StudentRecord&student)

{

//Ofgrade,finalcounts50%,midterm25%,quizes25%

doublequizAvg=(student.quiz1+student.quiz2)/2.0;

doublequizAvgNormalized=quizAvg*10;

student.average=student.final*0.5+

student.midterm*0.25+

quizAvgNormalized*0.25;

charletterGrade[]="FFFFFFDCBAA";

intindex=static_cast(student.average/10);

if(index<0||10<=index)

{

cout<<"Badnumericgradeencountered:

"

<

<<"Aborting.\n";

abort();

}

student.grade=letterGrade[index];

}

voidoutput(constStudentRecordstudent)

{

cout<<"Therecordforstudentnumber:

"

<

<<"Thequizgradesare:

"

<

<

<<"Themidtermandexamgradesare:

"

<

<

<<"Thenumericaverageis:

"<

<

<<"andthelettergradeassignedis"

<

<

}

 

Dataforthetestrun:

17109095

2989080

3787080

4585070

5404035

Commandlinecommandtoexecutethetextrun:

ch10prg1

Output:

enterthestudentnumber:

1

entertwo10pointquizes

710

enterthemidtermandfinalexamgrades.Theseare100pointtests

9095

enterthestudentnumber:

2

entertwo10pointquizes

98

enterthemidtermandfinalexamgrades.Theseare100pointtests

9080

enterthestudentnumber:

3

entertwo10pointquizes

78

enterthemidtermandfinalexamgrades.Theseare100pointtests

7080

enterthestudentnumber:

4

entertwo10pointquizes

58

enterthemidtermandfinalexamgrades.Theseare100pointtests

5070

enterthestudentnumber:

5

entertwo10pointquizes

40

enterthemidtermandfinalexamgrades.Theseare100pointtests

4035

Therecordforstudentnumber:

1

Thequizgradesare:

710

Themidtermandexamgradesare:

9095

Thenumericaverageis:

91.25

andthelettergradeassignedisA

Therecordforstudentnumber:

2

Thequizgradesare:

98

Themidtermandexamgradesare:

9080

Thenumericaverageis:

83.75

andthelettergradeassignedisB

Therecordforstudentnumber:

3

Thequizgradesare:

78

Themidtermandexamgradesare:

7080

Thenumericaverageis:

76.25

andthelettergradeassignedisC

Therecordforstudentnumber:

4

Thequizgradesare:

58

Themidtermandexamgradesare:

5070

Thenumericaverageis:

63.75

andthelettergradeassignedisD

Therecordforstudentnumber:

5

Thequizgradesare:

40

Themidtermandexamgradesare:

4035

Thenumericaverageis:

32.5

andthelettergradeassignedisF

*/

2.RedefineCDAccountfromDisplay10.1tobeaclassratherthanstruct.

Usethesamevariables,makethemprivate.

Addmemberfunctions:

toreturninitialbalance

toreturnbalanceatmaturity

toreturninterestrate

toreturntheterm

defaultconstructor

constructortosetspecifiedvalues

inputfunction(istream&);

outputfunction(ostream&);

Embedinatestprogram

ThecodeinDisplay10.1makesthebehavioroftherequiredfunctionsclear.

Noteoncapitalizationschemes:

Iuseaslightlydifferentcapitalizationschemethantheauthor.Youshouldmakeyourconventionscleartothestudent.Anycapitalizationthatproducesreadablecodeisacceptabletothisauthor.Theinstructor,asalways,isleftfreetodoasiswished.

//File:

ch10Prg2.cpp

//Title:

CDAccount

#include

usingnamespacestd;

classCDAccount

{

public:

CDAccount();

CDAccount(doublebal,doubleintRate,intT);

doubleInterestRate();

doubleInitialBalance();

doubleBalanceAtMaturity();

intTerm();

voidinput(istream&);

voidoutput(ostream&);

private:

doublebalance;

doubleinterestRate;//inPERCENT

intterm;//monthstomaturity;

};

intmain()

{

doublebalance;doubleintRate;

intterm;

CDAccountaccount=CDAccount(100.0,10.0,6);

cout<<"CDAccountinterestrate:

"

<

cout<<"CDAccountinitialbalance:

"

<

cout<<"CDAccountbalanceatmaturityis:

"

<

cout<<"CDAccounttermis:

"<

<<"months"<

account.output(cout);

cout<<"EnterCDinitialbalance,interestrate,"

<<"andterm:

"<

account.input(cin);

cout<<"CDAccountinterestrate:

"

<

cout<<"CDAccountinitialbalance:

"

<

cout<<"CDAccountbalanceatmaturityis:

"

<

cout<<"CDAccounttermis:

"<

<<"months"<

account.output(cout);

cout<

}

CDAccount:

:

CDAccount(){/*donothing*/}

CDAccount:

:

CDAccount(doublebal,doubleintRate,intT)

{

balance=bal;

interestRate=intRate;

term=T;

}

doubleCDAccount:

:

InterestRate()

{

returninterestRate;

}

doubleCDAccount:

:

InitialBalance()

{

returnbalance;

}

doubleCDAccount:

:

BalanceAtMaturity()

{

returnbalance*(1+(interestRate/100)*(term/12.0));

}

intCDAccount:

:

Term()

{

returnterm;

}

voidCDAccount:

:

input(istream&inStream)

{

inStream>>balance;

inStream>>interestRate;

inStream>>term;

}

voidCDAccount:

:

output(ostream&outStream)

{

outStream.setf(ios:

:

fixed);

outStream.setf(ios:

:

showpoint);

outStream.precision

(2);

outStream<<"whenyourCDmaturesin"<

<<"months"<

<<"itwillhaveabalanceof"

<

}

/*

Atypicalrunfollows:

CDAccountinterestrate:

10

CDAccountinitialbalance:

100

CDAccountbalanceatmaturityis:

105

CDAccounttermis:

6months

whenyourCDmaturesin6months

itwillhaveabalanceof105.00

EnterCDinitialbalance,interestrate,andterm:

200

10

12

CDAccountinterestrate:

10.00

CDAccountinitialbalance:

200.00

CDAccountbalanceatmaturityis:

220.00

CDAccounttermis:

12months

whenyourCDmaturesin12months

itwillhaveabalanceof220.00

*/

3.CDaccount,differentinterface

RedothedefinitionofclassCDAccountfromProject2sothattheinterfaceisthesamebuttheimplementationisdifferent.ThenewimplementationissimilartothesecondimplementationofBankAccountinDisplay10.7.Herethebalanceisrecordedintwointvalues,onefordollars,oneforcents.Themembervariableforinterestratestorestheinterestasafractionratherthanapercentage.TermisstoredasinProject2.

Remark:

Thechangestobemadeareinthefunctionsthattakebalanceasargument.Theimplementationofthemembersmustchange:

1)togeneratetheintobjectsdollarsandcentsfromtheexternalrepresentationofbalance(adouble)

2)totakedollarsandcents(intobjects)fromtheinternalrepresentationandgeneratetheexternalinformation.

//File:

ch10Prg3.cpp

//Title:

CDAccount-modificationofProgram1,butwith

//differentimplementationbutSAMEinterface.

//ThenewimplementationshouldbesimilartoDisplay10.7

//recordbalanceastwointvalues:

Onefordollars,onefor

//cents.interestrateisadouble(decimal)fractionrather

//thanapercent(0.043,not4.3%).termisstoredthesame

//wayasProgram1.

#include

usingnamespacestd;

classCDAccount

{

public:

CDAccount();

CDAccount(doublebal,doubleintRate,intT);

doubleInte

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

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1