1、 浙江大学浙江大学 C+课件课件 Object-Oriented Programming 一.Abstraction(数据抽象)Encapsulation(hidden implementation)封装性 Object State,Data,Message,Method,Interface,Operation,Function Type:All objects of a particular type can receive the same messages.二.Inheritance(继承性)三.Polymorphism(多态性)四.A Better C 四四.1/comments/*/
2、四四.2 Function Prototype void*malloc(size_t);四四.3 The new&delete Operators char*p=new char3;int*p=new int3;四四.4 Defining variables anywhere int k;k=5;int m;m=k+6;四四.5 Reference int k=8;int&m=k;四四.6 Inline function 四四.7 const 五.Object Oriented Programming 五五.1 Abstraction Encapsulation(hidden implemen
3、tation)object&class struct A int k;float b;int GetK()return k;A a;a.k=555;class A private:int k;/hidden implementation float b;public:void SetK(int k)/member function k=k;A a;a.k=777;/.X a.GetK()constructor(initialization)destructor(cleanup)class Stack ;五五.2 Inheritance class Basic ;class Derive1:pu
4、blic Basic ;五五.3 Polymorphism(多态性)五五.3.1 Static Polymorphism Overloading 五五.3.1.1 function Overloading(函数重载函数重载)五五.3.1.2 operator Overloading(操作符重载操作符重载)class Complex double 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+(c
5、onst Complex&c)const return Complex(m_r+c,m_r,m_i+c,m_i);double GetR()const retur m_r;double GetI()const retur m_i;class ComplexPtr Complex*m_ptr;public:ComplexPtr(Complex&c)m_ptr=c;const ComplexPtr&operator=(Complex*const ptr)m_ptr=ptr;return*this;Complex*operator-()const return m_ptr;Complex&opera
6、tor*()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);p
7、tr2=ptr1+c;+ptr;ptr+;Default Arguments(默认参数)五五.3.1.3 Redefining(重定义重定义)name hiding Overriding(重写,覆盖,超载,过载)五五.3.2 Dynamic polymorphism Upcasting(向上类型转换)Virtual Function Incremental development(增量式开发)六.File&Stream 六六.1 Text file cout 123.567 endl;k=123;cout k;cout.fill($);cout.precision(8);cout.width(
8、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);六六.2 Binary file read write 七.Templates 七七.1 Function templates template T Max(T x,T y)return(xy?x:y);七七.2 Class templates#include const int STACKSIZE=5
9、0;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;
10、八.Exception handling 八八.1 assert 八八.2 return true/false 八八.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)t
11、hrow 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;try for(i=0;i10;i+)s.Push(i);catch(Overflow obj)cout Stack Overflow endl;try for(i=0;i10;i+)cout s.Pop()endl;catch(Underflow)cout Stack Underflow endl;global
12、 data area stack heap 九.Copy-constructor T(const T&)overloading=overloading T(const&T)class String char*p;int l;public:String(char*s=null)if(s)l=:strlen(s);p=new charl+1;:strcpy(p,s)else p=null;l=0;String(const String&s)if(s.p)l=:strlen(s.p);p=new charl+1;:strcpy(p,s.p)else p=null;l=0;String()delete
13、 p;String s1(“Hi”);String s2=s1;一十.Virtual destructors&Pure virtual functions(虚析构函数与纯虚函数)Abstract base classes(抽象基类)class Base int*p;public:Base(int size)p=new intsize;Base()delete p;class Derive:public Base char*s;public:Derive(int size):Base(size)s=new charsize;Derive()s=new charsize;Base*pB=new D
14、erive(10);delete pB;一十一.Run-time type identification (RTTI)typeid(t).name()#include class Base ;class Derive:public Base ;void main()Base*bp=new Derive;couttypeid(*bp).name()endl;一十二.C+explicit casts dynamic_cast downcasting static_cast upcasting reinterpret_cast const_cast Base b;Derive d;Base*bp;D
15、erive*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);一十三.How C+implements late binding VTABLE VPTR 一十四.Static members class A static int m;int n;A a1,a2;A1.m=7;A*ap=new A;coutmm+=12;coutm”,”;delete apl couta1.m;一十五.N
16、amespace namespace A int m;namespace B int m;int n;void Fb()m=3;n=4;void Fa()B:m+=7;m=23;namespace B int m;int m;void F()m=8;A:m=9;B:m=10;A:B:m=10;一十六.Pointer to class member 一十七.Multiple inheritance class Base1 int i;class Base2 int i;class Derive:public Base1,public Base2 float f;int getInt()return i;float getFloat()return f;
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2