版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ObjectivesTounderstanddataabstractionfurtherTobeabletoknowwhataclassisandhowtocreateanobjectTo
understandencapsulationandinformationhiding01IntroductiontoStructures03Classesand
Objects02DataAbstraction04AccessControl05EncapsulationandInformationHiding01IntroductiontoStructureIntroductiontoStructuresCase
Study
1Problem:Writeaprogramforagradebookofagivencourse.Agradebookincludescourse’sname,student’sIDandgrade;Inputallinformationoncourseandstudent;Calculatetheaveragegradeofthecourse;Printthegradebook.Requirements:DatastructureA
datastructure
isagroupofdataelementsgroupedtogetherunderonename.structgradeBook{stringcourseName;stringstudentID;intstudentGrade;};Datatypes:built-intypes,e.g.int,char,floatandsoon.IntroductiontoStructuresinCstruct
gradeBook{stringcourseName;stringstudentID[studentNum];intstudentGrade[studentNum];};voidinput(gradeBook&oop);doubleaverage(gradeBook&oop);voidprint(gradeBook&oop);intmain(){gradeBookgbOOP;input(gbOOP);print(gbOOP);return0;}adatatype,auser-definedtypeMembersAstructuretypeisanaggregationofdifferentdatatypesunderonename.Dotoperator(.)Arrowoperator(->)Memberaccessoperatorsvoidinput(gradeBook&oop){cin>>
oop.courseName;for(inti=0;i<studentNum;i++)cin>>
oop.studentID[i]>>
oop.studentGrade[i];}IntroductiontoStructuresinC++struct
gradeBook{stringcourseName;stringstudentID[studentNum];intstudentGrade[studentNum];voidinput(){cin>>courseName;for(inti=0;i<studentNum;i++)cin>>studentID[i]>>studentGrade[i];}doubleaverage(){intsum=0;for(inti=0;i<studentNum;i++)sum+=studentGrade[i];return(double)sum/studentNum;}voidprint(){cout<<
"coursename:"
<<courseName<<endl;for(inti=0;i<studentNum;i++)cout<<studentID[i]<<
"|"
<<studentGrade[i]<<endl;cout<<
"course'saverage:"
<<average()<<endl;}};intmain(){gradeBookgbOOP;gbOOP.input();gbOOP.print();return0;}MemberfunctionsData
membersMembersStructurewithmemberfunctionsIntroductiontoStructureinC++struct
gradeBook{stringcourseName;stringstudentID[studentNum];intstudentGrade[studentNum]={0};voidinput();doubleaverage();voidprint();};intmain(){gradeBookgbOOP;gbOOP.input();gbOOP.print();return0;}void
gradeBook::input(){cin>>courseName;for(inti=0;i<studentNum;i++)cin>>studentID[i]>>studentGrade[i];}double
gradeBook::average(){intsum=0;for(inti=0;i<studentNum;i++)sum+=studentGrade[i];return(double)sum/studentNum;}C++11:InitializerinthestructureDefiningaStructureinCCase
Study
2Problem:WriteaprogramofaDatetype.Adateincludesyear,month,day;Inputallinformationonadate;Printthedate.Requirements:struct
Date{intyear,month,day;};intmain(){Datetoday,d[2];//arraycin>>today.year>>today.month>>today.day;cout<<today.year<<endl;cin>>d[0].year>>d[0].month>>d[0].day;cin>>d[1].year>>d[1].month>>d[1].day;cout<<d[1].year<<d[1].month<<d[1].day<<endl;Date*dptr=&today;//pointerdptr->year=2010;cout<<dptr->year<<dptr->month<<dptr->day<<endl;Date&rd=today;//referencecout<<rd.year<<rd.month<<rd.day<<endl;return0;}Dotoperator(.)Arrowoperator(->)MemberaccessoperatorsDefiningaStructureinCstruct
Date{ intyear,month,day;};voidinput_date(Date&date,int
y,int
m,int
d){date.year=y;date.month=m;date.day=d;}voidprint_date(Date&date){cout<<date.year<<date.month<<date.day<<endl;}intmain(){Datetoday;input_date(today,2017,10,1);print_date(today);return0;}DefiningaStructureinC++struct
Date{intyear,month,day;voidinput_date(inty,intm,intd);voidprint_date();};void
Date::input_date(int
y,int
m,int
d){year=y;month=m;day=d;}void
Date::print_date(){cout<<year<<month<<day<<endl;}intmain(){Datetoday;today.input_date(2016,10,12);today.print_date();return0;}Memberfunctions02DataAbstractionDataAbstractionLogicalpropertiesofthegradeBook,Datestructuredtypes:Threemembervariables,e.g.courseName,studentID,studentGrade,year,month,dayDeclarationoffunctions,e.g.input,average,print,input_date,print_date,…Implementation:DefinitionoffunctionsDataabstractionisdefinedasaprocessofseparatingthelogicalpropertiesofdatafromitsimplementationdetail.Dateabstractionisusedasamethodinanobject-orientedprogramming.AbstractDataTypesDefinitionofaDateADTdataTypeNamegradeBook,DateDomaineachdatatyperepresentsvalues,e.g.courseName,studentID,studentGrade,year,month,dayOperationsInputtheinformationofcourseandstudentsPrintagradebookinputadateprintadateAbstractdatatype(ADT)Adatatypeseparatesthelogicalpropertiesfromtheimplementationdetail.03ClassesandObjectsClassesandObjectsprint_dateinput_dateyear,month,daydateClasses
areanexpandedconceptof
datastructures:likedatastructures,theycancontaindatamembers,buttheycanalsocontainfunctionsasmembers.Classisauser-definedtypeinaprogramminglanguagethatisusedtorepresentanabstractdatatype(ADT).Aclassisasetorcollectionofabstractedobjectsthatsharecommoncharacteristics.printinputcourseName,studentIDstudentGradegradeBookDefinitionofaClassclassDate{
private:
intday;
intmonth;
intyear;
public:
voidinput_date(intd,intm,inty);
voidprint_date();};datamembermemberfunctionsaccesscontrolspecifierClassnameclassclass_name
{accesscontrolspecifier:datamembers;memberfunctions;};Syntaxtodeclareaclasstype
Thecomponentsofaclassarecalledthemembersoftheclass.Themembersoftheclassmaybeeitherdatatypeorfunctions.Dataintheclassarecalleddatamembers.Functionsdeclaredwithintheclassdefinitionarecalledmemberfunctions.DefinitionofaClassconst
intstudentNum=3;class
gradeBook{public:voidinput();doubleaverage();voidprint();private:stringcourseName="";stringstudentID[studentNum]={""};intstudentGrade[studentNum]={0};};memberfunctionsdatamemberClassnameaccesscontrolspecifierDefininganObjectDatemy_birthday;objectDeclarelocalobjectse.g.inanyfunctionDeclareglobalobjectsDeclareobjectsasclassmembersDeclareanobjectarraye.g.Dateday[10];Declareconstantobjectse.g.constDatedayDeclareobjectreferencesorpointerse.g.Date*pDay;Declarestaticobjecte.g.staticDatedayWhenaclassisused,anobjectisdeclared.Date*today;Dated1[3];ConceptofanobjectAnobject
isavariableofaclasstype,orclassinstance.Whenisanobjectdeclared?Inwhichwayisanobjectdeclared?Anobjectisdeclaredinthesameasbuilt-inttypesintmain(){Datetoday;today.input_date(2016,10,12);today.print_date();return0;}HowtoAccessMemberFunctionsinaClassMemberfunctionswithinaclasscanbeinvokedbyaclassvariable(i.e.object).objectaccessingmemberfunctionclassDate{
private:
intday;
intmonth;
intyear;
public:
voidinput_date(intd,intm,inty);
voidprint_date();};DefinitionofMemberFunctionsclass
Date{private:intday,month,year;public:voidinput_date(int
d,int
m,int
y){ day=d;month=m;year=y;}voidadd_year(int
n){year+=n;}voidadd_month(int
n){month+=n;}voidadd_day(int
n){day+=n;}};class
Date{private:intday,month,year;public:voidinput_date(int
d,int
m,int
y);voidadd_year(int
n);voidadd_month(int
n);voidadd_day(int
n);};inline
void
Date::add_year(n){year+=n;}inlinevoidadd_month(int
n){month+=n;}inlinevoidadd_day(int
n){day+=n;}DefinememberfunctionswithinaclassTheinlinefunctionsinstructcompilertoinsertcompletebodyofthefunctionwhereverthatfunctiongotusedincode.intmain(){Dateday;day.input_date(9,8,2020);day.add_year(1);return0;}DefinitionofMemberFunctionsBecausedifferentclassescanhavememberfunctionswiththesamename,wemustspecifytheclassnamewhendefiningamemberfunction.DefinememberfunctionsoutsidetheClassclass
Date{private:intday,month,year;public:voidinput_date(int
d,int
m,int
y);voidadd_year(int
n);voidadd_month(int
n);voidadd_day(int
n);};void
Date::input_date(int
d,int
m,int
y){day=d;month=m;year=y;}void
Date::add_year(n){year+=n;}void
Date::add_month(int
n){month+=n;}voidadd_day(int
n)//error{day+=n;}DefinitionofMemberFunctionsDefinememberfunctionsoutsidetheClassconst
intstudentNum=3;class
gradeBook{public:voidinput();doubleaverage();voidprint();private:stringcourseName="";stringstudentID[studentNum]={""};intstudentGrade[studentNum]={0};};void
gradeBook::input(){cin>>courseName;for(inti=0;i<studentNum;i++)cin>>studentID[i]>>studentGrade[i];}double
gradeBook::average(){intsum=0;for(inti=0;i<studentNum;i++)sum+=studentGrade[i];return(double)sum/studentNum;}void
gradeBook::print(){cout<<
"coursename:"
<<courseName<<endl;for(inti=0;i<studentNum;i++){ cout<<setw(9)<<right<<studentID[i]
<<setw(7)<<right<<studentGrade[i]<<endl;}cout<<
"----------------------------\n";cout<<
"Theaveragegrade:"
<<average()<<endl;}04AccessControlAccessControlclass
Date{private:intday,month,year;public:voidinput_date(int
d,int
m,int
y);voidadd_year(int
n);voidadd_month(int
n);voidadd_day(int
n);};accessspecifierAccesscontrol
isenforcedatcompiletime,notatrun-time.Whenamemberisaccessed,thecompilercheckstheaccessspecifierforit.Anaccessspecifierisoneofthreekeywords:private,publicorprotectedclass
Date{intday,month,year;public:voidinput_date(int
d,int
m,int
y);voidadd_year(int
n);voidadd_month(int
n);voidadd_day(int
n);};class
Date{public:voidinput_date(int
d,int
m,int
y);voidadd_year(int
n);voidadd_month(int
n);voidadd_day(int
n);private:intday,month,year;};class
Date{public:voidinput_date(int
d,int
m,int
y);private:voidadd_year(int
n);voidadd_month(int
n);voidadd_day(int
n);private:intday,month,year;};AccessControlTheprivatepartofthememberswithinaclasscanbeusedonlybymemberfunctionsofthisclass.(default)Thepublicpartofthememberswithaclasscanbeusedbymemberfunctionsofthisclassorotherfunctionsoutsidetheclass.intmain(){Datetoday;today.input_date(12,9,2021);return0;}accessspecifier-privateaccessspecifier-publicclass
Date{public:voidinput_date(int
d,int
m,int
y);voidadd_year(int
n);voidadd_month(int
n);voidadd_day(int
n);private:intday,month,year;};voidDate::input_date(int
d,int
m,int
y){day=d;month=m;year=y;}intmain(){Datetoday;today.day=15;//errorreturn0;}05EncapsulationandInform
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024至2030年中国高低速双轴搅拌机数据监测研究报告
- 2024年血液体液诊断产品项目综合评估报告
- 2024至2030年中国轻型汽车齿轮数据监测研究报告
- 2024年酒剂项目成效分析报告
- 2024至2030年中国缓冲纸垫制造机数据监测研究报告
- 2024至2030年中国玻璃钢售票台行业投资前景及策略咨询研究报告
- 2024至2030年中国手机喷涂专用贴头数据监测研究报告
- 2024至2030年中国复方菠萝酶片数据监测研究报告
- 《老住宅借用合同》
- 内蒙古呼伦贝尔市(2024年-2025年小学五年级语文)统编版专题练习(上学期)试卷及答案
- 污水处理厂EPC项目建设方案
- 酒店经理管理酒店运营
- AI在农业领域的应用
- 汽车eps行业国内外市场发展前景分析与投资风险预测报告
- 短视频运营实战:抖音短视频运营
- DB35T 2061-2022 村庄规划编制规程
- 园长进班指导制度方案及流程
- HG-T 20583-2020 钢制化工容器结构设计规范
- 监理工作中变更管理的规范与应对措施
- 郑州人才公寓策划方案
- 特殊餐食种类课件
评论
0/150
提交评论