《面向对象程序设计C++》(双语课件)Lecture 2 A Tour ofC++.ppt

上传人:e****s 文档编号:85458935 上传时间:2023-04-11 格式:PPT 页数:37 大小:96.50KB
返回 下载 相关 举报
《面向对象程序设计C++》(双语课件)Lecture 2 A Tour ofC++.ppt_第1页
第1页 / 共37页
《面向对象程序设计C++》(双语课件)Lecture 2 A Tour ofC++.ppt_第2页
第2页 / 共37页
点击查看更多>>
资源描述

《《面向对象程序设计C++》(双语课件)Lecture 2 A Tour ofC++.ppt》由会员分享,可在线阅读,更多相关《《面向对象程序设计C++》(双语课件)Lecture 2 A Tour ofC++.ppt(37页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Lecture 2:A Tour of C+A Tour of C+Main ContentsWhat is C+Procedural programmingModular programmingData abstractionObject-Oriented Programming2A Tour of C+History of C+Developed by Dr.Bjarne Stroustrup at AT&T Bell labs in the early 80sOriginally called C with Classes1985 First external C+release1990

2、 first Borland C+release,The Annotated C+Reference Manual1995 Initial draft standard released1997 Formally approved international C+standard is accepted ISO/ANSI C+3A Tour of C+C versus C+C is the base language for C+.It is:versatileexcellent for system programmingruns everywhere and on everythingC

3、has evolved,partly under the influence of C+.Example,the use of f(void).“Good C programs tend to be C+programs,”-Bjarne Stroustrup.For example,every program in the C programming language is a C+program.4A Tour of C+What is C+?C+is a general-purpose programming language with a bias towards system pro

4、gramming thatIs a better C,Supports data abstraction,Supports object-oriented programming,andSupports generic programming5A Tour of C+Procedural ProgrammingDecide which procedures you want;use the best algorithms you can find.C+supports for procedural programmingVariables and arithmeticTests and loo

5、psPointers and arrays6A Tour of C+/C+程序:求两个数的最大值#include float max(float a,float b)float c;if(ab)c=a;else c=b;return c;Typical example:7A Tour of C+void main()float x,y,z;coutxy;z=max(x,y);cout最大数是:;coutzn;8A Tour of C+Variables and arithmetic fundamental types:bool /Boolean,possible values are true

6、 and falsechar /character,for example,a,z,and 9int /integer,for example,1,42,and 1216arithmetic operators+/plus,both unary and binary-/minus,both unary and binary*/multiply/divide%/remainder9A Tour of C+Variables and arithmetic(cont.)comparison operators:=/equal!=/not equal /greater than=/greater th

7、an or equalC+performs all meaningful conversions between the basic types so that they can be mixed freely.10A Tour of C+Tests and loopsbool accept()cout answer;/read answerif(answer=y)return true;return false;void main()bool result;result=accept();coutresultendl;11A Tour of C+bool accept3()int tries

8、=1;while(tries 4)cout answer;/read answerswitch(answer)case y:return true;case n:return false;default:cout Sorry,I don t understand that.n;tries=tries+1;cout I ll take that for a no.n;return false;12A Tour of C+Pointers and arraysAn array can be declared like this:char v 1 0;/array of 10 charactersS

9、imilarly,a pointer can be declared like this:char *p;/pointer to characterA pointer variable can hold the address of an object of the appropriate type:p=&v3;/p points to vs fourth elementConsider copying ten elements from one array to another:void another _ function()int v1 1 0;int v2 1 0;/.for(int

10、i=0;i1 0;+i)v1i=v2i;13A Tour of C+Modular ProgrammingWith an increase in program size,the emphasis in the design of programs has shifted from the design of procedures toward the organization of data.Decide which modules you want;partition the program so that data is hidden within modules.14A Tour of

11、 C+Separate compilationC+support Cs notion of separate compilation.This can be used to organize a program into a set of semi-independent fragments.Example:namespace Stack void push(char);char pop();15A Tour of C+#include namespace Stack const int max_size=200;char vmax_size;int top=0;void Stack:push

12、(char c)vtop+=c;char Stack:pop()return v-top;16A Tour of C+#include#include void main()Stack:push(s);if(Stack:pop()!=s)cout Impossible!n;else cout Good!n;17A Tour of C+Data AbstractionModularity is a fundamental aspect of all successful large programs.However,modules in the form described previously

13、 are not sufficient to express complex systems cleanly.User-Defined TypesConcrete TypesAbstract Types18A Tour of C+User-Defined TypesDecide which type you want and provide a full set of operations for each type.class complexprivate:double real,imag;public:complex(double r,double i)real=r;imag=i;frie

14、nd complex operator-(complex,complex);double abscomplex();double GetReal()return real;double GetImag()return imag;19A Tour of C+complex operator-(complex a,complex b)return,);double complex:abscomplex()double t;t=real*real+imag*imag;return sqrt(t);void main()complex A(1.1,2.2),B(3.2,0.3);/定义类的对象定义类的

15、对象A时调用构造函数时调用构造函数 complex C=A-B;cout=()t;cout=()endl;cout abs of complex A=()endl;20A Tour of C+Abstract Types Sole purpose is to provide a base class for other classes by declaring one or more virtual functions as“pure”by initializing the function to zero.class Stack public:virtual void push(char c

16、)=0;virtual char pop()=0;class Underflow;/used as exception class Overflow ;/used as exception ;21A Tour of C+class List_Stack:public Stackpublic:List_Stack();List_Stack();void push();_ front(c);char pop();private:list lc;/(standard library)list of characters;The:public can be read as is derived fro

17、m,implements,and is a subtype of.22A Tour of C+char List_ Stack:pop()char x=();/get first element lc.pop_ front();/remove first element return x;void g()ListStack ls;f(ls);23A Tour of C+Object Orientation Programming(OOP)OOP Encapsulates data(attributes)and functions(behavior)into packages called cl

18、assesData and functions closely relatedUnit of C+programming:the class A class is like a student reusableObjects are instantiated(created)from the class for example,Xiao Ming is an instance of a“student class”C programmers concentrate on functions24A Tour of C+Classes have variables which describe t

19、he current state of the object.Changing the state of an object is done through the class functions.Every time we declare an object we create a new instance of the class.Classes and Objects25A Tour of C+Class HierarchiesThe inheritance mechanism provides a solution.Definition:It is a mechanism which

20、supports arrangement classification in C+programming.And it allows the programmer to explain the class in detail based on keeping the characters of original class.New classes created from existing classesAbsorb attributes and behaviors.26A Tour of C+class Shapepublic:Point where()return center;void

21、move(Point to)center=to;draw();virtual void draw()=0;virtual void rotate(int angle)=0;private:Point center;Color col;/.;27A Tour of C+class Circle:public Shapepublic:void draw()/*.*/void rotate(int)private:int radius;void rotate_all(vector&v,int angle)for(int i=0;irotate(angle);28A Tour of C+Now the

22、 programming paradigm is:Decide which classes you want;provide a full set of operations for each class;make commonality explicit by using inheritance.29A Tour of C+Generic ProgrammingDecide which algorithms you want;parameterize them so that they work for a variety of suitable types and data structu

23、res.Generic containerGeneric algorithms30A Tour of C+Containers Templates provide direct support for generic programming,that is,programming using types as parameters.The C+template mechanism allows a type to be a parameter in the definition of a class or short,the variable type is a parameter.31A T

24、our of C+template class Stackpublic:Stack(int s);Stack();void push(T);T pop();class Underflow;class Overflow ;private:T*v;int maxSize;int top;32A Tour of C+template Stack:Stack(int size)if(size 10000)maxSize=10000;else maxSize=size;v=new T maxSize;top=0;template Stack:Stack()delete v;33A Tour of C+#

25、include#include Stack sc(20);Stack si(10);int main(int argc,char*)sc.push(c);si.push(12345);if()!=c)std:cerr Impossible!n;else std:cerr Good!n;if()!=12345)std:cerr Impossible!n;else std:cerr Good!n;return 0;34A Tour of C+Generic Algorithms The C+standard library provides a variety of containers,and

26、users can write their own.Thus,we find that we can apply the generic programming paradigm once more to parameterize algorithms by containers.35A Tour of C+AdviceDont panic!All will become clear in time.You dont have to know every detail of C+to write good programs.Focus on programming techniques,not on language features.36A Tour of C+Had enough?37

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

当前位置:首页 > 管理文献 > 管理手册

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁