![C++面向对象程序设计双语教程(第3版)课件 class 5-classes objects-2_第1页](http://file4.renrendoc.com/view10/M02/27/18/wKhkGWW3eeaAd_pGAAIuAaLcfAc648.jpg)
![C++面向对象程序设计双语教程(第3版)课件 class 5-classes objects-2_第2页](http://file4.renrendoc.com/view10/M02/27/18/wKhkGWW3eeaAd_pGAAIuAaLcfAc6482.jpg)
![C++面向对象程序设计双语教程(第3版)课件 class 5-classes objects-2_第3页](http://file4.renrendoc.com/view10/M02/27/18/wKhkGWW3eeaAd_pGAAIuAaLcfAc6483.jpg)
![C++面向对象程序设计双语教程(第3版)课件 class 5-classes objects-2_第4页](http://file4.renrendoc.com/view10/M02/27/18/wKhkGWW3eeaAd_pGAAIuAaLcfAc6484.jpg)
![C++面向对象程序设计双语教程(第3版)课件 class 5-classes objects-2_第5页](http://file4.renrendoc.com/view10/M02/27/18/wKhkGWW3eeaAd_pGAAIuAaLcfAc6485.jpg)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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(析构函数)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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小升初模拟卷(试题)-2023-2024学年人教版数学六年级下册
- 空白借条格式范本合集
- 简单的租屋协议书范本合集
- 租赁私人商铺协议书范本合集
- 《第3课 炮兽》课文同步练习及答案
- 幼儿园教师节活动方案6篇
- 打架和解协议范例合集
- 个人新房屋购房合同合集
- 广东省汕头市澄海坝头中学高三物理模拟试卷含解析
- 山西省忻州市原平解村乡联合学校高三物理模拟试卷含解析
- Module4 Unit 1 Let's make a home library(说课稿)-2022-2023学年英语五年级下册
- 超星尔雅学习通《劳动通论》章节测试答案+2020超星尔雅学习通答案-大学精读章节测试
- 2022年春期2064国开电大专科《管理学基础》纸质形成性考核册答案
- 中级财务会计历年真题及答案
- 宏观经济学知到章节答案智慧树2023年海南大学
- GB/T 15166.6-2023高压交流熔断器第6部分:用于变压器回路的高压熔断器的熔断件选用导则
- 2023年中国工程机器人双足竞步项目交叉足赛
- 新华日报报业集团(南京)招聘工作人员 模拟检测试卷【共1000题含答案解析】
- 工程竣工验收告知书
- 护士长工作质量考核评分标准
- YB/T 804-1995钢铁渣处理及利用技术术语
评论
0/150
提交评论