=========================preview======================
(COMP151)comp151_99f_final.pdf
Back to COMP151 Login to download
======================================================
TheHongKongUniversityofScience&Technology
COMP151:Object-OrientedProgramming
FallSemester,1999
FinalExamination
December15,1999
Studentname: Studentid: Department: Labsection:
Instructions: 1Thereare6problemson10pages.Checkthatyouhaveall10pages. 2Writeyourname,studentid,department,andlabsectionnumberonthispage. 3Writeyourstudentidoneachofthefollowingpages. 4Answerallquestionsinthespaceprovided.Roughworkshouldbedoneonlyonthe
backpages,andcrossedoutwithadiagonallineafterwardstoshowthatitisnot
partofyouranswer. 5Leaveallpagesstapledtogether. 6Theexaminationwilllastfor180minutes. 7Stopwritingimmediatelywhenthetimeisup,andremainseatedinsilenceuntilall
workhasbeencollected. 8Youarenotallowedtoleaveduringthe.rsthalfhourandthelasthalfhourofthe
exam.
Forgradingpurposesonly
Problem1 /18
Problem2 /12
Problem3 /16
Problem4 /12
Problem5 /21
Problem6 /21

TOTAL: /100
1.(18points)Polymorphism
C++supportsthreedi.erentkindsofpolymorphism.Describethosethreekindsofpolymor-phism,andgivesmallexamplesinC++codetoillustratethem.Yourexamplesneednotbe completeworkingprograms.youonlyhavetogivetheessentialpartsthatillustrateyourpoint.
2.(12points)ClassHierarchies Theprogrambelowcontainsaclasshierarchyofthreeclasses:Top(thebaseclass),Middle (derivedfromTop,anditselfabaseclassforBottom),andBottom(derivedfromMiddle).The classde.nitionsarelegalC++andcompilewithoutanyerrors.Theydo,however,containatyp-icalexampleofverybadprogrammingstyle,whichleadstosurprisingpolymorphicbehaviour.
Thecodecontinuesonthenextpage.Themainfunctionconsistsofsixpartsandareturn statement.EachpartiseithercompletelycorrectC++,orcontainsoneormoreerrors.
Forthepartsthatarecorrect,writetheoutputtotherightofthecodeinthemainfunction.
Forthepartsthatcontainoneormoreerrors,brie.yexplaintheerrorstotherightofthecode inthemainfunction.Youdon'thavetogiveanyoutputforthepartsthatcontainerrors.
#include.iostream.
usingstd::cout.
classTop{
public:
virtual~Top(){}
virtualvoidf()const.0.
virtualvoidg()const{cout.."Top::g()\n".}
voidh()const{cout.."Top::h()\n".}

}.
classMiddle:publicTop{
public:
virtual~Middle(){}
virtualvoidf()const{cout.."Middle::f()\n".}
voidg()const{cout.."Middle::g()\n".}
virtualvoidh()const{cout.."Middle::h()\n".}

}.
classBottom:publicMiddle{
public:
virtual~Bottom(){}
voidf()const{cout.."Bottom::f()\n".}
virtualvoidg()const{cout.."Bottom::g()\n".}
virtualvoidh()const{cout.."Bottom::h()\n".}

}.
Studentid:
(Problem2,continued.)
intmain(){ //part1 Toptop. top.g().
//part2 Bottombottom. bottom.f().
//part3 Middlemiddle.bottom. middle.f().
//part4 Top*tp.newBottom. tp-.f(). tp-.g(). tp-.h().
//part5 Middle*mp.newBottom. mp-.f(). mp-.g(). mp-.h().
//part6 Bottom*bp.newMiddle bp-.f(). bp-.g(). bp-.h().
return0.
}
3.(16points)RelationshipsbetweenClasses
Namethefourkindsofrelationshipsbetweenclassesthatwehavediscussedinthecourse,and giveanexplanationofeachofthefourrelationships.
4.(12points)VirtualDestruct