达内C++培训课件_第1页
达内C++培训课件_第2页
达内C++培训课件_第3页
达内C++培训课件_第4页
达内C++培训课件_第5页
已阅读5页,还剩500页未读 继续免费阅读

下载本文档

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

文档简介

1、1Copyright Tarena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大达内科技加拿大达内科技( (中国中国) )公司公司中国北京中国北京电话:(010)62136369、62132839地址:北京市海淀区北三环西路甲18号 (大钟寺附近)中鼎大厦B座7层中国上海中国上海电话:(021)61202630、61202603地址:上海市北京东路668号 上海科技京城C区9层 加拿大多伦多加拿大多伦多电话:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, O

2、ntario, Canada邮编:M2J 3J3邮件:C 网址:www.tarena.ca中国中国广州广州电话:(020)85518868、85518898 地址:广州天河区岗顶侨鑫教育主楼三层The C+ Programming LanguageThe C+ Programming LanguageChapter 1Chapter 12C+ Programming in UNIXC+ Programming in UNIXu课程介绍lC+ 语法基础l面向对象程序设计的概念l大量的编程实践u目标l熟练掌握C+语法l具有面向对象程序设计的概念与能力l能熟练阅读复杂的C+程序源代码l能独立的设计与

3、完成面向对象的C+程序3课程内容简介课程内容简介 1 1uC+语言基础l保留字l变量,常量l表达式l语句l函数l程序的结构l数据结构与算法 数组、指针、引用、结构、链表与栈4课程内容简介课程内容简介 2 2uC+面向对象编程l类l构造函数与析构函数l静态成员与友员l函数重载l继承与多态uI/O流u模板u异常5程序设计语言介绍程序设计语言介绍1 1uWhat computer understand?lbitsuAssembler LanguagelLimited structurelGlobal scopelMachine codeuPrimitive High-Level LanguagelF

4、unction decompositionlData separationlHigh level structure6程序设计语言介绍程序设计语言介绍2 2uBlock Structured LanguagelEncapsulationlFlexible data scopinglModularizationuObject-Oriented LanguagelInheritancelPolymorphismlAbstract data types7C+C+程序设计语言程序设计语言u1972,AT&T, Bell Lab. Dennis Ritche, C languageu1980,

5、Bell Lab. Bjarne Stroustrup, C extension, u1983, C+ namedu1997, ANSI (American National Standards Institute) C+ (standard C+)8What C+ will we learn What C+ will we learn in this coursein this course ? ?uStandard C+: ANSI C+ is more scalable to different platform such as Unix, Microsoft windows, Macl

6、The standard C+ library got supported by most of industry providers.uYoud better dont know C. .?uWe will try to avoid C library and C syntax.uA C+ file can be with .cc, .cp, .cpp extensions.9为什么选标准为什么选标准 C+C+uANSI 规范了C+的标准,使之具有高度的可移植性。u程序能够运行得很快,可直接操作系统资源,保持机器层次的实现细节。u 不要求图形环境, 对系统要求相对较低。u易于解决与旧工程的接

7、口以及在数据库,存储和性能方面的技术限制。uC+ 是一种面向对象的多范型语言,可以将面向对象的模型映射成为C+ 的结构。它为开发者设计和编写一个解决方案提供了一定的选择范围。10C+C+的优点的优点uSupports data abstraction and object-oriented programminguContains all existing features of C, making the migration from C to C+ relatively easyuIs as portable and efficient as CuCan be linked to exis

8、ting C libraries and functionsuProvides strong static-type checkinguIs a general-purpose language11程序员应该具备的计算机知识程序员应该具备的计算机知识1 1u操作系统与应用程序u运行环境与运行机制l系统与命令l运行环境与命令行参数l进程l栈l堆12Binary and HexadecimalBinary and HexadecimaluBinary: 0101 1000uDecimal: 88uHexadecimal: 0 x581 byte = 8 bits.0123456789ABCDEF0

9、12345678910 1112 13 14 1513程序员应该具备的计算机知识程序员应该具备的计算机知识2 2u编辑器u编译器l编译器的功能l解释执行与编译执行的差别l熟悉自己常用的编译器,查错能力u连接器u库与库函数u系统调用14软件开发周期软件开发周期源程序编辑源程序编辑编译编译连接连接调试调试运行运行15Software LifecycleSoftware Lifecycleu开发u测试u维护u更新16熟悉你的环境熟悉你的环境u可用的UNIX服务器.21 ..26uTelnet命令介绍:telnet 192.168.

10、0.21Login: use your registered user account.Password: type in your pass word.17创建自己的学习帐号创建自己的学习帐号utelnet login: tarenaPassword: tarena欢迎使用达内科技欢迎使用达内科技( (中国中国) )公司开放实验室的服务公司开放实验室的服务! !Welcome to the OpenLab of Tarena Technologies Inc. Cananda.请按照以下提示创建您的用户帐号请按照以下提示创建您的用户帐号. .Please follow the steps t

11、o create your own account.(请输入您要注册的帐号请输入您要注册的帐号)Please enter your new account name:XXXXXXXX (请输入您的请输入您的E-Mail地址地址)Please enter your email address: XXXYYY.ZZZ18用自己的帐号登录用自己的帐号登录UNIXUNIX服务器服务器Escape character is .SunOS 5.8login: XXXXXXXChoose a new password.New password: *19程序员经常用到的程序员经常用到的UNIXUNIX命令命令

12、1 1u简单的文件维护与管理ls, cd, mkdir, rm, cp, mv, cat, moreu源程序的编写vi, edu编译与连接gcc, g+, ldu运行与调试adb,gdb20程序员经常用到的程序员经常用到的UNIXUNIX命令命令2 2u查看运行状态% 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)21第一个第一个UNIXUNIX上的上的C+C+程序程序u用vi编辑器来编写hello.

13、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;22g g的常用参数的常用参数u- c 编译成目标文件.ou- o指定输出文件名,输出文件名跟在-o后面,用空格分隔。如果不使用这个选项,缺省的输出文件名为a.out。u- g产生有调试信息的可执行文件u- w不产生警告信息u- l 连接指定的库文件u- L指定库文件的路径u- i 要包含的头

14、文件u- I 头文件的路径u- E 显示预处理后的程序文件到屏幕上,可以用-o指定输出到文件u- S 产生汇编程序如果没有c、E、S就会生成可执行文件23编译编译hello.cchello.cc% g+ -c hello.cc% ls24连接连接hello.ohello.o% g+ -o hello hello.o% ls% g+ hello.o% ls25运行运行hello hello 程序程序% hello% a.out26C+C+程序的基本结构程序的基本结构1 1/*the first C+ program*/#include using namespace std;/main func

15、tionint main()cout Hello world! endl;cout This is my first C+ program.n;27C+C+程序的基本结构程序的基本结构2 2u#include 与 #include uName space: 提供了一个全局标识符和全局变量所在的作用域。uint main() u注释u函数u函数的调用ucout语句28头文件头文件u#include语句u#include 与 #include 使用使用#include using namespace std;少用少用#include 29MainMain函数函数umain函数的作用uStandar

16、d C+ main( )格式: int main( ) return 0; /the default return value is 0; 30注释注释uC+的注释31基本的输出语句基本的输出语句ucout32练习程序练习程序hi.cchi.cc#include using namespace std;int main( )cout Hi Jian! endl;cout Have a nice day. endl;return 0;33练习程序练习程序myself.ccmyself.ccu编写一个程序,打印出自己的:l姓名l性别l年龄l家庭住址l电话号码l爱好u每一条信息输出为一行34在在hi

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

18、命令行参数int main(int argc, char *argv )37命令行参数程序命令行参数程序cmdline.cccmdline.cc#include using namespace std;int main (int argc, char* argv ) for(int i=0; iargc; i+) cout argv i = argvi endl; 38使用命令行参数的使用命令行参数的hi.cchi.ccu使用命令行参数,重新编写练习程序 cmdline.cc% hi John% hi Lisa% hi G. Bush39基本输入语句基本输入语句ucin语句u使用cin语句的h

19、i.ccl重写hi.cc程序,不带命令行参数l程序自动提示用户输入字符串来获得姓名与年龄40练习程序练习程序age.ccage.cc#include using namespace std;int main() unsigned int age; char name 50; cout please enter your name: name; cout please enter your age: age; cout your name is: name endl; cout You were age -2 ch; if (ch = y) /note the difference: if (

20、ch = y) cout good endl; else cout try again. endl; 42练习程序练习程序grade.ccgrade.ccuThis is your assignment.43Q & AQ & A44Copyright Tarena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大达内科技加拿大达内科技( (中国中国) )公司公司中国北京中国北京电话:(010)62136369、62132839地址:北京市海淀区北三环西路甲18号 (大钟寺附近)中鼎大厦B座7层中国上海中

21、国上海电话:(021)61202630、61202603地址:上海市北京东路668号 上海科技京城C区9层 加拿大多伦多加拿大多伦多电话:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, Ontario, Canada邮编:M2J 3J3邮件:C 网址:www.tarena.ca中国中国广州广州电话:(020)85518868、85518898 地址:广州天河区岗顶侨鑫教育主楼三层The C+ Programming LanguageThe C+ Programming LanguageChapter 2Chapter 245字符集字符集

22、uabcdefghijklmnopqrstuvwxyzuABCDEFGHIJKLMNOPQRSTUVWXYZu0 1 2 3 4 5 6 7 8 9u_u+ - * / % =u. , : ? | ! # &u( ) u空白46C+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, i

23、nt, long, new, operator, private, protected, public, return, short, signed, sizeof, static, struct, switch, template, this, throw, true, try, typedef, union, unsigned, virtual, void, while, 47常量与变量常量与变量u内存u程序的内存使用l常量l变量l动态内存u变量的类型48C+C+变量名变量名u变量名(identifier)l第一个字符必须是字母或下划线l只能使用字母,数字,或下划线l中间不能有空格l不能是

24、保留字,不能与全局函数和数据类型同名uC+严格区分大小写(UNIX中)u使用易懂的变量名(一般是相关的英语单词或者缩写)u长度一般不要超过32个字符不单是变量名,所有需要自己起名的地方都需要遵守这样的规则。49C+C+变量变量uC+是强类型语言l每一个变量都有确定的类型,且保持不变u基本数据类型l整型,int,l字符型, char,l实型, float, double,l逻辑型, bool ( standard c+ new feature ! )50基本数据类型基本数据类型1 1char, unsigned char, signed char, int, unsigned int, sign

25、ed int, short int, unsigned short int, signed short int, long int, signed long int, unsigned long int, float, double, long double,boolvoid:enum,struct,union,array,pointer,class51基本数据类型基本数据类型2 2u实型数据 (float, double) 无unsigned.52The standard C+ new featuresThe standard C+ new featuresubool: 其值必为关键字tru

26、e 或false u四个转型运算子:lstatic_cast: compiling time to check data type(primitive).lconst_cast: only for constant data type converting.ldynamic_cast: usually for top-down data type cast.lreinterpret_cast: more general cast for all data types.(到多态的时候再详细讲)53变量与数据类型变量与数据类型uC+是强类型语言l先声明,后使用uC+编译器对变量声明的处理54一个使

27、用变量的程序例子一个使用变量的程序例子#include using namespace std;int main( )int i;i = 5;cout i = i endl;i = 8;cout i = i endl;return 0;55另一个使用变量的例子程序另一个使用变量的例子程序#include using namespace std;int main( )i = 5; /see what happenscout i = i endl;i = 8;cout i = i endl;return 0;int i;56变量与变量的变量与变量的sizesizeu变量都有类型u变量在内存中的大小

28、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;57程序程序size.ccsize.ccu编写一个程序,打印出所有C+基本类型的大小#include using namespace std;int main() cout size of char is: sizeof(char) endl; cout siz

29、e 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)

30、, =, =, !=!, &, |& | u结合性u优先级: see table 3-1 in page 35 in the recommended book 1.62运算符的使用运算符的使用uif ( demo = 2 ) 与 if (demo = 2 )if ( 2= demo ) /左值与右值。uif ( demo != 2 ) 与 if ( demo =! 2 )63运算符的优先级运算符的优先级int a=8,b=4,c=5;cout (a%b ? b : c);cout a%b ? b:c;64变量的赋值变量的赋值u赋值表达式u变量的初始化u一次声明多个变量u声明并初

31、始化变量65无符号类型的值无符号类型的值u无符号整数类型的回绕unsigned short int snum;snum = 65535;cout snum= snum endl;snum = snum + 1;cout snum= snum endl;66有符号类型的值有符号类型的值u有符号整数类型的回绕int inum = 2147483647;cout inum = inum endl;inum = inum + 1;cout inum = inum endl;67常用类型的取值范围常用类型的取值范围u常用类型的取值范围int i = 65535;int j = 65535;cout i*

32、j/9 endl;68练习练习u为表示如下数据,应该使用什么类型的变量:l年龄l姓名l工资l电话号码l身份证号码l西三环到东三环的距离69练习程序练习程序bin.ccbin.cc#include using namespace std;int main( ) int a = 10; cout Please enter a number: a; unsigned int r; int k; unsigned int j; char str33; memset(str, 0, 33); str32=0; r = a; int i = 32;70练习程序练习程序bin.ccbin.ccdo j =

33、r; r = r/2; k = j - r * 2; if (k) str-i = 1; else str-i = 0; while(r != 0); cout str endl;71枚举类型枚举类型enum colorRED, GREEN, BLUE, WHITE, BLACK;color tvColor = GREEN;uEnum constant variable does not need memory allocation.uThe default value in Enum is: 0, 1, 2, 3, 4.uSpecify the element values: enum co

34、lor RED=100, GREEN=200, BLUE, WHITE=300, BLACK=400 72表达式表达式u左值与右值float a;a = 5/2;+a;u求值顺序u特殊表达式: l ( ? : )l逗号表达式: int a, b, c; a = 1, b=a=2, c=b+3;73表达式的求值顺序表达式的求值顺序u求值顺序u副作用74表达式的左值与右值表达式的左值与右值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

35、, b = a + 5, b+2);(a = 0, b = a + 5, b+2) = 9; /ohps , you may get problem. 75练习程序练习程序comma.cccomma.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;76程序语句程序语句u控制语句u表达式语句u空语句u语句块77控制语句控制语句u条件判

36、断语句if ( ) if ( ) else u循环控制语句while( ) do while( );for ( ) u多路选择语句switch( ) case: 78循环语句循环语句ufor语句udowhile语句uwhile语句ubreak语句ucontinue语句79分支语句分支语句uswitch语句switch(ss) case 1:break;case 2:break;default:break;80循环语句程序例子循环语句程序例子 chengFa.ccchengFa.ccu编写一个程序,打印出乘法口诀表l分别使用for语句,dowhile语句,while语句来实现The output

37、 is:1x1=11x2=2, 2x2=41x3=3, 2x3=6, 3x3=91x4=4, 2x4=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=4

38、5, 6x9=54, 7x9=63, 8x9=72, 9x9=8181练习程序练习程序year.ccyear.ccuThis is your assignment.u判断输入的年份是否是闰年。82Q & AQ & A83Copyright Tarena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大达内科技加拿大达内科技( (中国中国) )公司公司中国北京中国北京电话:(010)62136369、62132839地址:北京市海淀区北三环西路甲18号 (大钟寺附近)中鼎大厦B座7层中国上海中国上海电话:

39、(021)61202630、61202603地址:上海市北京东路668号 上海科技京城C区9层 加拿大多伦多加拿大多伦多电话:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, Ontario, Canada邮编:M2J 3J3邮件:C 网址:www.tarena.ca中国中国广州广州电话:(020)85518868、85518898 地址:广州天河区岗顶侨鑫教育主楼三层The C+ Programming LanguageThe C+ Programming LanguageChapter 3Chapter 384函数函数u什么是函数u函

40、数的基本要素l参数l返回值u函数的声明与定义u形参与值参u函数的调用85定义函数定义函数 函数名( )return u 86函数定义的例子函数定义的例子#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( course2 );87函数声明函数声明 函数名( )

41、;void disp( char* );float average(int, int);float average(int a, int b);为什么需要函数声明?88调用函数调用函数u函数的形参u函数的调用过程l填入值参l获得返回值89栈的技术简介栈的技术简介u栈的工作原理u函数的调用与栈90栈的原理栈的原理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;

42、i = f1( );j = f2( );cout i endl;cout j =0 & ch=9) ? 1 : 0);u必须先定义,不支持函数原形(声明)u不支持结构控制语句95递归函数递归函数u一个函数调用它自己u如何正确的递归l必须有结束的条件l并且该条件一定能够满足96使用与不使用递归的例子程序使用与不使用递归的例子程序u编写一个程序bigsum.cc ,使用一个函数来求n的值:#include using namespace std;int bigsum(int a) if(a = 0) return 0; return a + bigsum(a - 1);int main()

43、 int n; cout n; int m = bigsum(n); cout The sum is: m; cout endl; 97练习程序练习程序nbang.ccnbang.ccu使用递归函数,编写一个程序来求n!的值:uThis is your assignment98函数的重载函数的重载uC+中的函数可以重载u什么是函数的重载: 对于在不同类型上作不同运算而又用同样的名字的情况,称为重载。u函数重载的注意事项: 重载函数至少在参数个数,参数类型, 或参数顺序上有所不同。99函数重载的例子函数重载的例子u求两个数的平均值double Average(int, int);double A

44、verage(float, float);double Average(double, double);double Average(long, long);100思考题思考题u 在一个程序中定义了这两个函数会怎样?int Area(int width, int length=1);int Area(int size);101Area.cc source codeArea.cc source code#include using namespace std;int Area(int width, int length = 1) return width * length;int Area(in

45、t 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?102函数参数的函数参数的constconst限定限定u可以使用const限定词来修饰形参以保护实参不被修改。uconst形参的意义#include using namespace std;void disp(const int I)cout I endl;I = 100

46、; / think about main( )disp(50);103程序的结构程序的结构u多文件结构u外部变量与内部变量u变量的作用域与可见性u头文件u静态全局变量u静态函数104多文件结构多文件结构u按不同的功能模块将程序的源代码划分在多个文件中u不同源文件之间可以共享变量声明与类型定义uC+多文件的划分原则105外部变量与内部变量外部变量与内部变量u存储类:auto, extern, register, static, volatile.u什么是外部变量u什么是内部变量106变量的作用域与可见性变量的作用域与可见性u局部变量的作用域u静态局部变量的作用域u全局变量的作用

47、域u外部变量的作用域u常量的作用域107头文件头文件u头文件的作用u如何组织头文件u头文件的使用u编译选项108静态全局变量静态全局变量u何为静态全局变量l只在本源文件中可用109静态函数静态函数u定义静态函数l只在本源文件中可用110改写改写bigsum.ccbigsum.cc与与nbang.ccnbang.cc程序程序u将bigsum.cc与nbang.cc改写成多文件结构,每个文件只能有一个函数111改写改写bigsum.ccbigsum.cc与与nbang.ccnbang.cc程序程序u分别将前面的函数改为静态函数,再次编译并运行所有的程序uThis is your assignmen

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

49、amespace std;int main( ) cout Please enter a line: endl; char line120; int cnt = 0; cin.getline(line, 120); int i = 0;114练习程序练习程序mywc.ccmywc.ccwhile (istrlen(line) if (linei != ) cnt +; while (linei != ) & (i strlen(line) ) i+; i +;cout Total cnt words. endl;115Q & AQ & A116Copyright Tar

50、ena Corporation, 2005. All rights reserved.Tarena High-End IT T加拿大达内科技加拿大达内科技( (中国中国) )公司公司中国北京中国北京电话:(010)62136369、62132839地址:北京市海淀区北三环西路甲18号 (大钟寺附近)中鼎大厦B座7层中国上海中国上海电话:(021)61202630、61202603地址:上海市北京东路668号 上海科技京城C区9层 加拿大多伦多加拿大多伦多电话:(416)491-6456 地址:Suite 1208, Deerford Road, Toronto, Ontario, Canada

51、邮编:M2J 3J3邮件:C 网址:www.tarena.ca中国中国广州广州电话:(020)85518868、85518898 地址:广州天河区岗顶侨鑫教育主楼三层The C+ Programming LanguageThe C+ Programming LanguageChapter 4Chapter 4117复杂数据类型复杂数据类型u数组u结构u指针118数组数组1 1u由若干个同类型变量组成的集合u数组的声明 数组名元素个数;u下标是数组元素到开始的偏移量u数组下标从0开始uchar buf4;buf0buf1buf2buf3119数组数组2 2u数组在声明时,元素个数必须是常量或常量

52、表达式char buf10;int I;char bufI; /?int I = 10;char bufI; /?const int i = 10;char bufi;char bufi+1;120数组数组3 3u如果数组的声明带有初始化,可以直接对整个数组赋值u访问数组元素,使用下标操作符 int iA10;iA0 = 0;iA1 = 1;uint I = 0; I = iA0 + iA1; 121数组的初始化数组的初始化u在声明的时候就初始化int iA5 = 0,1,2,3,4;int iB = 1,2,3;u使用赋值语句初始化数组iA0 = 0;iA1 = 1;u数组的边界问题int

53、iC5;iC10 = 100; /run time ?Question: does C+ compiler have array bound checking?122数组程序例子数组程序例子u编写一个程序,从键盘接受一个字符串,将该字符串颠倒顺序,然后打印出来123练习程序练习程序findmax.ccfindmax.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

54、+) int temp; if(iAi iAj) /if(iAi iAj) temp = iAi; iAi = iAj; iAj = temp; cout The bigNumber is: iA9 endl;124多维数组多维数组u二维数组与多维数组: int iA510;int iB23 = 1,2,3, 4,5,6;int iC246;u 多维数组的初始化l仅有第一个维数可以省去int iB 3 = 1,2,3, 4,5,6, 7,8,9;125数组练习程序数组练习程序mdim.ccmdim.cc#include using namespace std;int maximum(int 4

55、, 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 row; i +) for (int j=0; j max) max = gradeij; return max; 126结构结构1 1u将不同类型的相关数据信息组织在一起u是用户自定义的类型l需要先

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

57、00010;p1.salary = 1.5;strcpy(p1.address, ZhongGuanChun, Beijing, China);129结构的赋值结构的赋值2 2u结构赋值的例子Person p1 = G. W Bush, 1000010, 1.5 , ZhongGuanChun, Beijing, China;Person p2 = p1;130结构的存储模式结构的存储模式1 1u每一个成员都有自己的存储空间u对每一个成员的操作都是独立的,各元素间不会相互影响nameidsalary131结构的存储模式结构的存储模式2 2#include using namespace std

58、;struct Person char name20;unsigned long id;float salary;int main( )Person p1 = Zhang Weilong, 1000101, 32;cout &p1= &p1 endl;cout &= & endl;cout &p1.id= &p1.id endl;cout &p1.salary= &p1.salary endl;132What are pointers for ?What are pointers for ?uAcces

59、sing array elements.uPassing arguments to a function when the function needs to modify the original argument.uPassing arrays and strings to functions.uObtaining memory from the system.uCreating data structures such as linked lists.133Pointer, address, variablePointer, address, variableint theVariabl

60、e =5; int * pPointer = & theVariable;5200020002001200220032004200520062007200820092010addresstheVariablepPointer134指针指针1 1u编译器为变量与常量分配存储空间u任何存储空间都用地址来表示u任何变量/常量都有地址u一个变量的地址也是一个有意义的值u变量地址也可进行运算u这个值也可以用另一个变量来存储u这另一个变量的类型就是指针类型135指针指针2 2u指针就是用来存储其他变量的地址的变量u指针变量自己也有地址u指针是有类型的u指针的类型要跟它所指向的变量的类型一致l整数指针,浮点数指针136理解指针的例子程序理解指针的例子程序addr.ccaddr.ccu定义下面的变量,分别输出它们的值与他们的存储地址int iVal1 = 1;int iVal2 = 2;double dVal1 = 1.1;do

温馨提示

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

评论

0/150

提交评论