




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Object-Oriented Programming1. Abstraction(数据抽象)Encapsulation(hidden implementation)封装性ObjectState,Data, Message, Method, Interface, Operation,FunctionType:All objects of a particular type can receive the same messages.2. Inheritance(继承性)3. Polymorphism(多态性)4. A Better C4.1 / comments/* */4.2 Function Prototypevoid *malloc(size_t);4.3 The new & delete Operators char *p = new char3; int *p = new int3; 4.4 Defining variables anywhereint k;k = 5;int m;m = k+6;4.5 Referenceint k = 8;int &m = k;4.6 Inline function4.7 const5. Object Oriented Programming5.1 AbstractionEncapsulation(hidden implementation)object & classstruct Aint k;float b; int GetK() return k; ;A a;a.k = 555;class Aprivate:int k; /hidden implementationfloat b;public:void SetK(int k) /member functionk = k; ;A a;a.k = 777; /. Xa.GetK() constructor (initialization)destructor(cleanup)class Stack ;5.2 Inheritanceclass Basic;class Derive1 : public Basic;5.3 Polymorphism(多态性)5.3.1 Static PolymorphismOverloading function Overloading(函数重载) operator Overloading(操作符重载)class Complexdouble m_r;double m_i;Complex(double r=0.0,double i=0.0)m_r=r;m_i=i;void operator()(double r,double i)m_r=r;m_i=i;Complex operator+(const Complex &c)constreturn Complex(m_r+c,m_r, m_i+c,m_i);double GetR() constretur m_r;double GetI() constretur m_i;class ComplexPtrComplex *m_ptr;public:ComplexPtr(Complex &c)m_ptr=c;const ComplexPtr & operator=(Complex * const ptr)m_ptr=ptr;return *this;Complex * operator-()constreturn m_ptr;Complex & operator *() return *m_ptr;Complex &operator (int i)return m_ptri;const Complex &operator (int i) const return m_ptri;ComplexPtr &operator+()m_ptr+;return *this;ComplexPtr &operator+(int)m_ptr+;return *this;void main()Complex c(3,4);ComplexPtr ptr(c);coutGetR()endl;cout(*ptr).GetR()endl;ptr=new Complex10;ptr1(5.6);ptr2=ptr1+c;+ptr;ptr+;l Default Arguments(默认参数) Redefining(重定义)l name hidingl Overriding(重写,覆盖,超载,过载)5.3.2 Dynamic polymorphisml Upcasting(向上类型转换)l Virtual Functionl Incremental development(增量式开发)6. File & Stream6.1 Text filecout 123.567 endl;k = 123;cout k;cout.fill($);cout.precision(8);cout.width(7);cout.setf(ios:hex);cout 23 endl;fstream in(in.txt,ios:in);fstream out(out.txt,ios:out);if (!in)return;while (!in.eof()char ch = in.get();out.put(ch); 6.2 Binary file read write7. Templates7.1 Function templatestemplate T Max(T x,T y)return (xy?x : y);7.2 Class templates#include const int STACKSIZE = 50;template class Stack T *m_pItem; int m_top; int m_size;public: Stack(int size = STACKSIZE) m_size = size; m_pItem = new intsize; m_top = -1; void Push(T item) m_pItem+m_top = item; T Pop();template T Stack:Pop() return m_pItemm_top-;void main()Stack s(10);int i;for (i=0;i10;i+)s.Push(i);for (i=0;i10;i+)cout s.Pop() endl;8. Exception handling8.1 assert8.2 return true/false8.3 try/catch throw#include class Overflow;class Underflow;const int STACKSIZE = 50;template class Stack T *m_pItem; int m_top; int m_size;public: Stack(int size = STACKSIZE) m_size = size; m_pItem = new intsize; m_top = -1; void Push(T item) if (m_top = m_size-1) throw Overflow(); m_pItem+m_top = item; T Pop();template T Stack:Pop() if (m_top 0) throw Underflow(); return m_pItemm_top-;void main()Stack s(3);int i;tryfor (i=0;i10;i+)s.Push(i);catch(Overflow obj)cout Stack Overflow endl;tryfor (i=0;i10;i+)cout s.Pop() endl;catch(Underflow)cout Stack Underflow endl;global data areastackheap9. Copy-constructorT(const T &)overloading =overloading T(const &T)class Stringchar *p;int l;public:String(char * s=null)if(s)l=:strlen(s);p=new charl+1;:strcpy(p,s)elsep=null;l=0;String (const String &s)if(s.p)l=:strlen(s.p);p=new charl+1;:strcpy(p,s.p)elsep=null;l=0;String()delete p;String s1(“Hi”);String s2=s1;10. Virtual destructors & Pure virtual functions(虚析构函数与纯虚函数)l Abstract base classes(抽象基类)class Baseint *p;public:Base(int size)p=new intsize;Base()delete p;class Derive:public Basechar * s;public:Derive(int size):Base(size)s=new charsize;Derive()s=new charsize;Base *pB=new Derive(10);delete pB;11. Run-time type identification (RTTI) typeid(t).name()#includeclass Base;class Derive:public Base;void main()Base *bp=new Derive;couttypeid(*bp).name()endl;12. C+ explicit castsdynamic_cast downcastingstatic_cast upcastingreinterpret_cast const_castBase b;Derive d;Base *bp;Derive *dp;bp=dynamic_cast(&d);bp=&b;dp=dynamic_cast(bp);bp=*d;dp= dynamic_cast(bp);Derive &dr1=dynamic_cast(*bp);Derive &dr2=dynamic_cast(b);13. How C+ implements late binding VTABLE VPTR 14. Static membersclass Astatic int m;int n;A a1,a2;A1.m=7;A* ap=new A;coutmm+=12;coutm”,”;delete aplcouta1.m;15. Namespacenamespace Aint m;namespace Bint m;int n;void Fb()m=3;n=4;void Fa()B
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖南省怀化三中2025届物理高一下期末学业水平测试试题含解析
- 2025届黑龙江省牡丹江市五县市物理高一第二学期期末综合测试模拟试题含解析
- 2025届陕西延安市实验中学大学区校际联盟高一物理第二学期期末复习检测模拟试题含解析
- 辽宁省阜新市海州高级中学2025年物理高二下期末复习检测试题含解析
- 宠物饲养与管理课件
- 2025届海南省临高县新盈中学高二物理第二学期期末经典模拟试题含解析
- 二零二五年财务顾问与财务软件公司合作协议
- 二零二五年度自建房竣工验收与交付使用合同
- 二零二五年财税代理服务合同-税务信息化解决方案
- 二零二五年酒吧餐厅综合承包运营管理协议
- 山东省枣庄市峄城区2024-2025学年七年级上学期期末考试数学试题(原卷版+解析版)
- 脑积水患者治疗与护理
- DBJT 13-309-2019非开挖顶管技术规程
- DBJ50-T-157-2022房屋建筑和市政基础设施工程施工现场从业人员配备标准
- 转岗建工作简历模板
- 物业小区保安服务合同
- 子宫肌瘤中医护理常规
- 员工内部调配协议
- 《食品毒理学》试卷A及答案
- 2024年山东省青岛市普通高中自主招生物理试卷(含解析)
- 课程设计液氨储罐设计
评论
0/150
提交评论