C++面向對象程序設計雙語教程(第3版)課件 class 5-classes objects-2_第1頁
C++面向對象程序設計雙語教程(第3版)課件 class 5-classes objects-2_第2頁
C++面向對象程序設計雙語教程(第3版)課件 class 5-classes objects-2_第3頁
C++面向對象程序設計雙語教程(第3版)課件 class 5-classes objects-2_第4頁
C++面向對象程序設計雙語教程(第3版)課件 class 5-classes objects-2_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論