Instructor’s Manual m
for
C++ How to Program, 3/e
m m m m
Deitel, Deitel & Nieto
m m m
©2000.m Deitelm &m Associates,m Inc.m and m Prenticem Hall.m Allm Rightsm Reserved.
,C++ How to Program: Third Edition Instructor’s
m m m m m m
Manual Contents
m m
Preface iii
Chapter m 1 IntroductionmtomComputers mandmC++ mProgramming: mSolutions 1
Chapter m 2 Controlm Structures:m Solutions 15
Chapter m 3 Functions: mSolutions 66
Chapter m 4 Arrays mSolutions: 120
Chapter m 5 Pointersm andm Strings:m Solutions 170
Chapter m 6 Classes mandmDatamAbstraction: mSolutions 235
Chapter m 7 Classes:mPartmII:mSolutions 264
Chapter m 8 Operator m Overloading: m Solutions 276
Chapter m 9 Inheritance: mSolutions 299
Chapter m 10 VirtualmFunctions mandmPolymorphism: mSolutions 318
Chapter m 11 C++m Streamm Input/Output:m Solutions 333
Chapter m 12 Templates: mSolutions 348
Chapter m 13 ExceptionmHandling: mSolutions 359
Chapter m 14 FilemProcessing: mSolutions 370
Chapter m 15 Datam Structures:m Solutions 390
Chapter m 16 Bits,mCharacters,mStrings mandmStructures: mSolutions 498
Chapter m 17 ThemPreprocessor: mSolutions 524
Chapter m 18 Cm Legacy m Codem Topics:m Solutions 531
Chapter m 19 Class mstringmandmStringmStreammProcessing: mSolutions 541
Chapter m 20 StandardmTemplate mLibrary m(STL): mSolutions 559
Chapter m 21 StandardmC++ mLanguage mAdditions: mSolutions 565
Appendix C++ mMultimediamCyber mClassroommSolutions mProvidedmonmCD 573
©2000.mDeitelm&mAssociates,mInc.mand mPrenticemHall.mAllmRights mReserved.
,Preface
Thank m you m form consideringm and/orm adoptingm ourm text m C++m Howm to m Program:m Third m Edition.m If m you m havem no
t m read mthem prefacemto m C++m Howm tom Program:m Thirdm Edition,m pleasem dom so.m Them prefacem containsm am carefulm w
alkthrough m of mthembook’smkey mfeatures,mincludingmourmnewmUnified mModelingmLanguage™m (UML™)mcasemstudy,mw
hich mcarefully mintroducesm them readerm to m them UMLmand m object-
oriented m design m (OOD). m Studentsm arem presented m with ma m detailed m prob-
mlem m statement m and m guided m through m a m simplified,m UML-based m object-
oriented m design m process.m Them completem 1000-
mline m C++m program m solution m form them case m study m ism presentedm in m them bookm andm provided m on m the m CD-
ROM m in m them back mof mthemtextbook.
Wemhavemworked mhard mto mproducema mtextbook mand mancillariesmthat mwemhopemyou mand myourmstudentsmwillmfind m
valu-mable.mThemfollowingmancillary mresourcesmaremavailable:
• C++m Howm to m Program:m Third m Edition’sm 250m programm examplesm arem included m on m them CD-
ROM m in m them back mof mthemtextbook.mThismhelpsminstructorsmpreparemlecturesmfastermand mhelpsmstudentsmmast
ermC++.mThemexamplesm aremalso mavailablemformdownload mat mwww.deitel.com.mWhen mextractingmthemsou
rcemcodemfrom mthemZIPmfile,m you m must m usem a m ZIP-
filem readerm such m asm WinZip m (www.winzip.com)m orm PKZIPm (www.pkware.com)mthat munderstandsmd
irectories.mThemfilemshould mbemextracted minto ma mseparatemdirectory m(e.g.,mcpphtp3e_examples).
• Microsoft mVisual mC++m6 mIntroductorymEdition msoftwaremismprovided mon mthemtextbook’smCD-
ROM.mThismsoft-
m ware mallowsmstudentsmto medit,mcompile mand mdebugmC++mprograms.mWe mhave mprovided mat mno mcharge ma mshor
tmVi-m sualm C++m 6 m tutorialm (in m Adobem PDFm format)m on m ourm Web m sitem (www.deitel.com).
• ThismC++mHowmto mProgram:mThird mEdition mInstructor’smManualmon mCDmcontainsmanswersmto mmost mof mtheme
x-mercisesmin mthemtextbook.mThemprogramsmaremseparatedmintomdirectoriesmby mchaptermand mexercisemnumber.
• Them optionalmC++m Multimedia m Cyberm Classroom:m Third m Edition misman minteractivemmultimedia mCDmversion
mof mthe mbook mformWindows.mItsmfeaturesminclude maudio mwalkthroughsmof mprograms,msection mreviewmquestions
m(which mare mavailable monly mon mthe mC++ mMultimedia mCybermClassroom:m Third m Edition),ma mtext-
search mengine,mthemability mto mexecutemexamplemprograms,mand mmore.mThemCybermClassroomm helpsmstudentsm
getmmoremout mof mtheirmcourses.mThemCybermClassroommismalso musefulmformstudentsmwho mmissma mlecturemand mh
avemto mcatch mup mquickly.mThemCybermClassroommismavailablemasma mstand-
alonemproduct m(seemthemlast mfewmpagesmof mthemtextbook mformthemISBNmnumber)m ormbundled mwith mthemtextboo
k m(at ma mdiscount)min ma mproduct mcalled mThemCompletemC++mTraining mCourse:mThirdmEdition m(ISBN# m0-13-
089563-6).
• Companion mWeb msitem(www.prenhall.com/deitel)mprovidesminstructormand mstudent mresources.mIn-
m structorm resourcesm include m textbook mappendicesm (e.g.,m Appendix m D,m "C++m Internet mand m Web m Resources
")m and ma msyllabusmmanagermformlesson mplanning.mStudent mresourcesmincludemchaptermobjectives,mtrue/falsemqu
estions,mchapterm highlights,m referencem materialsm and m a m messagem board.
• Customizablem Powerpointm Instructorm Lecturem Notes,m with m many m completem featuresm includingm sourcem c
ode,mand mkey mdiscussion mpointsmformeach mprogram mand mmajormillustration.mThesemlecturemnotesmaremavailable
mformin-mstructorsm and m studentsm at m no m charge m at m ourm Web m site m www.deitel.com.
• Lab m Manual m (availablem Springm 2001)—a m for-salem item m containingm closed-lab m sessions.
©2000.m Deitelm &m Associates,m Inc.m andm Prenticem Hall.m Allm Rightsm Reserv
ed.
, Wem would m sincerelym appreciatem yourm questions,m comments,m criticismsm and m correctionsm addressed m to m usm at:
©2000.m Deitelm &m Associates,m Inc.m andm Prenticem Hall.m Allm Rightsm Reserv
ed.
for
C++ How to Program, 3/e
m m m m
Deitel, Deitel & Nieto
m m m
©2000.m Deitelm &m Associates,m Inc.m and m Prenticem Hall.m Allm Rightsm Reserved.
,C++ How to Program: Third Edition Instructor’s
m m m m m m
Manual Contents
m m
Preface iii
Chapter m 1 IntroductionmtomComputers mandmC++ mProgramming: mSolutions 1
Chapter m 2 Controlm Structures:m Solutions 15
Chapter m 3 Functions: mSolutions 66
Chapter m 4 Arrays mSolutions: 120
Chapter m 5 Pointersm andm Strings:m Solutions 170
Chapter m 6 Classes mandmDatamAbstraction: mSolutions 235
Chapter m 7 Classes:mPartmII:mSolutions 264
Chapter m 8 Operator m Overloading: m Solutions 276
Chapter m 9 Inheritance: mSolutions 299
Chapter m 10 VirtualmFunctions mandmPolymorphism: mSolutions 318
Chapter m 11 C++m Streamm Input/Output:m Solutions 333
Chapter m 12 Templates: mSolutions 348
Chapter m 13 ExceptionmHandling: mSolutions 359
Chapter m 14 FilemProcessing: mSolutions 370
Chapter m 15 Datam Structures:m Solutions 390
Chapter m 16 Bits,mCharacters,mStrings mandmStructures: mSolutions 498
Chapter m 17 ThemPreprocessor: mSolutions 524
Chapter m 18 Cm Legacy m Codem Topics:m Solutions 531
Chapter m 19 Class mstringmandmStringmStreammProcessing: mSolutions 541
Chapter m 20 StandardmTemplate mLibrary m(STL): mSolutions 559
Chapter m 21 StandardmC++ mLanguage mAdditions: mSolutions 565
Appendix C++ mMultimediamCyber mClassroommSolutions mProvidedmonmCD 573
©2000.mDeitelm&mAssociates,mInc.mand mPrenticemHall.mAllmRights mReserved.
,Preface
Thank m you m form consideringm and/orm adoptingm ourm text m C++m Howm to m Program:m Third m Edition.m If m you m havem no
t m read mthem prefacemto m C++m Howm tom Program:m Thirdm Edition,m pleasem dom so.m Them prefacem containsm am carefulm w
alkthrough m of mthembook’smkey mfeatures,mincludingmourmnewmUnified mModelingmLanguage™m (UML™)mcasemstudy,mw
hich mcarefully mintroducesm them readerm to m them UMLmand m object-
oriented m design m (OOD). m Studentsm arem presented m with ma m detailed m prob-
mlem m statement m and m guided m through m a m simplified,m UML-based m object-
oriented m design m process.m Them completem 1000-
mline m C++m program m solution m form them case m study m ism presentedm in m them bookm andm provided m on m the m CD-
ROM m in m them back mof mthemtextbook.
Wemhavemworked mhard mto mproducema mtextbook mand mancillariesmthat mwemhopemyou mand myourmstudentsmwillmfind m
valu-mable.mThemfollowingmancillary mresourcesmaremavailable:
• C++m Howm to m Program:m Third m Edition’sm 250m programm examplesm arem included m on m them CD-
ROM m in m them back mof mthemtextbook.mThismhelpsminstructorsmpreparemlecturesmfastermand mhelpsmstudentsmmast
ermC++.mThemexamplesm aremalso mavailablemformdownload mat mwww.deitel.com.mWhen mextractingmthemsou
rcemcodemfrom mthemZIPmfile,m you m must m usem a m ZIP-
filem readerm such m asm WinZip m (www.winzip.com)m orm PKZIPm (www.pkware.com)mthat munderstandsmd
irectories.mThemfilemshould mbemextracted minto ma mseparatemdirectory m(e.g.,mcpphtp3e_examples).
• Microsoft mVisual mC++m6 mIntroductorymEdition msoftwaremismprovided mon mthemtextbook’smCD-
ROM.mThismsoft-
m ware mallowsmstudentsmto medit,mcompile mand mdebugmC++mprograms.mWe mhave mprovided mat mno mcharge ma mshor
tmVi-m sualm C++m 6 m tutorialm (in m Adobem PDFm format)m on m ourm Web m sitem (www.deitel.com).
• ThismC++mHowmto mProgram:mThird mEdition mInstructor’smManualmon mCDmcontainsmanswersmto mmost mof mtheme
x-mercisesmin mthemtextbook.mThemprogramsmaremseparatedmintomdirectoriesmby mchaptermand mexercisemnumber.
• Them optionalmC++m Multimedia m Cyberm Classroom:m Third m Edition misman minteractivemmultimedia mCDmversion
mof mthe mbook mformWindows.mItsmfeaturesminclude maudio mwalkthroughsmof mprograms,msection mreviewmquestions
m(which mare mavailable monly mon mthe mC++ mMultimedia mCybermClassroom:m Third m Edition),ma mtext-
search mengine,mthemability mto mexecutemexamplemprograms,mand mmore.mThemCybermClassroomm helpsmstudentsm
getmmoremout mof mtheirmcourses.mThemCybermClassroommismalso musefulmformstudentsmwho mmissma mlecturemand mh
avemto mcatch mup mquickly.mThemCybermClassroommismavailablemasma mstand-
alonemproduct m(seemthemlast mfewmpagesmof mthemtextbook mformthemISBNmnumber)m ormbundled mwith mthemtextboo
k m(at ma mdiscount)min ma mproduct mcalled mThemCompletemC++mTraining mCourse:mThirdmEdition m(ISBN# m0-13-
089563-6).
• Companion mWeb msitem(www.prenhall.com/deitel)mprovidesminstructormand mstudent mresources.mIn-
m structorm resourcesm include m textbook mappendicesm (e.g.,m Appendix m D,m "C++m Internet mand m Web m Resources
")m and ma msyllabusmmanagermformlesson mplanning.mStudent mresourcesmincludemchaptermobjectives,mtrue/falsemqu
estions,mchapterm highlights,m referencem materialsm and m a m messagem board.
• Customizablem Powerpointm Instructorm Lecturem Notes,m with m many m completem featuresm includingm sourcem c
ode,mand mkey mdiscussion mpointsmformeach mprogram mand mmajormillustration.mThesemlecturemnotesmaremavailable
mformin-mstructorsm and m studentsm at m no m charge m at m ourm Web m site m www.deitel.com.
• Lab m Manual m (availablem Springm 2001)—a m for-salem item m containingm closed-lab m sessions.
©2000.m Deitelm &m Associates,m Inc.m andm Prenticem Hall.m Allm Rightsm Reserv
ed.
, Wem would m sincerelym appreciatem yourm questions,m comments,m criticismsm and m correctionsm addressed m to m usm at:
©2000.m Deitelm &m Associates,m Inc.m andm Prenticem Hall.m Allm Rightsm Reserv
ed.