浙江大学C++课件.docx

上传人:b****2 文档编号:2981251 上传时间:2023-05-05 格式:DOCX 页数:19 大小:21.54KB
下载 相关 举报
浙江大学C++课件.docx_第1页
第1页 / 共19页
浙江大学C++课件.docx_第2页
第2页 / 共19页
浙江大学C++课件.docx_第3页
第3页 / 共19页
浙江大学C++课件.docx_第4页
第4页 / 共19页
浙江大学C++课件.docx_第5页
第5页 / 共19页
浙江大学C++课件.docx_第6页
第6页 / 共19页
浙江大学C++课件.docx_第7页
第7页 / 共19页
浙江大学C++课件.docx_第8页
第8页 / 共19页
浙江大学C++课件.docx_第9页
第9页 / 共19页
浙江大学C++课件.docx_第10页
第10页 / 共19页
浙江大学C++课件.docx_第11页
第11页 / 共19页
浙江大学C++课件.docx_第12页
第12页 / 共19页
浙江大学C++课件.docx_第13页
第13页 / 共19页
浙江大学C++课件.docx_第14页
第14页 / 共19页
浙江大学C++课件.docx_第15页
第15页 / 共19页
浙江大学C++课件.docx_第16页
第16页 / 共19页
浙江大学C++课件.docx_第17页
第17页 / 共19页
浙江大学C++课件.docx_第18页
第18页 / 共19页
浙江大学C++课件.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

浙江大学C++课件.docx

《浙江大学C++课件.docx》由会员分享,可在线阅读,更多相关《浙江大学C++课件.docx(19页珍藏版)》请在冰点文库上搜索。

浙江大学C++课件.docx

浙江大学浙江大学C+课件课件Object-OrientedProgramming一.Abstraction(数据抽象)Encapsulation(hiddenimplementation)封装性ObjectState,Data,Message,Method,Interface,Operation,FunctionType:

Allobjectsofaparticulartypecanreceivethesamemessages.二.Inheritance(继承性)三.Polymorphism(多态性)四.ABetterC四四.1/comments/*/四四.2FunctionPrototypevoid*malloc(size_t);四四.3Thenew&deleteOperatorschar*p=newchar3;int*p=newint3;四四.4Definingvariablesanywhereintk;k=5;intm;m=k+6;四四.5Referenceintk=8;int&m=k;四四.6Inlinefunction四四.7const五.ObjectOrientedProgramming五五.1AbstractionEncapsulation(hiddenimplementation)object&classstructAintk;floatb;intGetK()returnk;Aa;a.k=555;classAprivate:

intk;/hiddenimplementationfloatb;public:

voidSetK(intk)/memberfunctionk=k;Aa;a.k=777;/.Xa.GetK()constructor(initialization)destructor(cleanup)classStack;五五.2InheritanceclassBasic;classDerive1:

publicBasic;五五.3Polymorphism(多态性)五五.3.1StaticPolymorphismOverloading五五.3.1.1functionOverloading(函数重载函数重载)五五.3.1.2operatorOverloading(操作符重载操作符重载)classComplexdoublem_r;doublem_i;Complex(doubler=0.0,doublei=0.0)m_r=r;m_i=i;voidoperator()(doubler,doublei)m_r=r;m_i=i;Complexoperator+(constComplex&c)constreturnComplex(m_r+c,m_r,m_i+c,m_i);doubleGetR()constreturm_r;doubleGetI()constreturm_i;classComplexPtrComplex*m_ptr;public:

ComplexPtr(Complex&c)m_ptr=c;constComplexPtr&operator=(Complex*constptr)m_ptr=ptr;return*this;Complex*operator-()constreturnm_ptr;Complex&operator*()return*m_ptr;Complex&operator(inti)returnm_ptri;constComplex&operator(inti)constreturnm_ptri;ComplexPtr&operator+()m_ptr+;return*this;ComplexPtr&operator+(int)m_ptr+;return*this;voidmain()Complexc(3,4);ComplexPtrptr(c);coutGetR()endl;cout(*ptr).GetR()endl;ptr=newComplex10;ptr1(5.6);ptr2=ptr1+c;+ptr;ptr+;DefaultArguments(默认参数)五五.3.1.3Redefining(重定义重定义)namehidingOverriding(重写,覆盖,超载,过载)五五.3.2DynamicpolymorphismUpcasting(向上类型转换)VirtualFunctionIncrementaldevelopment(增量式开发)六.File&Stream六六.1Textfilecout123.567endl;k=123;coutk;cout.fill($);cout.precision(8);cout.width(7);cout.setf(ios:

hex);cout23endl;fstreamin(in.txt,ios:

in);fstreamout(out.txt,ios:

out);if(!

in)return;while(!

in.eof()charch=in.get();out.put(ch);六六.2Binaryfilereadwrite七.Templates七七.1FunctiontemplatestemplateTMax(Tx,Ty)return(xy?

x:

y);七七.2Classtemplates#includeconstintSTACKSIZE=50;templateclassStackT*m_pItem;intm_top;intm_size;public:

Stack(intsize=STACKSIZE)m_size=size;m_pItem=newintsize;m_top=-1;voidPush(Titem)m_pItem+m_top=item;TPop();templateTStack:

Pop()returnm_pItemm_top-;voidmain()Stacks(10);inti;for(i=0;i10;i+)s.Push(i);for(i=0;i10;i+)couts.Pop()endl;八.Exceptionhandling八八.1assert八八.2returntrue/false八八.3try/catchthrow#includeclassOverflow;classUnderflow;constintSTACKSIZE=50;templateclassStackT*m_pItem;intm_top;intm_size;public:

Stack(intsize=STACKSIZE)m_size=size;m_pItem=newintsize;m_top=-1;voidPush(Titem)if(m_top=m_size-1)throwOverflow();m_pItem+m_top=item;TPop();templateTStack:

Pop()if(m_top0)throwUnderflow();returnm_pItemm_top-;voidmain()Stacks(3);inti;tryfor(i=0;i10;i+)s.Push(i);catch(Overflowobj)coutStackOverflowendl;tryfor(i=0;i10;i+)couts.Pop()endl;catch(Underflow)coutStackUnderflowendl;globaldataareastackheap九.Copy-constructorT(constT&)overloading=overloadingT(const&T)classStringchar*p;intl;public:

String(char*s=null)if(s)l=:

strlen(s);p=newcharl+1;:

strcpy(p,s)elsep=null;l=0;String(constString&s)if(s.p)l=:

strlen(s.p);p=newcharl+1;:

strcpy(p,s.p)elsep=null;l=0;String()deletep;Strings1(“Hi”);Strings2=s1;一十.Virtualdestructors&Purevirtualfunctions(虚析构函数与纯虚函数)Abstractbaseclasses(抽象基类)classBaseint*p;public:

Base(intsize)p=newintsize;Base()deletep;classDerive:

publicBasechar*s;public:

Derive(intsize):

Base(size)s=newcharsize;Derive()s=newcharsize;Base*pB=newDerive(10);deletepB;一十一.Run-timetypeidentification(RTTI)typeid(t).name()#includeclassBase;classDerive:

publicBase;voidmain()Base*bp=newDerive;couttypeid(*bp).name()endl;一十二.C+explicitcastsdynamic_castdowncastingstatic_castupcastingreinterpret_castconst_castBaseb;Derived;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);一十三.HowC+implementslatebindingVTABLEVPTR一十四.StaticmembersclassAstaticintm;intn;Aa1,a2;A1.m=7;A*ap=newA;coutmm+=12;coutm”,”;deleteaplcouta1.m;一十五.NamespacenamespaceAintm;namespaceBintm;intn;voidFb()m=3;n=4;voidFa()B:

m+=7;m=23;namespaceBintm;intm;voidF()m=8;A:

m=9;B:

m=10;A:

B:

m=10;一十六.Pointertoclassmember一十七.MultipleinheritanceclassBase1inti;classBase2inti;classDerive:

publicBase1,publicBase2floatf;intgetInt()returni;floatgetFloat()returnf;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2