《面向对象程序设计英文教学课件:09_Case Study》由会员分享,可在线阅读,更多相关《面向对象程序设计英文教学课件:09_Case Study(52页珍藏版)》请在金锄头文库上搜索。
1、Object-Oriented Programming & C+Object-Oriented Programming & C+College of Computer Science, CQU09 Case StudyObject-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case Study1OutlinepArrayListpComposition(Amethodtorelateclasses)pImplementStackusingArrayList(Composition)pImpl
2、ementQueueusingArrayList(Composition)Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case Study2Knowledge PointspBook:AcceleratedC+,Chapter0-12pBook:数据结构(C语言版),清华大学出版社,严蔚敏,Chapter1-3Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case S
3、tudyArray ListpImplementasimplearraylistusingC+.pProperties:nDataelementsstoredinthelistnSizeofthedataelementsstoredinthelistnSizeoftheallocatedmemoryforthelistpMethods:nInitializingofthelistnInsertanelementtothelistnRemoveanelementfromthelistnGetanelementfromthelistnOutputthelist3Object-Oriented Pr
4、ogramming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray ListpArrayList.hClassdeclarationpArrayList.cppClassimplementationpmain.cppTestcode4Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class declaration)typedefintElemType;c
5、lassArrayListprivate:ElemType*elems;/dataelementsintsizeOfElems;/sizeofthedataelementsintsizeOfAllocatedMemory;/sizeofallocatedmemory/memoryincrementusedforreallocatingmemorystaticconstintINC=10;/reallocatesmemorywhenthereisnoenoughspacetostore/elementsvoidreallocate();5Object-Oriented Programming &
6、 C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class declaration)public:ArrayList(intsize=0);/constructorArrayList(constArrayList&list);/copyconstructorArrayList();/destructor/returnsthesizeofdataelementsinthislistintsize()const;/addsanelementtothelistatthepositionindicatedb
7、ytheindexvoidadd(intindex,ElemTypee);/addsanelementtotheendofthelistvoidadd(ElemTypee);6Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case Study/removesanelementatthespecifiedindexinthislistvoidremove(intindex);/removesthefirstelementofthelistvoidremove();/removesal
8、loftheelementsfromthelistvoidclear();/returnstheelementatthespecifiedindexinthislistElemType&operator(intindex);/assignsvaluesofotherlisttothislistArrayList&operator=(constArrayList&list);7Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class decl
9、aration)/outputsthelisttoastreamfriendostream&operator(ostream&os,constArrayList&list);8Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyDiscussion(Before implementation)staticconstintINC=10;pWhyisINCstaticandconst?intsize()const;pWhyissize()const?ArrayList(c
10、onstArrayList&list);ArrayList&operator=(constArrayList&list);friendostream&operator(ostream&os,constArrayList&list);pWhyislistaconstreference?9Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class implementation)ArrayList:ArrayList(intsize)elems=n
11、ewElemTypesize;sizeOfElems=0;sizeOfAllocatedMemory=size;10Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class implementation)ArrayList:ArrayList(constArrayList&list)elems=newElemTypelist.sizeOfAllocatedMemory;sizeOfAllocatedMemory=list.sizeOfAll
12、ocatedMemory;sizeOfElems=list.sizeOfElems;for(inti=0;ilist.sizeOfElems;i+)elemsi=list.elemsi;11Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class implementation)ArrayList:ArrayList()deleteelems;12Object-Oriented Programming & C+Object-Oriented
13、Programming & C+09 Case Study09 Case StudyArray List(Class implementation)intArrayList:size()constreturnsizeOfElems;13Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class implementation)voidArrayList:reallocate()intnewSizeOfAllocatedMemory=sizeOf
14、AllocatedMemory+INC;ElemType*newElems=newElemTypenewSizeOfAllocatedMemory;for(inti=0;isizeOfElems;i+)newElemsi=elemsi;deleteelems;elems=newElems;sizeOfAllocatedMemory=newSizeOfAllocatedMemory;14Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class
15、 implementation)voidArrayList:add(intindex,ElemTypee)intnewSizeOfArray=sizeOfElems+1;if(sizeOfAllocatedMemory=index;i-)elemsi+1=elemsi;elemsindex=e;sizeOfElems=newSizeOfArray;15Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyDiscussion(After implementation)v
16、oidArrayList:add(intindex,ElemTypee)pIfindexsizeOfElemsorindexsizeOfElems|index0)throwIndexoutofbounds!;intnewSizeOfArray=sizeOfElems+1;if(sizeOfAllocatedMemory=index;i-)elemsi+1=elemsi;elemsindex=e;sizeOfElems=newSizeOfArray;17Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case
17、Study09 Case StudyArray List(Class implementation)voidArrayList:add(ElemTypee)add(this-sizeOfElems,e);18Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class implementation)voidArrayList:remove(intindex)intnewSizeOfElems=sizeOfElems-1;if(indexnewS
18、izeOfElems|index0)throwindexoutofbounds!;if(sizeOfElems=0)throwThereisnoelementtoremove!;for(inti=index;isizeOfElems-1|index0)throwIndexoutofbounds;returnelemsindex;22Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyArray List(Class implementation)ArrayList&A
19、rrayList:operator=(constArrayList&list)deleteelems;elems=newElemTypelist.sizeOfAllocatedMemory;sizeOfAllocatedMemory=list.sizeOfAllocatedMemory;sizeOfElems=list.sizeOfElems;for(inti=0;ilist.sizeOfElems;i+)elemsi=list.elemsi;return*this;23Object-Oriented Programming & C+Object-Oriented Programming &
20、C+09 Case Study09 Case StudyArray List(Class implementation)ostream&operator(ostream&os,constArrayList&list)for(inti=0;ilist.size();i+)coutlist.elemsit;returnos;24Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyTest CodetryArrayListl1;/insertselementstothefr
21、ontofl1l1.add(0,10);l1.add(0,20);l1.add(0,30);/addsanelementtotheendofl1l1.add(40);/displaysl1coutl1:l1endl;ArrayListl2(l1);/removesanelementatindex1l2.remove(1);/displaysl2coutl2:l2endl;25Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyTest Code/removesthef
22、irstelementl2.remove();/displaysl2coutl2:l2endl;ArrayListl3=l2;/displaysl3coutl3:l3endl;coutThesizeofl3isl3.size()endl;/returnsthefirstelementandassigns1000toitl30=1000;/displaysl3coutl3:l3endl;/clearl3l3.clear();26Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case
23、StudyTest Code/displaysl3coutl3:l3endl;l3.add(1,200);catch(char*e)coutException:eendl;27Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyResult28Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyDiscussion(After impleme
24、ntation)ElemType&operator(intindex);pWhydoesoperatorreturnareferencetoElemType?pIfitischangedtoElemTypeoperator(intindex),whatwillbehappen?29Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyCompositionpOneofthefundamentalactivitiesofanysoftwaresystemdesignise
25、stablishingrelationshipsbetweenclasses.pTwofundamentalwaystorelateclassesareinheritanceandcomposition.pCompositionisthebestwaytoachievecodereuse.pObject-orienteddesignprinciple:neveruseinheritanceforcodereuseunlesspolymorphismisrequired(Wewilldiscussthisprincipleinlatercourse)30Object-Oriented Progr
26、amming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyCompositionThefollowingcodesisanexampleofcompoitionclassApublic:methodOfA();classBprivate:Aa;public:methodOfB()a.methodOfA();31Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStackpComposit
27、esArrayListtoimplementStackpStackisakindoflistwhichis“lastin,firstout”pProperties:nAnarraylisttostoredatapMethods:nInitializingofthestacknPushanelementtothestacknPopanelementfromthestacknOutputthestack32Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStackpA
28、rrayList.hClassdeclarationofArrayListpArrayList.cppClassimplementationofArrayListpStack.hClassdeclarationofStackpStack.cppClassimplementationofStackpmain.cppTestcode33Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class declaration)classStackprivate:A
29、rrayListlist;/alisttostoredataelementpublic:Stack(intsize=0);/constructorStack(constStack&stack);/copyconstructorStack();/destructorintsize()const;/returnsthesizeofdataelementsinthestackvoidpush(ElemTypee);/pushesanelementtothestack34Object-Oriented Programming & C+Object-Oriented Programming & C+09
30、 Case Study09 Case StudyStack(Class declaration)/popsanelementfromthestackElemTypepop();/removesalloftheelementsfromthestackvoidclear();/assignsvaluesofotherstacktothisstackStack&operator=(constStack&stack);/outputsthestacktoastreamfriendostream&operator(ostream&os,constStack&stack);35Object-Oriente
31、d Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation)Stack:Stack(intsize):list(size)36Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation)Stack:Stack(constStack&stack)/callsoperator=oft
32、heArrayListlist=stack.list;37Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation)Stack:Stack()/donothing38Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation)intStack:siz
33、e()constreturnlist.size();39Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation)voidStack:push(ElemTypee)list.add(e);40Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation
34、)ElemTypeStack:pop()intindex=list.size()-1;ElemTypelast=listindex;list.remove(index);returnlast;41Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation)voidStack:clear()list.clear();42Object-Oriented Programming & C+Object-Oriented Progr
35、amming & C+09 Case Study09 Case StudyStack(Class implementation)Stack&Stack:operator=(constStack&stack)/callsoperator=oftheArrayListlist=stack.list;return*this;43Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyStack(Class implementation)ostream&operator(ostr
36、eam&os,constStack&stack)/callsoperatoroftheArrayListosstack.list;returnos;44Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyTest codeStacks1;s1.push(1);s1.push(2);s1.push(3);couts1:s1endl;ElemTypelast=s1.pop();coutlastelementofs1:lastendl;couts1:s1endl;Stack
37、s2(s1);s2.push(4);couts2:s2endl;Stacks3=s1;s3.pop();couts1:s1endl;couts2:s2endl;couts3:s3endl;45Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyResult46Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyDiscussion(After
38、 implementation)ElemTypepop();pCanthisbechangedto“ElemType&pop()”?Stack:Stack()pWhydoesthisfunctiondonothing?47Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyQueuepCompositesArrayListtoimplementStackpQueueisakindoflistwhichis“firstin,firstout”pProperties:nA
39、narraylisttostoredatapMethods:nInitializingofthequeuenAddanelementtotheendofthequeuenRemovethefirstelementofthequeuenOutputthequeue48Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyQueue(Class declaration)classQueueprivate:ArrayListlist;/alisttostoredatapubl
40、ic:Queue(intsize=0);/constructorQueue(constQueue&queue);/copyconstructorQueue();/destructorintsize()const;/returnsthesizeofdataelementsinthequeue49Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case StudyQueue(Class declaration)/addsanelementtotheendofthequeuevoidenq
41、ueue(ElemTypee);/removesthefirstelementofthequeueElemTypedequeue();/removesalloftheelementsfromthequeuevoidclear();/assignsvaluesofotherstacktothisqueueQueue&operator=(constQueue&queue);/outputsthequeuetoastreamfriendostream&operator(ostream&os,constQueue&queue);50Object-Oriented Programming & C+Object-Oriented Programming & C+09 Case Study09 Case Study51HomeworkImplementsQueuepArrayList.hClassdeclarationofArrayListpArrayList.cppClassimplementationofArrayListpQueue.hClassdeclarationofQueuepQueue.cppClassimplementationofQueue