浙江大学C++讲义2012.doc

上传人:鲁** 文档编号:558806616 上传时间:2022-10-03 格式:DOC 页数:16 大小:66KB
返回 下载 相关 举报
浙江大学C++讲义2012.doc_第1页
第1页 / 共16页
浙江大学C++讲义2012.doc_第2页
第2页 / 共16页
浙江大学C++讲义2012.doc_第3页
第3页 / 共16页
浙江大学C++讲义2012.doc_第4页
第4页 / 共16页
浙江大学C++讲义2012.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《浙江大学C++讲义2012.doc》由会员分享,可在线阅读,更多相关《浙江大学C++讲义2012.doc(16页珍藏版)》请在金锄头文库上搜索。

1、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 Functio

2、n 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 implementati

3、on)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 Der

4、ive1 : public Basic;5.3 Polymorphism(多态性)5.3.1 Static PolymorphismOverloading5.3.1.1 function Overloading(函数重载)5.3.1.2 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 Com

5、plex &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 & ope

6、rator *() 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 Complex

7、10;ptr1(5.6);ptr2=ptr1+c;+ptr;ptr+;l Default Arguments(默认参数)5.3.1.3 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.prec

8、ision(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#includ

9、e 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;i1

10、0;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 =

11、 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

12、=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()

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号