Instructor’s Manual a
for
C++ How to Program, 3/e
a a a a
Deitel, Deitel & Nieto
a a a
©2000.a Deitela &a Associates,a Inc.a anda Prenticea Hall.a Alla Rightsa Reserved.
,C++ How to Program: Third Edition Instructor’s
a a a a a a a
Manual Contents a
Preface iii
Chaptera 1 IntroductionatoaComputersaandaC++aProgramming:aSolutions 1
Chaptera 2 Controla Structures:a Solutions 15
Chaptera 3 Functions:aSolutions 66
Chaptera 4 ArraysaSolutions: 120
Chaptera 5 Pointersa anda Strings:a Solutions 170
Chaptera 6 ClassesaandaDataaAbstraction:aSolutions 235
Chaptera 7 Classes:a Parta II:a Solutions 264
Chaptera 8 Operatora Overloading:a Solutions 276
Chaptera 9 Inheritance:a Solutions 299
Chaptera 10 VirtualaFunctionsaandaPolymorphism:aSolutions 318
Chaptera 11 C++a Streama Input/Output:a Solutions 333
Chaptera 12 Templates:aSolutions 348
Chaptera 13 ExceptionaHandling:aSolutions 359
Chaptera 14 FileaProcessing:aSolutions 370
Chaptera 15 Dataa Structures:a Solutions 390
Chaptera 16 Bits,aCharacters,aStringsaandaStructures:aSolutions 498
Chaptera 17 TheaPreprocessor:aSolutions 524
Chaptera 18 Ca Legacya Codea Topics:a Solutions 531
Chaptera 19 ClassastringaandaStringaStreamaProcessing:aSolutions 541
Chaptera 20 StandardaTemplateaLibrarya(STL):aSolutions 559
Chaptera 21 StandardaC++aLanguageaAdditions:aSolutions 565
Appendix C++aMultimediaaCyberaClassroomaSolutionsaProvidedaonaCD 573
©2000.aDeitela&aAssociates,aInc.aandaPrenticeaHall.aAllaRightsaReserved.
,Preface
Thanka youa fora consideringa and/ora adoptinga oura texta C++a Howa toa Program:a Thirda Edition.a Ifa youa havea nota reada
thea prefacea toa C++a Howa toa Program:a Thirda Edition,a pleasea doa so.a Thea prefacea containsa aa carefula walkthrougha ofa
theabook’sakeyafeatures,aincludingaouranewaUnifiedaModelingaLanguage™a (UML™)acaseastudy,awhichacarefullyaintrod
ucesa thea readera toa thea UMLa anda object-orienteda designa (OOD).a Studentsa area presenteda witha aa detaileda prob-
alema statementa anda guideda througha aa simplified,a UML-baseda object-orienteda designa process.a Thea completea 1000-
alinea C++a programa solutiona fora thea casea studya isa presenteda ina thea booka anda provideda ona thea CD-
ROMa ina thea backaofatheatextbook.
Weahaveaworkedahardatoaproduceaaatextbookaandaancillariesathataweahopeayouaandayourastudentsawillafindavalu-
aable.aTheafollowingaancillaryaresourcesaareaavailable:
• C++a Howa toa Program:a Thirda Edition’sa 250a programa examplesa area includeda ona thea CD-
ROMa ina thea backaofatheatextbook.aThisahelpsainstructorsapreparealecturesafasteraandahelpsastudentsamasteraC++.
aTheaexamplesa areaalsoaavailableaforadownloadaatawww.deitel.com.aWhenaextractingatheasourceacodeafrom
atheaZIPafile,a youa musta usea aa ZIP-
filea readera sucha asa WinZipa (www.winzip.com)a ora PKZIPa (www.pkware.com)athataunderstandsadirect
ories.aTheafileashouldabeaextractedaintoaaaseparateadirectorya(e.g.,acpphtp3e_examples).
• MicrosoftaVisualaC++a6aIntroductoryaEditionasoftwareaisaprovidedaonatheatextbook’saCD-ROM.aThisasoft-
a wareaallowsastudentsatoaedit,acompileaandadebugaC++aprograms.aWeahaveaprovidedaatanoachargeaaashortaVi-
a suala C++a 6a tutoriala (ina Adobea PDFa format)a ona oura Weba sitea (www.deitel.com).
• ThisaC++aHowatoaProgram:aThirdaEditionaInstructor’saManualaonaCDacontainsaanswersatoamostaofatheaex-
aercisesainatheatextbook.aTheaprogramsaareaseparatedaintoadirectoriesabyachapteraandaexerciseanumber.
• Thea optionalaC++a Multimediaa Cybera Classroom:a Thirda EditionaisaanainteractiveamultimediaaCDaversionaofat
heabookaforaWindows.aItsafeaturesaincludeaaudioawalkthroughsaofaprograms,asectionareviewaquestionsa(whichaar
eaavailableaonlyaonatheaC++a MultimediaaCyberaClassroom:a Thirda Edition),aaatext-
searchaengine,atheaabilityatoaexecuteaexampleaprograms,aandamore.aTheaCyberaClassrooma helpsastudentsagetamo
reaoutaofatheiracourses.aTheaCyberaClassroomaisaalsoausefulaforastudentsawhoamissaaalectureaandahaveatoacatchaup
aquickly.a TheaCyberaClassroomaisaavailableaasaaastand-
aloneaproducta(seeathealastafewapagesaofatheatextbookaforatheaISBNanumber)a orabundledawithatheatextbooka(ataaad
iscount)ainaaaproductacalledaTheaCompleteaC++aTrainingaCourse:aThirdaEditiona(ISBN#a0-13-089563-6).
• CompanionaWebasitea(www.prenhall.com/deitel)aprovidesainstructoraandastudentaresources.aIn-
a structora resourcesa includea textbooka appendicesa (e.g.,a Appendixa D,a "C++a Interneta anda Weba Resources")a and
aaasyllabusamanageraforalessonaplanning.aStudentaresourcesaincludeachapteraobjectives,atrue/falseaquestions,achap
tera highlights,a referencea materialsa anda aa messagea board.
• Customizablea Powerpointa Instructora Lecturea Notes,a witha manya completea featuresa includinga sourcea code,a
andakeyadiscussionapointsaforaeachaprogramaandamajoraillustration.aThesealectureanotesaareaavailableaforain-
astructorsa anda studentsa ata noa chargea ata oura Weba sitea www.deitel.com.
• Laba Manuala (availablea Springa 2001)—aa for-salea itema containinga closed-laba sessions.
Wea woulda sincerelya appreciatea youra questions,a comments,a criticismsa anda correctionsa addresseda toa usa at:
©2000.a Deitela &a Associates,a Inc.a anda Prenticea Hall.a Alla Rightsa Reserved.
, IV
Weawillarespondaimmediately.aPleaseareadathealatestacopyaofathea DeitelaBuzza(publishedaeveryaAprilaandaNovembe
r)aforainformationaonaforthcomingaDeitelapublications,aancillaries,aproductaoptionsaandaorderingainfor-
amation.a Toa receivea thea DeitelaBuzz,a pleasea contacta Jenniea Burgera ().
Watcha oura Deitela &a Associates,a Inc.a Weba sitea (www.deitel.com)a anda oura Prenticea Halla Weba site
(www.prenhall.com/deitel)a fora thea latesta publicationaupdates.
Wea woulda likea toa thanka thea extraordinarya teama ofa publishinga professionalsa ata Prenticea Halla whoa madea C++a
HowatoaProgram:aThirdaEditionaandaitsaancillariesapossible.aOuraComputeraScienceaeditor,aPetraaRecter,aworkedaclosel
ya witha usa toa ensurea thea timelya availabilitya anda professionala qualitya ofa thesea ancillaries.
Weawouldaalsoalikeatoathankatwoaofaourastudentainterns—
AftabaBukharia(aaComputeraScienceamajoraataBostonaUniversity)aandaJasonaRosenfelda(aaComputeraScienceamajoraataN
orthwesternaUniversity)aforatheiraassistanceainapreparingathisaInstructor’saManual.
HarveyaM.aDeitela
PaulaJ.aDeitel
©2000.a Deitela &a Associates,a Inc.a anda Prenticea Hall.a Alla Rightsa Reserved.
for
C++ How to Program, 3/e
a a a a
Deitel, Deitel & Nieto
a a a
©2000.a Deitela &a Associates,a Inc.a anda Prenticea Hall.a Alla Rightsa Reserved.
,C++ How to Program: Third Edition Instructor’s
a a a a a a a
Manual Contents a
Preface iii
Chaptera 1 IntroductionatoaComputersaandaC++aProgramming:aSolutions 1
Chaptera 2 Controla Structures:a Solutions 15
Chaptera 3 Functions:aSolutions 66
Chaptera 4 ArraysaSolutions: 120
Chaptera 5 Pointersa anda Strings:a Solutions 170
Chaptera 6 ClassesaandaDataaAbstraction:aSolutions 235
Chaptera 7 Classes:a Parta II:a Solutions 264
Chaptera 8 Operatora Overloading:a Solutions 276
Chaptera 9 Inheritance:a Solutions 299
Chaptera 10 VirtualaFunctionsaandaPolymorphism:aSolutions 318
Chaptera 11 C++a Streama Input/Output:a Solutions 333
Chaptera 12 Templates:aSolutions 348
Chaptera 13 ExceptionaHandling:aSolutions 359
Chaptera 14 FileaProcessing:aSolutions 370
Chaptera 15 Dataa Structures:a Solutions 390
Chaptera 16 Bits,aCharacters,aStringsaandaStructures:aSolutions 498
Chaptera 17 TheaPreprocessor:aSolutions 524
Chaptera 18 Ca Legacya Codea Topics:a Solutions 531
Chaptera 19 ClassastringaandaStringaStreamaProcessing:aSolutions 541
Chaptera 20 StandardaTemplateaLibrarya(STL):aSolutions 559
Chaptera 21 StandardaC++aLanguageaAdditions:aSolutions 565
Appendix C++aMultimediaaCyberaClassroomaSolutionsaProvidedaonaCD 573
©2000.aDeitela&aAssociates,aInc.aandaPrenticeaHall.aAllaRightsaReserved.
,Preface
Thanka youa fora consideringa and/ora adoptinga oura texta C++a Howa toa Program:a Thirda Edition.a Ifa youa havea nota reada
thea prefacea toa C++a Howa toa Program:a Thirda Edition,a pleasea doa so.a Thea prefacea containsa aa carefula walkthrougha ofa
theabook’sakeyafeatures,aincludingaouranewaUnifiedaModelingaLanguage™a (UML™)acaseastudy,awhichacarefullyaintrod
ucesa thea readera toa thea UMLa anda object-orienteda designa (OOD).a Studentsa area presenteda witha aa detaileda prob-
alema statementa anda guideda througha aa simplified,a UML-baseda object-orienteda designa process.a Thea completea 1000-
alinea C++a programa solutiona fora thea casea studya isa presenteda ina thea booka anda provideda ona thea CD-
ROMa ina thea backaofatheatextbook.
Weahaveaworkedahardatoaproduceaaatextbookaandaancillariesathataweahopeayouaandayourastudentsawillafindavalu-
aable.aTheafollowingaancillaryaresourcesaareaavailable:
• C++a Howa toa Program:a Thirda Edition’sa 250a programa examplesa area includeda ona thea CD-
ROMa ina thea backaofatheatextbook.aThisahelpsainstructorsapreparealecturesafasteraandahelpsastudentsamasteraC++.
aTheaexamplesa areaalsoaavailableaforadownloadaatawww.deitel.com.aWhenaextractingatheasourceacodeafrom
atheaZIPafile,a youa musta usea aa ZIP-
filea readera sucha asa WinZipa (www.winzip.com)a ora PKZIPa (www.pkware.com)athataunderstandsadirect
ories.aTheafileashouldabeaextractedaintoaaaseparateadirectorya(e.g.,acpphtp3e_examples).
• MicrosoftaVisualaC++a6aIntroductoryaEditionasoftwareaisaprovidedaonatheatextbook’saCD-ROM.aThisasoft-
a wareaallowsastudentsatoaedit,acompileaandadebugaC++aprograms.aWeahaveaprovidedaatanoachargeaaashortaVi-
a suala C++a 6a tutoriala (ina Adobea PDFa format)a ona oura Weba sitea (www.deitel.com).
• ThisaC++aHowatoaProgram:aThirdaEditionaInstructor’saManualaonaCDacontainsaanswersatoamostaofatheaex-
aercisesainatheatextbook.aTheaprogramsaareaseparatedaintoadirectoriesabyachapteraandaexerciseanumber.
• Thea optionalaC++a Multimediaa Cybera Classroom:a Thirda EditionaisaanainteractiveamultimediaaCDaversionaofat
heabookaforaWindows.aItsafeaturesaincludeaaudioawalkthroughsaofaprograms,asectionareviewaquestionsa(whichaar
eaavailableaonlyaonatheaC++a MultimediaaCyberaClassroom:a Thirda Edition),aaatext-
searchaengine,atheaabilityatoaexecuteaexampleaprograms,aandamore.aTheaCyberaClassrooma helpsastudentsagetamo
reaoutaofatheiracourses.aTheaCyberaClassroomaisaalsoausefulaforastudentsawhoamissaaalectureaandahaveatoacatchaup
aquickly.a TheaCyberaClassroomaisaavailableaasaaastand-
aloneaproducta(seeathealastafewapagesaofatheatextbookaforatheaISBNanumber)a orabundledawithatheatextbooka(ataaad
iscount)ainaaaproductacalledaTheaCompleteaC++aTrainingaCourse:aThirdaEditiona(ISBN#a0-13-089563-6).
• CompanionaWebasitea(www.prenhall.com/deitel)aprovidesainstructoraandastudentaresources.aIn-
a structora resourcesa includea textbooka appendicesa (e.g.,a Appendixa D,a "C++a Interneta anda Weba Resources")a and
aaasyllabusamanageraforalessonaplanning.aStudentaresourcesaincludeachapteraobjectives,atrue/falseaquestions,achap
tera highlights,a referencea materialsa anda aa messagea board.
• Customizablea Powerpointa Instructora Lecturea Notes,a witha manya completea featuresa includinga sourcea code,a
andakeyadiscussionapointsaforaeachaprogramaandamajoraillustration.aThesealectureanotesaareaavailableaforain-
astructorsa anda studentsa ata noa chargea ata oura Weba sitea www.deitel.com.
• Laba Manuala (availablea Springa 2001)—aa for-salea itema containinga closed-laba sessions.
Wea woulda sincerelya appreciatea youra questions,a comments,a criticismsa anda correctionsa addresseda toa usa at:
©2000.a Deitela &a Associates,a Inc.a anda Prenticea Hall.a Alla Rightsa Reserved.
, IV
Weawillarespondaimmediately.aPleaseareadathealatestacopyaofathea DeitelaBuzza(publishedaeveryaAprilaandaNovembe
r)aforainformationaonaforthcomingaDeitelapublications,aancillaries,aproductaoptionsaandaorderingainfor-
amation.a Toa receivea thea DeitelaBuzz,a pleasea contacta Jenniea Burgera ().
Watcha oura Deitela &a Associates,a Inc.a Weba sitea (www.deitel.com)a anda oura Prenticea Halla Weba site
(www.prenhall.com/deitel)a fora thea latesta publicationaupdates.
Wea woulda likea toa thanka thea extraordinarya teama ofa publishinga professionalsa ata Prenticea Halla whoa madea C++a
HowatoaProgram:aThirdaEditionaandaitsaancillariesapossible.aOuraComputeraScienceaeditor,aPetraaRecter,aworkedaclosel
ya witha usa toa ensurea thea timelya availabilitya anda professionala qualitya ofa thesea ancillaries.
Weawouldaalsoalikeatoathankatwoaofaourastudentainterns—
AftabaBukharia(aaComputeraScienceamajoraataBostonaUniversity)aandaJasonaRosenfelda(aaComputeraScienceamajoraataN
orthwesternaUniversity)aforatheiraassistanceainapreparingathisaInstructor’saManual.
HarveyaM.aDeitela
PaulaJ.aDeitel
©2000.a Deitela &a Associates,a Inc.a anda Prenticea Hall.a Alla Rightsa Reserved.