




已阅读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年一级建造师考试生产管理试题及答案
- 2024年审计师考试临考准备试题及答案
- 一级消防工程师应变能力考核试题及答案
- 2024年高级审计师考试各科目复习试题及答案
- 2024年消防工程师应试策略试题及答案
- 中级审计师考生职场规划试题及答案
- 当前政策与考试内容关系试题及答案
- 智能水务一体化管理系统项目售后服务与培训方案
- 专业合作社财务报表EXCEL三表
- 车辆租赁合同(无中介-非租车公司)(标准版)
- 物业业主见面会方案
- 油气集输管线工程施工组织设计方案
- 北京市初中学业水平考试体育与健康知识模拟练习题(含答案)
- 韩国语topik单词-初级+中级
- 管式加热炉烟气低温露点腐蚀
- 非营利组织会计民间非营利组织会计
- 2023学生食堂管理制度
- 华尔道夫酒店项目塔吊截臂施工方案
评论
0/150
提交评论