=========================preview======================
(COMP151)comp151_99f_final_sol.pdf
Back to COMP151 Login to download
======================================================
COMP151FinalExam,Dec.151999 Grading:foreachofthethreekindsofpolymorphism,6pointscanbeearned: Modelanswersandgradinginstructions.
2pointsfor(moreorless)correctlynamingthekindofpolymorphism,and 4pointsforacorrectexample.
Question1(18points):Polymorphism
Thethreekindsofpolymorphismwehaveseenare Ofcourse,studentswillprobablycomeopwithmanydi.erentexamples,andthatisOK.
Forfunctionoverloadingandtemplates,itisOKiftheyonlygive2functionde.nitionsanda 1.Functionoverloading.Forexample:
templatede.nition,respectively,withoutcallingthefunction.
intmin(inta,intb){
Forthevirtualfunctionde.nition,itisnotsu.cientiftheyonlygiveabaseclassandaderived
if(a..b)returna.
classwithanoverloadedfunction.theusethroughapointerofreferencemustbegiventoo.
elsereturnb.
Substract2pointsfortheexampleofthevirtualfunctionmechanismiftheuseismissing.
}
stringmin(conststring&a,conststring&b){
if(a..b)returna.
elsereturnb.

}
//...
inti.min(14,67).
strings.min("Hello","World").

2.Thevirtualfunctionmechanism,combinedwithpointers/refencestobaseclasses.Forex-ample:

classB{
public:
virtualvoidf(){cout.."B::f()\n".}

}.
classD:publicB{
public:
virtualvoidf(){cout.."D::f()\n".}

}.
//...
B*bp.newD.
bp-.f()

3.Templates.ForExample:
template.classT.
Tmin(Ta,Tb){
if(a..b)returna.
elsereturnb.

}
//...
inti.min(14,67).
strings.min("Hello","World").

12
Question2(12points):ClassHierarchies
Note:theexampleofbadprogrammingstylethatwasmentionedinthetextoftheassignment referstothefunctionh().ThisfunctionisnotvirtualinthebaseclassTop,butitisoverridden inthederivedclassMiddleanyway(andmadevirtualtoo!).
Solution:
part1:containsanerror.ItisnotposibletodeclareanobjectoptypeTop,sincethatclass containsapurevirtualfunction.
part2:printsBottom::f()
part3:printsMiddle::f()(eventhoughmiddleiscopiedfrombottom,wedon'tgetpolymor-phismhere,sincewedon'tusepointersorreferences.)
part4:prints
Bottom::f()
Bottom::g()
Top::h()
(ThelastlinecanbeexplainedbythefactthatTop::h()isnotvirtual.)
part5:prints
Bottom::f()
Bottom::g()
Bottom::h()
(ThelastlinecanbeexplainedbythefactthatMiddle::h()hasbeenmadevirtual.)
part6:containsanerror:itisnotpossibletohaveapointertoaderivedclass(i.e.,aBottom*) pointtoabaseclass(i.e.,aMiddle.)
Grading:2pointsforeachcorrectpart.Forthepartsthatcontainanerror,theerrormustbe explained.give0pointsforthatpartotherwise.
Question3(16points):RelationshipsbetweenClasses
ThefourrelationshipsaretheISrelationship(alsocalledISAorIS-A),theHASrelationship,the OWNSrelationship,andtheUSESrelationship.
WespeakofanISrelationshipwhenoneclassisderivedfromanotherclass,orwhenaclass implementsaninterfaceclass.Thederivedclasscanbeusedanywherewherethebaseclassis expected(thesubstitionprinciple).
WespeakofaHASrelationshipwhenoneclassiscontainedintheother(asaclass-typemember). Thisisalsocalledaggregation.
WespeakofanOWNSrelationshipwhen(anobjectof)oneclassisontheheap,and(anobject of)theotherclassisresponsiblef