版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
ObjectivesToknowtheUMLdiagram-classdiagramBeawareoftheeffectofconstructorsanddestructorinaclassTobeabletodefineconstructorsanddestructorsandusethemTo
understandthedefinitionofaclassfurther01UMLDiagram04CaseStudy03Destructors02Constructors01UMLDiagramProblem-SolvingCase
Study
1DefineaDateclasswiththeday,monthandyear.Requirement:InputthedataofanobjectOutputthedataoftheobjectReset(modify)thedataoftheobjectGetthedayoftheobjectGetthemonthoftheobjectdataabstractionData:year,month,day-intFunctions(operations):inputoutputresetgetDaygetMonthcheckvoidinput()voidoutput()voidreset()intgetDay()intgetMonth()boolcheck()Problem-SolvingUsingUMLClassDiagramThe
UnifiedModelingLanguage(UML)isageneral-purposedevelopmental,modelinglanguageinthefieldofsoftwareengineeringthatisintendedtoprovideastandardwaytovisualizethedesignasystem.TheUMLdiagramisoftenusedforobject-orienteddesign.The
UML
classdiagramisagraphicalnotationusedtoconstructandvisualizeobjectorientedsystems.AclassdiagramintheUMLisatypeofstaticstructurediagramthatdescribesthestructureofasystembyshowingthesystem’s:classestheirattributes(datamembersinC++)operations/methods(memberfunctionsinC++)therelationshipsamongobjectsProblem-SolvingUsingUMLClassDiagramEncapsulationdataabstractionDate-day:int-month:int-year:int-check():bool+input():void+output():void+reset():void+getDay():int+getMonth():intdatamembers(properties)memberfunctions(Operations)UMLanalysisclass_nameaccessspecifier(-,+)datamemberaccessspecifier(-,+)memberfunctionsclassdiagramdataabstractionData:year,month,day-intFunctions(operations):inputoutputresetgetDaygetMonthcheckvoidinput()voidoutput()voidreset()intgetDay()intgetMonth()boolcheck()intmain(){Datetoday;today.input();today.output();today.reset();cout<<"theDateis"<<today.getMonth()<<"-"<<today.getDay();return0;}Implementationclass
Date{public:voidinput();voidoutput();voidreset();intgetDay();intgetMonth();private:intday,month,year;boolcheck();};Date-day:int-month:int-year:int-check():bool+input():void+output():void+reset():void+getDay():int+getMonth():intInformationhidingimplementationbool
Date::check(){if(day<1||day>31||month<1||month>12||year<1){ cout<<"Invaliddate!\n";return
false;}else return
true;}void
Date::reset(){cout<<"Resetadate\n";input();}int
Date::getMonth(){returnmonth;}int
Date::getDay(){returnday;}void
Date::output(){cout<<year<<"-"<<month<<"-"<<day<<endl;}Implementationvoid
Date::input(){do{cout<<"Entertheyear,monthanddayofadate:\n";cin>>year>>month>>day;}while(!check());}Date-day:int-month:int-year:int-check():bool+input():void+output():void+reset():void+getDay():int+getMonth():int02ConstructorsConstructorsintmain(){Datetoday;today.input();today.output();today.reset();cout<<"theDateis"<<today.getMonth()<<"-"<<today.getDay();return0;}Allocatememoryandinitializedatamembersvoid
Date::input(){do{cout<<"Entertheyear,monthanddayofadate:\n";cin>>year>>month>>day;}while(!check());}ConstructorWhotoallocatememory?Howmuchtoallocatememoryforobject?Howtostoredataofanobject?ConstructorsForexample,Datetoday;Aconstructorisaspecialmemberfunctionthatisautomaticallycalledwheneveraclassobjectiscreated.Aconstructorisrecognizedbyhavingthesamenameas
theclassitself.DefinitionofConstructorsMemberfunctionItsnameisthesameasclass’snameNoreturntypewithinitsdeclaration/definitionNoreturnstatementwithinitsdefinitionclass
Date{public:Date();voidoutput();voidreset();intgetDay();intgetMonth();private:intday,month,year;boolcheck();};constructorofclassDateDate::Date(){do{cout<<"Entertheyear,monthanddayofadate:\n";cin>>year>>month>>day;}while(!check());}Usageof
Constructorsintmain(){Datetoday;Datemybirthday;}classDate{public:Date();//..};Whenaclasshasaconstructor,allobjectsofthatclasswillbeinitializedbyaconstructorcall.OverloadingConstructorsThereareafewconstructorsinaclass.Constructorsobeythesameoverloadingrulesasdootherfunctions.Aslongastheconstructorsdiffersufficientlyintheirparametertypes,thecompilercanselectthecorrectoneforeachuse.
OverloadingConstructorsclassDate{public:
Date(int,int,int);Date(int,int);Date(int);Date();Date(const*char);private:intday,month,year;};intmain(){Datetoday(4);Datejuly4(“July42020”);Datenow;}AfewconstructorsinaclassaredefinedDefaultConstructorsDefaultconstructorsaredefinedinthethreeways.class
Date{public:Date();……intgetMonth();private:intday,month,year;boolcheck();};class
Date{public:Date(int=2020,int=9,int=1);……intgetMonth();private:intday,month,year;boolcheck();};DefaultconstructorDate::Date(){do{ cin>>year>>month>>day;}while(!check());}Date::Date(int
y,int
m,int
d){year=y;month=m;day=d;if(!check())exit(1);}3.Theconstructoriswithdefaultparameters;1.Theconstructorisnotdefinedintheclass;2.Theconstructoriswithoutparameters;class
Date{public:Date();Date(int=2020,int=9,int=1);……intgetMonth();private:intday,month,year;boolcheck();};intmain(){Datetoday(2015);Datetomorrow;return0;}//errorDefaultConstructorsWhenaclasshasmorethanonedefaultconstructor,thisleadstoambiguouscalltooverloadedconstructors.03DestructorsDestructors(析構函數(shù))Adestructorisaspecialmemberfunctionthatisautomaticallyinvokedwheneveraclassobjectgoesoutofitsscope.Adestructorisrecognizedbyhavingthesameasthenameofitsclassprefixedbya~.
intmain(){Datetomorrow;return0;}Destructorsclass
Date{public:Date();……intgetMonth();~Date();private:intday,month,year;boolcheck();};destructorMemberfunctionItsnameisthesameasclass’snameprefixedbya~Noreturntypewithinitsdeclaration/definitionNoreturnstatementwithinitsdefinitionNoparameterswithinitsdefinitionDate::~Date(){cout<<"callingthedestructor\n";}intmain(){Datetomorrow;f();return0;}voidf(){Dateday;}OnlyonedestructorinaclassOrdersofConstructorandDestructorCallsAconstructorisimplicitlycalledwhenanobjectofaclassiscreated.Adestructorisimplicitlycalledwhenanobjectgoesoutofscope.Aconstructormakessurethatanobjectisproperlycreatedandinitialized.Conversely,adestructor
makessurethatanobjectisproperlycleanedupbeforeitisdestroyed.OrdersofConstructorandDestructorCallsclass
Date{public:Date(int=2020,int=9,int=1);voidoutput();~Date();private:intday,month,year;};Date::Date(int
y,int
m,int
d){cout<<"callingtheconstructor\n";year=y;month=m;day=d;}Date::~Date(){cout<<"callingthedestructor\n";output();}intmain(){Datetoday(2019);
Datetomorrow(2019,10,24);return0;}Outputresult:callingtheconstructorcallingtheconstructorcallingthedestructor2019-10-24callingthedestructor2020-9-1void
Date::output(){cout<<year<<"-"<<month<<"-"<<day<<endl;}Whentheobjectsarecreatedfromtoptodownin
a
scope,theconstructoriscalledinturn.Whentheobjectsgooutoftheirscope,thedestructorsarecalledinreverseorderofcreatingobjects.04CaseStudyCaseStudy-ProductSalesTotheissueofproductsale,youneedtodo:inputeachproduct'sID,unitprice,sales;(2)calculatetherevenueofallproducts;(3)printsaleinformation.YouanalysethisissuebyusingUMLandwriteoutabstracteddataandfunctions.DataabstractionDat
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 園區(qū)管理合同范例
- 勞務裝修包工合同范例
- 夫妻合伙開餐館合同范例
- 地板供銷合同范例
- 個人投資回報合同范例
- 印尼勞務合同范例
- 租房要買桌子合同范例
- 水管員勞務合同范例
- 小學操場硬化合同范例
- 投影儀合同范例
- 山東省濟南市2023-2024學年高一上學期1月期末考試 物理 含答案
- 機器學習(山東聯(lián)盟)智慧樹知到期末考試答案章節(jié)答案2024年山東財經(jīng)大學
- 商業(yè)倫理與企業(yè)社會責任(山東財經(jīng)大學)智慧樹知到期末考試答案章節(jié)答案2024年山東財經(jīng)大學
- 2024年輔警招聘考試試題庫及完整答案(全優(yōu))
- 2024年江蘇省普通高中學業(yè)水平測試小高考生物、地理、歷史、政治試卷及答案(綜合版)
- 《保健按摩師》(二級)理論知識鑒定要素細目表
- 甘蔗制糖簡介
- 三秦出版社五年級上冊綜合實踐教案
- 屋頂分布式光伏項目安全文明施工控制措施
- 水泥保證供應實施方案及服務承諾書
- 2022機要密碼工作總結機要室工作總結.doc
評論
0/150
提交評論