教学课件·《C++程序设计简明教程(第二版)_第1页
教学课件·《C++程序设计简明教程(第二版)_第2页
教学课件·《C++程序设计简明教程(第二版)_第3页
教学课件·《C++程序设计简明教程(第二版)_第4页
教学课件·《C++程序设计简明教程(第二版)_第5页
已阅读5页,还剩186页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、121世纪高等院校计算机系列教材 C+程序设计简明教程 (第二版) 教学课件 2教 学 主 要 内 容 第 1 章 C+基础 第 2 章 C+流程控制语句 第 3 章 指针、结构体和联合体 第 4 章 函数 第 5 章 C+类 第 6 章 C+运算符的重载 第 7 章 继承和模板 第 8 章 C+流与文件3第 1 章 C+概述主要内容: C+程序举例 C+程序组成、运行 数据类型 常量和变量 运算符与表达式4#includevoid main() coutHello world.n;程序1.1 简单C+程序举例程序输出结果:Hello world.5程序1.2 带注释的程序#includevo

2、id main()/*This is a comment and it extends until the closing star-slash comment mark .*/ coutHello,world!.n;/This comment ends at the of the line.程序输出结果:Hello world.6int add(int x,int y) coutIn add() received x and yn; return(x+y); void main()int a,b,c; coutIm in main()!n; couta; cinb; coutnCalling

3、 add()n; c=add(a,b); coutnBack in main().n; coutc was c;程序1.3 由两个函数组成的程序程序输出结果:Im in main()!Enter two numbers: 2 4Calling add()In add() received 2 and 4Back in main()c was 67程序功能:计算矩形面积class rect /建立计算矩形面积的rect类 public: int area(int a, int b ) return a*b; ;void main( ) rect s ; /建立rect类的对象s couts.ar

4、ea(3,9)endl ; /通过对象s计算矩形面积程序1.4 带类的C+程序程序输出结果:278#include /包含头文件fun1(参数列表) /函数fun1fun2(参数列表) /函数fun2class myclass / myclass类void main() /主函数C+程序结构9main()主函数是一个特殊的函数,是程序的入口。每一个C+程序必须有一个且只允许有一个main()函数可以把程序划分成若干个函数,每个函数完成一个专门的任务。程序中的各个函数必须有不同的函数名,不能重名。每个函数由函数名和函数体组成函数体由大括号之内的一系列语句组成。C+程序说明10 一个C+程序经过编

5、写、编译、连接、执行环节。 常见C+开发环境有: Turbo C+、Borland C+、 Visual C+等。C+程序的执行编写源程序编译得到目标程序执行可执行文件得到程序结果C+编译程序 库函数和其他目标程序 连接目标程序生成可执行文件文字编辑程序11数据和运算介绍: C+的标识符、保留字、数据类型 变量、常量、运算符和表达式等12(1)标识符只能由字母、数字和下划线组成,且标识符的第一个字符必须是字母或下划线。(2)标识符严格区分大小写字母,如a1和A1是两个不同的标识符。(3)不能用C+的保留字作标识符。如main、public、new等。 (4)应当使标识符能够具有一定的含义。 如

6、用标识符myphone表示电话号码。例 合法标识符: myclass2 myphone _name例 非法标识符: 2myclass public -nameC+对标识符的规定13无符号短整型unsigned short : 2字节 065535短整型short : 2字节 -3276832767整型int: 2字节 -3276832767无符号长整型unsigned long:4字节 04294967295长整型long: 4字节 -2,1474836482147,483647字符型char: 1字节 -128127浮点型float: 4字节 3.4e-383.4e38双精度型double:

7、8字节 1.7e-3081.7e308C+提供的数据类型14程序1.5 使用sizeof()运算符求字节数void main() coutThe size of an int is:sizeof(int)bytesn; coutThe size of a short int is: sizeof(short)bytesn; coutThe size of a long int is: sizeof(long)bytesn; coutThe size of a char is: sizeof(char)bytesn; coutThe size of a float is: sizeof(floa

8、t)bytes.n;程序输出结果: The size of an int is: 2bytes.The size of a short int is: 2bytes.The size of a long int is: 4bytes.The size of a char is: 1bytes.The size of a float is: 4bytes.15整型常量可采用十进制、八进制整数和十六进制表示。 八进制整数以数字0开头 如:012、-017十六进制整数以0 x或OX开头(数字0,字母x) 如:0 x64、-OX2A整型常量末尾添加一个大写字母L或小写字母l,表示长整型数 如:123L

9、、-123L、027L。在整型常量末尾添加写字母U或u表示无符号整型数 如:123U1整型常量16实型常量表示带小数的数值常量。又称为浮点常量或实数。实型常量只有十进制形式。如0.123、12.3、-123.0、-12.3实型常量可用科学计数法(指数形式)表示 如1.23e2或1.23E2,代表1.23102 如-1.23e2或-1.23E2,代表-1.23102实型常量17字符常量和转义字符 字符常量表示单个字符,要求用单引号将字符括起来 如A、a,2常用转义字符 含义 对应ASCII码b 退格 x08t 水平制表符tab x09n 换行 x0af 换页 x0cr 回车 x0d“ 双引号 x

10、22 单引号 x27 反斜线 x5cddd 用3位8进制数表示字符 如101 表示Axff 用2位16进制数表示字符 如x41 表示A18字符串常量 字符串常量是用双引号括起来的字符序列 如Hello、How are you ? 字符串中包含转义字符,如C:MY.CPP 用连接符(+)连接字符串 如: ABC+DE 如: ABC+DE+FG19符号常量用一个标识符表示数据(1)用#define定义常量 格式为:#define 常量名 字符串 例:#define students 36 void main() coutstudentsn;(2)用关键字const定义常量 格式为:const 类型

11、 常量名=常数; 例:const int student=36;符号常量20 枚举常量是一定范围的整数集合 说明枚举类型常量的格式: enum 枚举类型名称 枚举值1,枚举值2,;例如:enum color red,green,blue,black,white;把color定义为枚举类型。定义red、green、blue、black、white为符号常量,取值分别为0、1、2、3、4。作用是将程序中出现的符号替换为对应的整数。例如:符号red替换为整数0,符号green替换为整数1。枚举常量211.定义变量格式 数据类型 变量名1=初值,变量名2=初值;例 char result; /声明一个

12、存放字符、名为result的变量 int j; /声明一个int型整数变量 double x, y ; /声明两个double类型的变量 long m=0, n; /声明long型整数变量2. typedef语句 使用typedef语句为数据类型命名 即给某个数据类型再取一个名字变量22程序1.6 计算矩形面积#includevoid main() int length=38,width, area;width=10;area =length*width;coutlength:lengthn;coutwidth:widthn;coutarea:arean;程序输出结果:length:38wid

13、th:10area:38023typedef int ust;void main() ust length=38,width=10,area; coutlength:lengthn; coutwidth:widthn; area=length*width; coutarea:arean;程序1.7 使用typedef语句24相同类型的数据可以直接运算。不同类型的数据进行运算时,要将数据转换为同一类型再运算。数据类型转换分为自动转换和强制转换两种。自动转换从低到高的转换规则: 低 charintlongunsignedfloatdouble 高通过强制转换可以转换成指定类型。强制转换的方法是:(

14、数据类型)数据 例如: int k=4 ; double x=3.5, y ; y=x+(double)k; 数据类型转换25运算符与表达式 对于每个运算符要掌握它的功能、优先级和结合性 运算符的优先级决定了表达式中各个运算符的运算顺序 运算符的结合性决定了优先级相同运算符的运算顺序26程序1.8 算术运算举例void main() short i=13 ;int j=24; float x=2.34F; double y=1.2; coutj/i=j/iendl;couti%3=i%5endl;couti/2.0=i/2.0endl;coutj/5=(float)j/5endl;coutx+

15、y=x+yendl;程序输出结果:j/i=1i%3=3i/2.0=6.5j/5=4.8x+y=3.5427程序1.9 单目算术运算举例void main() char c=E ; int i=3, j=3; int m=i+ , n=+j , k=-j; coutm=m); coutn=n); coutk=k); -c ; k+; coutc=c); coutk= = =3 75 7!=8关系运算符29void main()int m=53 , n=24 ,k=24; char c1=A, c2=B;coutn=k is (n=k)endl; coutc2 is c2)endl; coutc1

16、!=c2 is (c1!=c2)endl;cout8 is 8)endl;cout86 is 86)c2 is 0c1!=c2 is 12!=58 is 11086 is 0程序1.10 关系运算举例30逻辑运算符 逻辑运算是对关系运算表达式进行运算。 名称 运算符 格式 逻辑与 & 表达式1 & 表达式2 逻辑或 | 表达式1 | 表达式2 逻辑非 ! ! 表达式 例如 43 & 76 | 75 !(7=8)31程序1.11 逻辑运算举例void main() int m=53 , n=24 ; char c1=A, c2=B,c3=C; coutc1&c2c3&c1c3)endl; cou

17、t2*2+1)endl; cout2 | 53 & !3):表达式3 例 43 ? 4:3 逗号运算符 表达式1,表达式2, ,表达式n 逗号表达式的值为最后一个表达式的值 例 43 ,x=9 ,x+35输入输出 输入输出是程序的基本功能 C+利用流进行输入输出 用于输入输出的流库包含在头文件iostream.h中36程序1.13 输出举例#includevoid main()cout1+2=1+2endl; cout1.3 2 C+endl;程序输出结果:1+2=31.3 2 C+37#includevoid main() coutThis is line one.nThis is line

18、 two.n; coutThis is line three.nThis is line four.n ; coutThis is line five.endlthis is line six.endl;程序输出结果:This is line one.This is line two.This is line three.This is line four.This is line five.This is line six.程序1.14 实现换行举例38程序1.15 数据以各种进制输出#includevoid main() cout将10输出为八进制数:oct10endl; cout将10输

19、出为十六进制数:hex10endl; cout将10输出为十六进制数:10endl; cout将10输出为十进制数:dec10endl; cout将10输出为十制制数:10endl;程序输出结果:将10输出为八进制数:12将10输出为十六进制数:a将10输出为十六进制数:a将10输出为十进制数:10将10输出为十进制数:10 39程序1.16 设置输出宽度和有效数字#include#includevoid main() coutsetw(2)ABsetw(4)ABsetw(5)ABendl; coutsetw(3)123setw(5)123setw(6)123endl; coutsetprec

20、ision(4)23.45678endl; coutsetprecision(5)23.45678endl;程序输出结果:AB AB AB123 123 12323.4623.45740输入 C+提供输入流 cin 利用输入流cin,从键盘输入数据,并赋给指定变量41程序1.17 使用输入流cin程序输出结果:Type two numbers and press Enter: 2 6The typed numbers were:2 and 6Type any character and press Enter: xThe letter typed was xvoid main()int fir

21、st,second; char letter; coutfirstsecond; coutThe typed numbers were:firstandsecondendl; coutletter; coutThe typed letter was letterendl;42数组用数组可以存储一组类型相同的数据。数组中的每个成员称为数组元素。数组要先定义后使用43一维数组 一维数组定义格式:数据类型 数组名数组元素个数; 例: int a4; double b5; 数组a包括4个数组元素:a0、a1、a2、a3 数组b包括5个数组元素:b0、b1、b2、b3、 b444定义一维数组同时给数组元

22、素赋值,称为一维数组初始化。数组初始化有两种格式: static 数据类型 数组名 =元素1初值,元素2初值,元素3初值; static 数据类型 数组名元素个数=元素1初值,元素2初值,元素3初值;例如:static int a =1,2,3,4 ;static int b5=1,2,3,4,5 ;static int c6=3,2+2,5 ;一维数组的引用:数组名下标例如 couta2“, “a3; 一维数组初始化和引用45程序1.18 一维数组定义和使用举例#includevoid main() int f =2,3,3+2; int k=2; cout数据元素f0值是f0endl; c

23、out数据元素f1值是f1endl; cout数据元素f2值是fkendl; f0=fk+4; f1=f1*2; cout三个数据元素之和f0+f1+f2endl; 程序输出结果:数据元素f0值是2数据元素f1值是3数据元素f2值是5三个数据元素之和是2046定义格式:数据类型 数组名行数列数;例如:int x34;定义3行4列的二维数组,数组名为x矩阵为: 0列 1列 2列 3列x00 x01 x02 x03 0行 x10 x11 x12 x13 1行x20 x21 x22 x23 2行引用二维数组元素的形式:数组名行下标 列下标二维数组47二维数组的初始化例如:static int b23

24、=1,3,5,2,4,6; 初始化后形成的矩阵为: 1 3 5 2 4 6 例如:static int b23=1,3,5,2,4,6; 例如:static int x33=1,2,3,4,5,6; 1 0 0 2 3 0 4 5 6 48程序1.19 二维数组定义和使用举例#includevoid main() int b23= 1,2,3,4,5,6 ; cout数据元素a00值是b00endl; cout数据元素b03值是b03endl; b23=2.8; cout数据元素b23值是b23endl;程序输出结果:数据元素b00值是1数据元素b03值是4数据元素b23值是249 第2章 C

25、+流程控制语句 if 语句 switch语句 for语句 while语句 do - while语句 break语句 continue语句 其他语句50 流程控制语句用于控制程序中各语句的执行顺序 C+流程控制语句有: 选择语句、循环语句、跳转语句等。流程控制语句51if 语句if语句是二分支选择语句if语句可以给出两种操作,由表达式结果(非0 或0)选择其中的一种操作。if语句有以下格式:if (表达式) 语句;if (表达式) 语句1; else 语句2;if (表达式) 语句; 语句; if (表达式) 语句; 语句; else 语句; 语句; 52 程序2.1 判断键盘输入的整数是否为偶

26、数,是输出is,不是输出not#includevoid main( ) int x ; cinx ; if (x%2=0 ) coutis; else coutnot; 53程序2.2 从键盘输入一个正整数,判断整数是否为1、7、13、19#includevoid main( ) int x ; cinx ; if (x%6=1) coutis; else coutc ; if (c=a & c=z) cout=a & c=z) cout是小写字母; else cout是其他字符;程序2.3 ifelse语句的嵌套形式55switch 语句switch语句是多分支选择语句。使用switch语句

27、可以给出多种操作,根据表达式的值从中选择一种操作。switch语句格式:switch(表达式) case 常量表达式1: 若干语句; break; case 常量表达式2: 若干语句; break; case 常量表达式n: 若干语句; break; default : 若干语句; 56Switch 语句注意事项 (1)每个case表示一个分支。根据switch表达式的值决定选择哪个case分支。break语句是switch的出口,作用是当执行完一个case分支后,跳出switch语句,继续执行switch语句后面的语句。 (2)switch后面的表达式可以是整型表达式或字符表达式。case后

28、面的数据必须是常量或常量表达式。各个case常量表达式的值不能相等。否则发生冲突。 (3)各个case出现的次序不影响语句执行结果。5760分以下为D等,6069为C等,7089为B等,90100为A等void main() int score=87; switch (score/10) case 0: case 1: case 2: case 3: case 4: case 5: coutscore +分是D等endl; break; case 6: coutscore +分是C等endl; break; case 7: case 8: coutscore +分是B等endl; break;

29、 case 9: coutscore +分是A等endl; break; default : cout“数据错误”; 程序2.6 根据变量score中的考试分数,输出对应的等级58循环语句C+提供的循环语句有:for语句while语句do while语句 59for 语句for语句通常用于构造重复次数固定的循环。格式为: for (表达式1; 表达式2; 表达式3) 循环体语句块 例如: for (int i=1; i=10; i+) cout你好endl;60程序2.7 输出1到100之间的所有整数#includevoid main() int counter; for (counter=1

30、;counter=100; counter+) coutcounter ; cout n;61void main() int counter; for (counter=1;counter=100;counter+=5) coutcounter ; cout n; 程序2.8 每隔5个数,输出1到100之间的整数62void main()long int n=1; int i,num; coutnum; for (i=1;i=num;i+) n=n*i; couti!=nendl; 程序输出结果:Enter a number:61!=12!=23!=64!=245!=1206!=720程序2.

31、9 计算正整数的阶乘63while语句while语句用于构造循环次数由条件控制的循环。while语句的格式: while (表达式)循环体语句块64 程序2.13 计算圆周率,计算精度是1e-06#includeiostream.h#includevoid main( ) double sum=0, newdatum=1.0; int n=1; while (newdatum1e-6) sum=sum+newdatum; n+; newdatum=1.0/(n*n); coutpi is sqrt(sum*6)endl; 65 do while 语句 do while语句与while语句类似,

32、用于构造由条件控制的循环。 do while语句的格式: do循环体语句块while (表达式);66循环嵌套 循环嵌套是指循环语句的循环体内又包含另一个循环语句,即循环套循环。程序2.15 输出九九表void main( ) int bcs,cs; for (bcs=1; bcs=9; bcs+) for (cs=1; cs=bcs ;cs+) coutbcs*cs=bcs*cs ; coutendl; 67程序输出结果:1*1=12*1=2 2*2=43*1=3 3*2=6 3*3=94*1=4 4*2=8 4*3=12 4*4=165*1=5 5*2=10 5*3=15 5*4=20 5

33、*5=256*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=367*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=498*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=649*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=8168void main( ) int m=0;for(int i=1; i=10; i+) for(int j=1; j=20; j+) for(int k=1; k=30; k+ )

34、m+; coutm=mendl;程序输出结果:m=6000程序2.16 三层循环嵌套举例169100元钱买100只鸡。公鸡5元一只,母鸡3元一只,小鸡1元3只,输出所有的购买方案。void main( ) int a,b,c ; /a是公鸡数,b是母鸡数,c是小鸡数。for(a=0;a=20;a+) for(b=0;b=33;b+) for(c=0;c=99;c+) if( a+b+c=100 & 5*a+3*b+c/3.0=100) cout公鸡数=a 母鸡数=b 小鸡数=cendl; 程序2.17 三重循环举例2结果为:公鸡数=0母鸡数=25小鸡数=75公鸡数=4母鸡数=18小鸡数=78公

35、鸡数=8母鸡数=11小鸡数=81公鸡数=12母鸡数=4小鸡数=8470 break是流程控制语句。 break语句用在循环语句和switch语句中。 break语句有两种格式: break; break 标号; break语句71程序2.18 判断输入的某数是否为素数(质数) 素数是除1和本身之外不能被其它数整除的数。 void main() int m, i=1; coutm; while(+im) if(m%i=0) coutnot primeendl; break; if (i=m) coutm is prime.endl; 程序输出结果:enter a number:77 is pri

36、me.72程序2.19 计算两个整数的最大公约数使用辗转相除法void main() int m,n,temp; coutmn; if (mn) temp=m;m=n;n=temp; do temp=m%n; m=n; n=temp; while (n!=0); cout最大公约数是mendl;程序输出结果:Enter two numbers :12 8最大公约数是473continue 语句格式:continue;continue 语句只能用于循环语句中。当在循环体内遇到continue语句时,它结束本次循环体的执行,即continue语句后面的语句不被执行,立即执行下一次循环。74程序2.

37、20 使用continue语句举例计算一个数组中正数和void main() int sum,a =3,0,-5,7,8,-2,-9,-6,6; sum=0; for(int i=0; i9;i+) if (ai0) continue; sum+=ai; coutsum=sumendl; 程序输出结果:sum=24 指针 指针是一个地址值,是一个存储单元的地址。可以通过指针访问存储单元。 指针变量 1.定义指针变量格式: 数据类型 *指针变量名; 例如:int *pointer; 2.指针运算符&和* 使用&运算符得到变量的地址。 使用*运算符得到指针所指变量存放的数据。第3章 指针、结构体和

38、联合体程序3.1 使用&和*运算符举例#includevoid main()int x=10, *pointer;pointer=&x;coutx=xendl; cout*pointer=*pointerendl;*pointer=20;coutx=xendl;cout*pointer=*pointerendl;输出结果为:x=10*pointer=10 x=20*pointer=20 数组每个元素都占用存储单元,它们都有相应的地址。可以通过数组元素地址访问数组元素。 数组名就是指针。一维数组的数组名是数组第1个元素地址,组名加1是第2个元素的地址,依次类推。例 static int a3=1

39、,2,3; for(i=0;i3;i+) cout*(a+i) aiendl; 输出结果为: 1 1 2 2 3 3数组的指针可以定义一个指针变量存放数组的地址。程序3.3void main( ) static int a3=1,2,3; int *p; p=a; for(i=0; i3; i+) coutpi *(p+i)endl;输出结果为:1 12 23 3数组的指针变量例 char str=hello; 为数组str分配6个字节,前5个字节存放字符,最后字节存放0。0是字符串的结束标志。 char str30=This is a book; 为数组str分配30个字节,前14个字节存放

40、This is a book字符串与指针字符数组的指针变量可以定义一个指针变量,存放字符数组的地址。程序3.4#includevoid main( )char str30=This is a book.; char *charpoint; /charpoint是指向字符数组的指针变量 charpoint=str; coutcharpointendl; coutcharpoint0charpoint1endl; cout*(charpoint)*(charpoint+1)endl;输出结果为:This is a book.This is a book.ThTh处理字符串的函数包含在头文件stri

41、ng.h中。字符串函数的参数常是字符串数组名、字符串指针变量等。(1)求字符串长度函数strlen(字符串) 计算一个字符串的长度,并返回这个字符串的长度。字符串的结束标志不计算在内。 函数的参数可以是字符串数组名、字符串指针变量等。处理字符串的库函数程序3.5#include#includevoid main() char str20=abcde;coutThe length is strlen(str)endl;coutinclude0,The length issizeof(str)endl;char *p=str;coutThe length is strlen(p)endl;输出结果

42、为:The length is 5include 0,The length is 20The length is 5(2)字符串的拷贝函数strcpy(字符串1,字符串2) 用于将字符串2的内容拷贝到字符串1中。函数的参数可以是字符串数组名、字符串指针变量等。(3)字符串的连接函数strcat(字符串1,字符串2) 用于将字符串2的内容连接到字符串1之后。(4)字符串的比较函数strcmp(字符串1,字符串2) 当字符串1与字符串2完全相同时,函数返回值为0。 当字符串1大于字符串2时,返回一个大于0的整数。 当字符串1小于字符串2时,返回一个小于0的整数。其他字符串函数程序3.6#inclu

43、de#includevoid main()char str120,str220,str320=abcde;strcpy(str1,Monday);strcpy(str2,str1);strcpy(str3,ABCDE);coutstr1endl;coutstr2endl;coutstr3endl;输出结果为:MondayMondayABCDE程序3.7#include#includevoid main()char str120=abcd,str240=12345;strcat(str1,efg);strcat(str2,str1);coutstr1endl;coutstr2endl;输出结果为

44、:abcdefg12345abcdefg程序3.8#include#includevoid main()char str120=bag,str240=big;coutstrcmp(str1,str2)endl;coutstrcmp(str2,str1)endl;coutstrcmp(red,red)endl;输出结果为: -1 1 0const关键字用于定义有名的常量。程序3.9 用const定义常量#includevoid main() const int size=10; char strsize; coutThesize of str is sizeof(str)endl; size+;

45、 /错误语句const关键字 为变量取的别名称为引用。 例如:数据类型 &变量2=变量1; &为引用运算符。变量2是变量1的别名。 引用是一个指针,该指针指向被引用的变量,即引用是被引用变量的地址。 例如:int x; int &y=x;引用与指针程序3.10 定义和使用引用举例void main( ) int a=9; int &b=a;coutHere a=an;coutHere &b=&bn; a+;coutHere a=an;coutHere &b=&bstudent1.age; coutstudent1.age;结构体的定义和使用struct student char number1

46、0; char name10; char sex; int age; ;void main()student student1;coutstudent1.number; /为成员输入数据;coutstudent1.sex;coutstudent1.age;cout该学生的情况如下:endl;coutstudent1.numberendl; /输出成员数据coutstudent1.sexendl;endl;coutstudent1.ageendl; 程序3.11 定义和使用结构体结构体数组数组的每个元素是结构体类型,这样的数组称

47、为结构体数组。访问结构体数组的格式:数组名下标.成员名程序3.12 结构体数组struct student char number10; char name10; char sex; int age; void main() student student13; int k; for(k=0;kstudent1k.number; ; cinstudent1k.sex; cinstudent1k.age; for(k=0;k3;k+) coutendl;coutstudent1k.numberendl; endl; couts

48、tudent1k.sexendl; coutstudent1k.age成员名struct student char number10; char name10; char sex; int age;void main() student student1,*p =&student1; coutp-number; coutp-name; coutp-sex; coutp-age; cout该学生的情况如下:endl; coutnumberendl; coutnameendl; coutsexendl; coutageendl; 在C+中,结构体可以包括数据成员和函数。程序3.14struct s

49、tudent char number10; char name10; char sex; int age; ;void output() /函数,用于输出数据cout该学生的情况如下:endl; coutnumberendl; coutnameendl; coutsexendl; coutageendl;void main()student student1; coutstudent1.number; ; coutstudent1.sex; coutstudent1.age; student1.output(); 结构体中包括函数联合体 联合体也是用户自定义类

50、型,它在定义形式上与结构体类似,但存储不同于结构体。联合体的各个成员共享一块内存空间。程序3.16 定义和使用联合体union abc char a4; int b; long c; ;void main() union abc x; x.c=0 x41424344; ; ; couthexx.bn; coutx.a0 x.a1 x.a2 x.a3成员名union abc char a4; int b; long c; ;void main()union abc x; union abc *p; p=&x; x.c=0 x41424344; ; couthexcn; couthexx.bn;

51、couthexbn;C+可以将函数加到联合体。使联合体可以包括数据成员和操作。程序3.10union abcchar a; int b; long c; void output() coutan; coutbn; coutcn;void main() union abc x; x.c=0 x41424344; x.output(); 输出结果: 41424344 41424344 D联合体中包括函数99第4章 函数函数是C+程序的主要组成部分。一个函数是一段具有独立功能的程序段。将一个程序划分为若干个函数,使程序具有模块化特性,提高程序的效率和移植性。100函数必须先定义,后使用。定义形式:

52、函数值类型 函数名(参数列表) 函数体语句块 例: 函数dec求两个数的差值。有x和y两个参数,函数值是int int dec(int x,int y) return(x-y); 例: 函数out用于输出两个数的差值以及和值。函数无返回值, void out(int x,int y) /用void表示无返回值 coutx-y; coutx+y; 函数定义101 一个文件有多个函数时,一般将主函数放在前面。放在后面的函数需要进行说明。 函数原型说明格式:函数值类型 函数名(参数列表);int dec(int x,int y); /函数说明语句 void out(int x,int y); /函数

53、说明语句 void main( ) coutdec(5,3)endl; /调用函数dec out(5,3); /函数调用out int dec(int x,int y) return(x-y); void out(int x,int y) coutx-yendl; coutx+yendl; 函数原型说明102 使用函数的过程称为函数调用 函数调用的方式:函数名(实参列表)程序3.1 调用无返回值函数举例void lprint(char a) ; /函数原型说明void main() lprint(A); /调用无返回值的函数void lprint(char a) /定义无返回值的函数 cout

54、Output= a; 函数调用103int dec (int a, int b); /函数原型说明void main() int li_num; li_num= dec(10,3); /函数调用 coutNumber=li_num; return 0;int dec(int a, int b) return a-b; /函数返回值程序3.2 调用有返回值函数举例104 dec函数的参数类型是int,调用dec函数时给了两个实型数,系统自动将实数转成int数。 int dec(int a, int b); /函数原型说明 void main() int li_num; li_num= dec(1

55、0.0,3.0); /函数调用,给了两个实型数。 coutNumber= li_num; int dec(int a, int b) return a-b; /函数返回值程序3.3 参数自动转换举例105在调用一个函数中又调用另一个函数,称为嵌套调用。 程序3.5 函数嵌套调用。 void a1(void); void a2(void); void main( ) int a; coutmain program begin n; a1(); coutmain program end n;void a1() coutfunction a1 begin n; a2(); void a2() cou

56、tfunction a2 begin n; 程序输出结果: main program begin function a1 begin main program end函数嵌套调用106函数返回有两种情况: 一是无返回值返回,二是有返回值返回。 1.无返回值返回 用void说明 void mul(int a, int b) 2.有返回值返回 mul(int a, int b) /有返回值,返回值类型默认为int类型 int mul(int a, int b) long mul(int a, int b) /有返回值,返回类型为long 函数返回与返回值107函数的返回值可以是一个指针。这种函数称

57、为指针型函数。指针型函数的定义形式:类型 *函数名(形式参数列表) 指针型函数的原型说明形式:类型 *函数名(参数列表);程序3.7 指针型函数char *match();void main( )char *p; p=match(); if (p) coutthe char of match() function is *pendl; else coutno! n;char *match() char a=B; return &a; /返回指针程序输出结果:the char of match() function is B函数返回指针108函数的参数是可选项,即函数可以有参数,也可以没有参数。

58、函数参数的类型根据实际需要决定。形参:定义函数时给出的参数称为函数的形式参数,简称形参。实参:向函数传递的参数称为实际参数,简称实参。实参可以是常量、变量、数组元素等。匹配:形参和实参要一一对应,包括数据类型、个数、次序参数传递: C+采用三种方法向函数传递参数:传值、传地址和传引用函数的参数109函数的实参是数值数据,函数的形参是变量程序3.8 向函数传值。void swap(int a, int b) int x; x=a; a=b; b=x;void main( ) int a=1,b=2; swap(a,b); coutmain program a=an; coutmain progr

59、am b=an;程序输出结果:main program a=1main program b=2传值110函数的形参是地址(指针)变量,函数的实参是地址值。程序3.9 向函数传地址举例void swap(int *a, int *b) /地址变量int x; x=*a;*a=*b;*b=x; void main( ) int a=1,b=2; swap(&a,&b); /传地址 coutmain program a=an; coutmain program b=bn;程序输出结果:main program a=2main program b=1传地址111用引用作函数形参,用与引用对应的变量作实

60、参,称为传引用调用函数。程序3.11 传引用调用函数举例void swap(int &,int&);void main( ) int a=1,b=2; swap(a,b); coutmain program a=a,b=bn; void swap(int &m, int &n)int x; coutfunction swap begin m=m,n=nn; x=a;a=b;b=x; cout“function swap end a=”m“,n=”n“n”;程序输出结果:function swap begin a=1, b=2function swap end a=2, b=1main prog

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论