c程序设计(part2)_第1页
c程序设计(part2)_第2页
c程序设计(part2)_第3页
c程序设计(part2)_第4页
c程序设计(part2)_第5页
已阅读5页,还剩73页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C+程序设计(part2),OOP,Whatnon-OOSolution,#include#defineSTACK_SIZE100structStackinttop;intbufferSTACK_SIZE;voidmain()Stackst1,st2;st1.top=-1;st2.top=-1;intx;push(st1,12);pop(st1,x);,boolpush(Stack,boolpop(Stack,st1.buffer2=-1;st2.buffer2+;,_,OOP,OOSolution,#include#defineSTACK_SIZE100classStackprivate:inttop;intbufferSTACK_SIZE;public:Stack()top=-1;boolpush(inti);boolpop(int,boolStack:push(inti);if(top=STACK_SIZE-1)cout,this-,this-,this-,this-,this-,this-,st1.buffer2=-1;,Encapsulation,InformationHidding,OOP,ConceptsProgramObject1+Object2+ObjectnObject:Data+OperationMessage:functioncallClassObject-OrientedObject-BasedOnlyProvideEncapsulationWithoutInheritanceAda,etc,OOP,Why评价标准EfficencyofDevelopmentQualityExternalCorrectness、Efficiency、Robustness、ReliabilityUsability、ReusabilityInternalReadability、Maintainability、Portability,HUIUX,产品在规定的条件下和规定的时间内完成规定功能的能力(概率度量:可靠度),需求架构设计构建模式代码测试用例项目组织结构,OOP,Advantages提高开发效率和软件质量更高层次的抽象数据抽象数据封装更好的模块化支持(高内聚、低耦合)软件复用(部分重用)对需求变更具有更好的适应性,C+vsJava,类,类定义class;组成成员:成员变量、成员函数成员变量:在类定义中给出的是声明成员函数:在类定义中给出的是定义或声明类成员的访问控制描述,类,classTDatepublic:voidSetDate(inty,intm,intd);intIsLeapYear();voidPrint();private:intyear,month,day;,类定义不完整,类,成员变量声明在类定义中对成员变量的说明是声明不能赋值在类定义中声明成员变量时不能初始化引用或指针类型成员变量,类,classA;classBAa;/ErrorBb;/ErrorA*p;/OKB*q;/OKA/OK,类型还未定义/未定义完全只能声明成指针或引用,类,成员函数在类定义中定义成员函数(inline)classTDatepublic:voidSetDate(inty,intm,intd)year=y;month=m;day=d;intIsLeapYear()return(year%4=0,类,在类定义外定义成员函数,classTDatepublic:voidSetDate(inty,intm,intd);voidPrint();private:intyear,month,day;,voidTDate:SetDate(inty,intm,intd)year=y;month=m;day=d;voidTDate:Print()coutyear.month.dayCreateButton();Label*Label=fac-CreateLabel();,抽象工厂模式AbstactFactory,虚函数,虚析构函数,classB;classD:publicB;B*p=newD;?:deletep;,classstringclassBclassD:publicBstringname;B*p=newD;?:deletep;,虚函数,确定publicinheritance,是真正意义的“is_a”关系不要定义与继承而来的非虚成员函数同名的成员函数,classBpublic:voidmf();classD:publicB;Dx;B*pB=,public:voidmf();,/B:mf,/D:mf,classrectanglepublic:voidsetHeight(int);voidsetWidth(int);intheight()const;intwidth()const;classSquare:publicrectanglepublic:voidsetLength(int);,virtual,virtual,private:voidsetHeight(int);voidsetWidth(int);,Penguin,classFlyingBirdclassNonFlyingBird,virtualvoidfly()error(Penguinscantfly!);,voidmakeBigger(Rectangle,assert(s.width()=s.height();,虚函数,明智地运用privateInheritanceImplemented-in-term-of需要使用BaseClass中的protected成员,或重载virtualfunction不希望一个BaseClass被client使用在软件”设计”层面无意义,只用于软件实现层面,classCHumanBeing;classCStudent:privateCHumanBeing;voideat(constCHumanBeing/Error,虚函数,纯虚函数只有函数接口会被继承子类必须继承函数接口(必须)提供实现代码一般虚函数函数的接口及缺省实现代码都会被继承子类必须继承函数接口可以继承缺省实现代码非虚函数函数的接口和其实现代码都会被继承必须同时继承接口和实现代码,classShapepublic:virtualvoiddraw()const=0;virtualvoiderror(conststring,虚函数,绝对不要重新定义继承而来的缺省参数值静态绑定效率,classApublic:virtualvoidf(intx=0);classB:publicApublic:virtualvoidf(intx=1);,classC:publicApublic:virtualvoidf(int);,A*p_a;Bb;p_a=,A*p_a1;Cc;p_a1=,对象中只记录虚函数的入口地址,多继承,多继承定义class:,成员表继承方式public、private、protected继承方式及访问控制的规定同单继承派生类拥有所有基类的所有成员,多继承,Bedweight,Sleep(),SetWeight(),Sofaweight,WatchTV(),SetWeight(),SleepSofa,foldOut(),多继承,Furnitureweight,SetWeight(),Furnitureweight,SetWeight(),Bed,Sleep(),Sofa,WatchTV(),SleepSofa,FoldOut(),Base-ClassDecomposition,多继承,Furnitureweight,SetWeight(),Bed,Sleep(),Sofa,WatchTV(),SleepSofa,FoldOut(),VirtualInheritance,多继承,基类的声明次序决定:对基类构造函数/析构函数的调用次序对基类数据成员的存储安排名冲突:虚基类如果直接基类有公共的基类,则该公共基类中的成员变量在多继承的派生类中有多

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论