C++课件达内科技内部资料.ppt

上传人:牧羊曲112 文档编号:6154046 上传时间:2023-09-30 格式:PPT 页数:505 大小:1.24MB
返回 下载 相关 举报
C++课件达内科技内部资料.ppt_第1页
第1页 / 共505页
C++课件达内科技内部资料.ppt_第2页
第2页 / 共505页
C++课件达内科技内部资料.ppt_第3页
第3页 / 共505页
C++课件达内科技内部资料.ppt_第4页
第4页 / 共505页
C++课件达内科技内部资料.ppt_第5页
第5页 / 共505页
点击查看更多>>
资源描述

《C++课件达内科技内部资料.ppt》由会员分享,可在线阅读,更多相关《C++课件达内科技内部资料.ppt(505页珍藏版)》请在三一办公上搜索。

1、The C+Programming LanguageChapter 1,C+Programming in UNIX,课程介绍C+语法基础面向对象程序设计的概念大量的编程实践目标熟练掌握C+语法具有面向对象程序设计的概念与能力能熟练阅读复杂的C+程序源代码能独立的设计与完成面向对象的C+程序,课程内容简介 1,C+语言基础保留字变量,常量表达式语句函数程序的结构数据结构与算法数组、指针、引用、结构、链表与栈,课程内容简介 2,C+面向对象编程类构造函数与析构函数静态成员与友员函数重载继承与多态I/O流模板异常,程序设计语言介绍1,What computer understand?bitsAsse

2、mbler LanguageLimited structureGlobal scopeMachine codePrimitive High-Level LanguageFunction decompositionData separationHigh level structure,程序设计语言介绍2,Block Structured LanguageEncapsulationFlexible data scopingModularizationObject-Oriented LanguageInheritancePolymorphismAbstract data types,C+程序设计语言

3、,1972,AT&T,Bell Lab.Dennis Ritche,C language1980,Bell Lab.Bjarne Stroustrup,C extension,1983,C+named1997,ANSI(American National Standards Institute)C+(standard C+),What C+will we learn in this course?,Standard C+:ANSI C+is more scalable to different platform such as Unix,Microsoft windows,MacThe sta

4、ndard C+library got supported by most of industry providers.Youd better dont know C.?We will try to avoid C library and C syntax.A C+file can be with.cc,.cp,.cpp extensions.,为什么选标准 C+,ANSI 规范了C+的标准,使之具有高度的可移植性。程序能够运行得很快,可直接操作系统资源,保持机器层次的实现细节。不要求图形环境,对系统要求相对较低。易于解决与旧工程的接口以及在数据库,存储和性能方面的技术限制。C+是一种面向对象

5、的多范型语言,可以将面向对象的模型映射成为C+的结构。它为开发者设计和编写一个解决方案提供了一定的选择范围。,C+的优点,Supports data abstraction and object-oriented programmingContains all existing features of C,making the migration from C to C+relatively easyIs as portable and efficient as CCan be linked to existing C libraries and functionsProvides stron

6、g static-type checkingIs a general-purpose language,程序员应该具备的计算机知识1,操作系统与应用程序运行环境与运行机制系统与命令运行环境与命令行参数进程栈堆,Binary and Hexadecimal,Binary:0101 1000Decimal:88Hexadecimal:0 x581 byte=8 bits.,程序员应该具备的计算机知识2,编辑器编译器编译器的功能解释执行与编译执行的差别熟悉自己常用的编译器,查错能力连接器库与库函数系统调用,软件开发周期,Software Lifecycle,开发测试维护更新,熟悉你的环境,可用的UN

7、IX服务器192.168.0.21 Telnet命令介绍:Login:use your registered user account.Password:type in your pass word.,创建自己的学习帐号,telnet,login:tarenaPassword:tarena欢迎使用达内科技(中国)公司开放实验室的服务!Welcome to the OpenLab of Tarena Technologies Inc.Cananda.请按照以下提示创建您的用户帐号.Please follow the steps to create your own account.(请输入您要注

8、册的帐号)Please enter your new account name:XXXXXXXX(请输入您的E-Mail地址)Please enter your email address:,用自己的帐号登录UNIX服务器,Escape character is.SunOS 5.8login:XXXXXXXChoose a new password.New password:*,程序员经常用到的UNIX命令1,简单的文件维护与管理ls,cd,mkdir,rm,cp,mv,cat,more源程序的编写vi,ed编译与连接gcc,g+,ld运行与调试adb,gdb,程序员经常用到的UNIX命令2,

9、查看运行状态%ps ef%grep aaa a.txt(aaa is the chars in the file name a.txt)%prstat(ctrl D to exit)%kill pid(pid is a process id),第一个UNIX上的C+程序,用vi编辑器来编写hello.cc源程序%vi hello.cc,/*the first C+program*/#include using namespace std;/main functionint main()cout Hello world!endl;cout This is my first C+program.n

10、;,g的常用参数,-c 编译成目标文件.o-o指定输出文件名,输出文件名跟在-o后面,用空格分隔。如果不使用这个选项,缺省的输出文件名为a.out。-g产生有调试信息的可执行文件-w不产生警告信息-l 连接指定的库文件-L指定库文件的路径-i 要包含的头文件-I 头文件的路径-E 显示预处理后的程序文件到屏幕上,可以用-o指定输出到文件-S 产生汇编程序如果没有c、E、S就会生成可执行文件,编译hello.cc,%g+-c hello.cc%ls,连接hello.o,%g+-o hello hello.o%ls%g+hello.o%ls,运行hello 程序,%hello%a.out,C+程序

11、的基本结构1,/*the first C+program*/#include using namespace std;/main functionint main()cout Hello world!endl;cout This is my first C+program.n;,C+程序的基本结构2,#include 与#include Name space:提供了一个全局标识符和全局变量所在的作用域。int main()注释函数函数的调用cout语句,头文件,#include语句#include 与#include,使用#include using namespace std;少用#incl

12、ude,Main函数,main函数的作用Standard C+main()格式:int main()return 0;/the default return value is 0;,注释,C+的注释,基本的输出语句,cout,练习程序hi.cc,#include using namespace std;int main()cout Hi Jian!endl;cout Have a nice day.endl;return 0;,练习程序myself.cc,编写一个程序,打印出自己的:姓名性别年龄家庭住址电话号码爱好每一条信息输出为一行,在hi.cc中使用字符串,#include using n

13、amespace std;int main()char name=John;cout Hi name!endl;cout Have a nice day.endl;return 0;,字符与字符串类型,字符,字符串/字符数组char ch=A;char str120=Hello world!;char str2=Have a nice day!;,不同的main()格式,命令行参数%ls l(or ls-al)%vi hello.cc在程序中使用命令行参数int main(int argc,char*argv),命令行参数程序cmdline.cc,#include using namespac

14、e std;int main(int argc,char*argv)for(int i=0;iargc;i+)cout argv i=argvi endl;,使用命令行参数的hi.cc,使用命令行参数,重新编写练习程序 cmdline.cc%hi John%hi Lisa%hi G.Bush,基本输入语句,cin语句使用cin语句的hi.cc重写hi.cc程序,不带命令行参数程序自动提示用户输入字符串来获得姓名与年龄,练习程序age.cc,#include using namespace std;int main()unsigned int age;char name 50;cout name

15、;cout age;cout your name is:name endl;cout You were age-2 years old two years ago.n;,条件语句,if语句ifelse语句不同if的等价与不等价形式 char ch;cin ch;if(ch=y)/note the difference:if(ch=y)cout good endl;else cout try again.endl;,练习程序grade.cc,This is your assignment.,Q&A,Thank You!,The C+Programming LanguageChapter 2,字符

16、集,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0 1 2 3 4 5 6 7 8 9_+-*/%=.,:?|!#&()空白,C+的保留字,auto,bool,break,case,catch,char,class,const,const_cast,continue,default,delete,do,double,else,enum,extern,false,float,for,friend,goto,if,inline,int,long,new,operator,private,protected,public,return,sh

17、ort,signed,sizeof,static,struct,switch,template,this,throw,true,try,typedef,union,unsigned,virtual,void,while,常量与变量,内存程序的内存使用常量变量动态内存变量的类型,C+变量名,变量名(identifier)第一个字符必须是字母或下划线只能使用字母,数字,或下划线中间不能有空格不能是保留字,不能与全局函数和数据类型同名C+严格区分大小写(UNIX中)使用易懂的变量名(一般是相关的英语单词或者缩写)长度一般不要超过32个字符不单是变量名,所有需要自己起名的地方都需要遵守这样的规则。,C

18、+变量,C+是强类型语言每一个变量都有确定的类型,且保持不变基本数据类型整型,int,字符型,char,实型,float,double,逻辑型,bool(standard c+new feature!),基本数据类型1,char,unsigned char,signed char,int,unsigned int,signed int,short int,unsigned short int,signed short int,long int,signed long int,unsigned long int,float,double,long double,boolvoid:enum,str

19、uct,union,array,pointer,class,基本数据类型2,实型数据(float,double)无unsigned.,The standard C+new features,bool:其值必为关键字true 或false 四个转型运算子:static_cast:compiling time to check data type(primitive).const_cast:only for constant data type converting.dynamic_cast:usually for top-down data type cast.reinterpret_cast:

20、more general cast for all data types.(到多态的时候再详细讲),变量与数据类型,C+是强类型语言先声明,后使用C+编译器对变量声明的处理,一个使用变量的程序例子,#include using namespace std;int main()int i;i=5;cout i=i endl;i=8;cout i=i endl;return 0;,另一个使用变量的例子程序,#include using namespace std;int main()i=5;/see what happenscout i=i endl;i=8;cout i=i endl;retur

21、n 0;int i;,变量与变量的size,变量都有类型变量在内存中的大小int i;double d;cout size of i is sizeof(i)endl;cout size of int is sizeof(int)endl;cout size of d is sizeof(d)endl;cout size of double is sizeof(double)endl;,程序size.cc,编写一个程序,打印出所有C+基本类型的大小#include using namespace std;int main()cout size of char is:sizeof(char)en

22、dl;cout size of unsigned char is:sizeof(unsigned char)endl;cout size of signed char is:sizeof(signed char)endl;cout size of int is:sizeof(int)endl;cout size of unsigned int is:sizeof(unsigned int)endl;cout size of signed int is:sizeof(signed int)endl;cout size of short int is:sizeof(short int)endl;,

23、变量的取值范围,变量的类型与值的范围常用类型的取值范围,常量,常量与常量的数据类型const double pi=3.14,const限定符,限定一个常量或者函数方便编译器来检测非法的修改操作,运算符,运算符+,-,*,/,%,+,-,=,=,结合性优先级:see table 3-1 in page 35 in the recommended book 1.,运算符的使用,if(demo=2)与 if(demo=2)if(2=demo)/左值与右值。if(demo!=2)与 if(demo=!2),运算符的优先级,int a=8,b=4,c=5;cout(a%b?b:c);cout a%b?b

24、:c;,变量的赋值,赋值表达式变量的初始化一次声明多个变量声明并初始化变量,无符号类型的值,无符号整数类型的回绕unsigned short int snum;snum=65535;cout snum=snum endl;snum=snum+1;cout snum=snum endl;,有符号类型的值,有符号整数类型的回绕int inum=2147483647;cout inum=inum endl;inum=inum+1;cout inum=inum endl;,常用类型的取值范围,常用类型的取值范围int i=65535;int j=65535;cout i*j/9 endl;,练习,为表

25、示如下数据,应该使用什么类型的变量:年龄姓名工资电话号码身份证号码西三环到东三环的距离,练习程序bin.cc,#include using namespace std;int main()int a=10;cout a;unsigned int r;int k;unsigned int j;char str33;memset(str,0,33);str32=0;r=a;int i=32;,练习程序bin.cc,do j=r;r=r/2;k=j-r*2;if(k)str-i=1;else str-i=0;while(r!=0);cout str endl;,枚举类型,enum colorRED,

26、GREEN,BLUE,WHITE,BLACK;color tvColor=GREEN;Enum constant variable does not need memory allocation.The default value in Enum is:0,1,2,3,4.Specify the element values:enum color RED=100,GREEN=200,BLUE,WHITE=300,BLACK=400,表达式,左值与右值float a;a=5/2;+a;求值顺序特殊表达式:(?:)逗号表达式:int a,b,c;a=1,b=a=2,c=b+3;,表达式的求值顺序,

27、求值顺序副作用,表达式的左值与右值,int a,b,c,d;int e=(a=1,b=a,c=a+b,d=c+5);(a=1,b=a,c=a+b,d=c+5)=8;e=(a=0,b=a+5,b+2);(a=0,b=a+5,b+2)=9;/ohps,you may get problem.,练习程序comma.cc,#include using namespace std;int main()int a,b,c,d;int e=(a=1,b=a,c=a+b,d=c+5);(a=1,b=a,c=a+b,d=c+5)=8;cout d=d endl;,程序语句,控制语句表达式语句空语句语句块,控制语

28、句,条件判断语句if()if()else 循环控制语句while()do while();for()多路选择语句switch()case:,循环语句,for语句dowhile语句while语句break语句continue语句,分支语句,switch语句switch(ss)case 1:break;case 2:break;default:break;,循环语句程序例子 chengFa.cc,编写一个程序,打印出乘法口诀表分别使用for语句,dowhile语句,while语句来实现,The output is:1x1=11x2=2,2x2=41x3=3,2x3=6,3x3=91x4=4,2x4

29、=8,3x4=12,4x4=161x5=5,2x5=10,3x5=15,4x5=20,5x5=251x6=6,2x6=12,3x6=18,4x6=24,5x6=30,6x6=361x7=7,2x7=14,3x7=21,4x7=28,5x7=35,6x7=42,7x7=491x8=8,2x8=16,3x8=24,4x8=32,5x8=40,6x8=48,7x8=56,8x8=641x9=9,2x9=18,3x9=27,4x9=36,5x9=45,6x9=54,7x9=63,8x9=72,9x9=81,练习程序year.cc,This is your assignment.判断输入的年份是否是闰年

30、。,Q&A,Thank You!,The C+Programming LanguageChapter 3,函数,什么是函数函数的基本要素参数返回值函数的声明与定义形参与值参函数的调用,定义函数,函数名()return,函数定义的例子,#include using namespace std;void disp(char str)cout This is your string:str endl;int main()char course1=C+;char course2=Java;char course3=Oracle;char course4=UNIX;disp(course1);disp(

31、course2);,函数声明,函数名();void disp(char*);float average(int,int);float average(int a,int b);为什么需要函数声明?,调用函数,函数的形参函数的调用过程填入值参获得返回值,栈的技术简介,栈的工作原理函数的调用与栈,栈的原理,i=f1();j=f2();cout i endl;cout j endl;,cout f1()endl;cout f2()endl;,#include using namespace std;int f1();int f2();int main()int i,j;i=f1();j=f2();c

32、out i endl;cout j endlint f1()int n=5000;return n;int f2()int n;return n;,变量的作用域,局部变量与全局变量,默认参数,函数的形参可以指定默认值必须从右到左指定参数的默认值函数在调用时,是按从左到右的顺序在匹配参数,使用默认参数的函数例子,enum SexMALE,FEMALE;void disp(char*name,Sex gender=MALE);如果在函数声明中指定了默认值,则在函数定义时不能再指定,内联函数,提高程序运行效率内联函数的定义inline int isnumber(char ch)return(ch=0

33、 必须先定义,不支持函数原形(声明)不支持结构控制语句,递归函数,一个函数调用它自己如何正确的递归必须有结束的条件并且该条件一定能够满足,使用与不使用递归的例子程序,编写一个程序bigsum.cc,使用一个函数来求n的值:#include using namespace std;int bigsum(int a)if(a=0)return 0;return a+bigsum(a-1);int main()int n;cout n;int m=bigsum(n);cout The sum is:m;cout endl;,练习程序nbang.cc,使用递归函数,编写一个程序来求n!的值:This

34、is your assignment,函数的重载,C+中的函数可以重载什么是函数的重载:对于在不同类型上作不同运算而又用同样的名字的情况,称为重载。函数重载的注意事项:重载函数至少在参数个数,参数类型,或参数顺序上有所不同。,函数重载的例子,求两个数的平均值double Average(int,int);double Average(float,float);double Average(double,double);double Average(long,long);,思考题,在一个程序中定义了这两个函数会怎样?int Area(int width,int length=1);int Are

35、a(int size);,Area.cc source code,#include using namespace std;int Area(int width,int length=1)return width*length;int Area(int size)/int Area(int size,int leng)is not allowed return size*size;int main()cout Area(3,5)endl;/set length=5 instead.cout Area(10)endl;/is this ok?,函数参数的const限定,可以使用const限定词来

36、修饰形参以保护实参不被修改。const形参的意义#include using namespace std;void disp(const int I)cout I endl;I=100;/think about this.int main()disp(50);,程序的结构,多文件结构外部变量与内部变量变量的作用域与可见性头文件静态全局变量静态函数,多文件结构,按不同的功能模块将程序的源代码划分在多个文件中不同源文件之间可以共享变量声明与类型定义C+多文件的划分原则,外部变量与内部变量,存储类:auto,extern,register,static,volatile.什么是外部变量什么是内部变量

37、,变量的作用域与可见性,局部变量的作用域静态局部变量的作用域全局变量的作用域外部变量的作用域常量的作用域,头文件,头文件的作用如何组织头文件头文件的使用编译选项,静态全局变量,何为静态全局变量只在本源文件中可用,静态函数,定义静态函数只在本源文件中可用,改写bigsum.cc与nbang.cc程序,将bigsum.cc与nbang.cc改写成多文件结构,每个文件只能有一个函数,改写bigsum.cc与nbang.cc程序,分别将前面的函数改为静态函数,再次编译并运行所有的程序This is your assignment,练习程序hash.cc,#include using namespace

38、 std;int main()char line100;cout line;int ch=0;for(int i=0;istrlen(line);i+)ch+=linei;cout The hash of your string is:ch endl;,练习程序mywc.cc,输入一行文字,统计单词的个数。为了输入的时候能包含空格,程序使用了getline函数。#include using namespace std;int main()cout Please enter a line:endl;char line120;int cnt=0;cin.getline(line,120);int

39、i=0;,练习程序mywc.cc,while(istrlen(line)if(linei!=)cnt+;while(linei!=),Q&A,Thank You!,The C+Programming LanguageChapter 4,复杂数据类型,数组结构指针,数组1,由若干个同类型变量组成的集合数组的声明 数组名元素个数;下标是数组元素到开始的偏移量数组下标从0开始char buf4;,数组2,数组在声明时,元素个数必须是常量或常量表达式char buf10;int I;char bufI;/?int I=10;char bufI;/?const int i=10;char bufi;ch

40、ar bufi+1;,数组3,如果数组的声明带有初始化,可以直接对整个数组赋值访问数组元素,使用下标操作符 int iA10;iA0=0;iA1=1;int I=0;I=iA0+iA1;,数组的初始化,在声明的时候就初始化int iA5=0,1,2,3,4;int iB=1,2,3;使用赋值语句初始化数组iA0=0;iA1=1;数组的边界问题int iC5;iC10=100;/run time?Question:does C+compiler have array bound checking?,数组程序例子,编写一个程序,从键盘接受一个字符串,将该字符串颠倒顺序,然后打印出来,练习程序fin

41、dmax.cc,#include using namespace std;int main()int iA=103,5,68,115,32,23,66,599,38,444;for(int i=0;i 10;i+)for(int j=0;j i;j+)int temp;if(iAi iAj),/if(iAi iAj)temp=iAi;iAi=iAj;iAj=temp;cout The bigNumber is:iA9 endl;,多维数组,二维数组与多维数组:int iA510;int iB23=1,2,3,4,5,6;int iC246;多维数组的初始化仅有第一个维数可以省去int iB 3

42、=1,2,3,4,5,6,7,8,9;,数组练习程序mdim.cc,#include using namespace std;int maximum(int 4,int,int);int main()int sg34=68,77,73,86,87,96,78,89,90,70,81,86;cout the max grade is maximum(sg,3,4)endl;,int maximum(int grade 4,int row,int col)int max=grade00;for(int i=0;i max)max=gradeij;return max;,结构1,将不同类型的相关数据

43、信息组织在一起是用户自定义的类型需要先声明类型的定义才能使用结构与数组的区别数组只是同一个数据类型的聚集数组本身不是一个新的数据类型,结构2,struct;(结构变量名);struct Person char name20;unsigned long id;float salary;char address200;p1,p2;,结构的赋值1,通过取成员操作(.)来引用结构变量的元素Person p1=G.W Bush,1000010,1.5,ZhongGuanChun,Beijing,China;strcpy(p1.name,G.W Bush);p1.id=1000010;p1.salary=

44、1.5;strcpy(p1.address,ZhongGuanChun,Beijing,China);,结构的赋值2,结构赋值的例子Person p1=G.W Bush,1000010,1.5,ZhongGuanChun,Beijing,China;Person p2=p1;,结构的存储模式1,每一个成员都有自己的存储空间对每一个成员的操作都是独立的,各元素间不会相互影响,结构的存储模式2,#include using namespace std;struct Person char name20;unsigned long id;float salary;int main()Person p

45、1=Zhang Weilong,1000101,32;cout,What are pointers for?,Accessing array elements.Passing arguments to a function when the function needs to modify the original argument.Passing arrays and strings to functions.Obtaining memory from the system.Creating data structures such as linked lists.,Pointer,addr

46、ess,variable,int theVariable=5;int*pPointer=,5,2000,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,address,theVariable,pPointer,指针1,编译器为变量与常量分配存储空间任何存储空间都用地址来表示任何变量/常量都有地址一个变量的地址也是一个有意义的值变量地址也可进行运算这个值也可以用另一个变量来存储这另一个变量的类型就是指针类型,指针2,指针就是用来存储其他变量的地址的变量指针变量自己也有地址指针是有类型的指针的类型要跟它所指向的变量的类型一致整数指针,浮

47、点数指针,理解指针的例子程序addr.cc,定义下面的变量,分别输出它们的值与他们的存储地址int iVal1=1;int iVal2=2;double dVal1=1.1;double dVal2=2.2;,指针的定义,*;int*ip;const int*icp;char*str;NULL指针,指针的操作,取地址操作符,指针的类型,整数型变量int iVal;iVal=10;整数指针性变量int*iPtr;iPtr=Any data type in c+can be a pointer type.,指针的使用,指针在使用前必须初始化指针的类型决定了指针所参与的运算指针只能进行加减法运算,理

48、解指针的例子程序 ptracc.cc,#include using namespace std;int main()int var1=11;int var2=22;int*ptr;ptr=,理解指针的例子程序 ptracc.cc,Var1(11),Var2(22),Var1(11),Var2(22),*ptr is 11,*ptr is 22,ptr,ptr,指针与数组,数组的本质就是数组里所有变量的首地址数组的下标操作就是针对地址,指针与数组的例子程序 ptr.cc,分别用数组的方式与指针的方式来操作int iv5分别给他们赋初值并输出他们的值与地址:#include using names

49、pace std;int j;int iv5=1,2,3,4,5;int main()int a=800;int i;int b=900;int*ptr=,指针与数组的例子程序 ptr.cc,cout,结构指针,结构是一种数据类型结构类型也有指针结构变量vs结构指针变量结构指针变量的操作结构变量用(.)操作来存取成员结构的指针用(-)操作来存取成员,结构数组,数组的元素是某一个结构类型struct Person char name20;unsigned long id;float salary;char address200;;Person allPeople100;,结构中的数组元素,结构中

50、的某个元素是数组类型struct Person char name20;unsigned long id;float salary;char address200;;Person Jack=Jack,2L,8000.00,Toronto;,指针作形参,利用指针在函数中传递参数,程序swap1.cc,#include using namespace std;void swap(int,int);int main()int a=3,b=8;cout a=a,b=b endl;swap(a,b);cout after swapping n;cout a=a,b=b endl;void swap(in

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号