达内C教程MKVCoreCProgrammingnew课件.ppt

上传人:本田雅阁 文档编号:2318445 上传时间:2019-03-20 格式:PPT 页数:505 大小:2.15MB
返回 下载 相关 举报
达内C教程MKVCoreCProgrammingnew课件.ppt_第1页
第1页 / 共505页
达内C教程MKVCoreCProgrammingnew课件.ppt_第2页
第2页 / 共505页
达内C教程MKVCoreCProgrammingnew课件.ppt_第3页
第3页 / 共505页
亲,该文档总共505页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《达内C教程MKVCoreCProgrammingnew课件.ppt》由会员分享,可在线阅读,更多相关《达内C教程MKVCoreCProgrammingnew课件.ppt(505页珍藏版)》请在三一文库上搜索。

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

2、computer understand? bits Assembler Language Limited structure Global scope Machine code Primitive High-Level Language Function decomposition Data separation High level structure,程序设计语言介绍2,Block Structured Language Encapsulation Flexible data scoping Modularization Object-Oriented Language Inheritan

3、ce Polymorphism Abstract data types,C+程序设计语言,1972,AT&T, Bell Lab. Dennis Ritche, C language 1980, Bell Lab. Bjarne Stroustrup, C extension, 1983, C+ named 1997, ANSI (American National Standards Institute) C+ (standard C+),What C+ will we learn in this course ?,Standard C+: ANSI C+ is more scalable

4、to different platform such as Unix, Microsoft windows, Mac The standard 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+的标准, 使之具有高度的可移植性。 程序能够运行得很快,可直接

5、操作系统资源,保持机器层次的实现细节。 不要求图形环境, 对系统要求相对较低。 易于解决与旧工程的接口以及在数据库,存储和性能方面的技术限制。 C+ 是一种面向对象的多范型语言,可以将面向对象的模型映射成为C+ 的结构。它为开发者设计和编写一个解决方案提供了一定的选择范围。,C+的优点,Supports data abstraction and object-oriented programming Contains all existing features of C, making the migration from C to C+ relatively easy Is as porta

6、ble and efficient as C Can be linked to existing C libraries and functions Provides strong static-type checking Is a general-purpose language,程序员应该具备的计算机知识1,操作系统与应用程序 运行环境与运行机制 系统与命令 运行环境与命令行参数 进程 栈 堆,Binary and Hexadecimal,Binary: 0101 1000 Decimal: 88 Hexadecimal: 0x58 1 byte = 8 bits.,程序员应该具备的计算机

7、知识2,编辑器 编译器 编译器的功能 解释执行与编译执行的差别 熟悉自己常用的编译器,查错能力 连接器 库与库函数 系统调用,软件开发周期,Software Lifecycle,开发 测试 维护 更新,熟悉你的环境,可用的UNIX服务器 192.168.0.21 192.168.0.23 192.168.0.26 Telnet命令介绍: telnet 192.168.0.21 Login: use your registered user account. Password: type in your pass word.,创建自己的学习帐号,telnet ,login: tarena Pa

8、ssword: tarena 欢迎使用达内科技(中国)公司开放实验室的服务! Welcome to the OpenLab of Tarena Technologies Inc. Cananda. 请按照以下提示创建您的用户帐号. Please follow the steps to create your own account. (请输入您要注册的帐号)Please enter your new account name:XXXXXXXX (请输入您的E-Mail地址)Please enter your email address: XXXYYY.ZZZ,用自己的帐号登录UNIX服务器,E

9、scape character is . SunOS 5.8 login: XXXXXXX Choose a new password. New password: *,程序员经常用到的UNIX命令1,简单的文件维护与管理 ls, cd, mkdir, rm, cp, mv, cat, more 源程序的编写 vi, ed 编译与连接 gcc, g+, ld 运行与调试 adb,gdb,程序员经常用到的UNIX命令2,查看运行状态 % ps ef % grep aaa a.txt (aaa is the chars in the file name a.txt) % prstat (ctrl

10、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 function int main() cout “Hello world!“ endl; cout “This is my first C+ program.n“; ,g的常用参数,- c 编译成目标文件.o - o指定输出文件名,输出文件名跟在-o后面,用空格分隔。如果不使用这个选项,缺

11、省的输出文件名为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+程序的基本结构1,/*the first C+ progr

12、am*/ #include using namespace std; /main function int 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; 少用

13、#include ,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,编写一个程序,打印出自己的: 姓名 性别 年龄 家庭住址 电话号码 爱好 每一条信息输出

14、为一行,在hi.cc中使用字符串,#include using namespace 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 在程序中使用命

15、令行参数 int main(int argc, char *argv ),命令行参数程序cmdline.cc,#include using namespace 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程序,不带命令行参数 程序自动提

16、示用户输入字符串来获得姓名与年龄,练习程序age.cc,#include using namespace std; int main() unsigned int age; char name 50; cout name; 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 ( c

17、h = y) cout “good“ endl; else cout “try again.“ endl; ,练习程序grade.cc,This is your assignment.,Q & A,Thank You!,The C+ Programming Language Chapter 2,字符集,abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0 1 2 3 4 5 6 7 8 9 _ + - * / % = . , : ? “ | ! # & ( ) 空白,C+的保留字,auto, bool, break, case, cat

18、ch, 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, short, signed, sizeof, static, struct, switch, template, this, throw, true, try, typedef, union,

19、unsigned, virtual, void, while, ,常量与变量,内存 程序的内存使用 常量 变量 动态内存 变量的类型,C+变量名,变量名(identifier) 第一个字符必须是字母或下划线 只能使用字母,数字,或下划线 中间不能有空格 不能是保留字,不能与全局函数和数据类型同名 C+严格区分大小写(UNIX中) 使用易懂的变量名(一般是相关的英语单词或者缩写) 长度一般不要超过32个字符 不单是变量名,所有需要自己起名的地方都需要遵守这样的规则。,C+变量,C+是强类型语言 每一个变量都有确定的类型,且保持不变 基本数据类型 整型,int, 字符型, char, 实型, fl

20、oat, 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, bool void :enum,struct,union,array,pointer,class,基本

21、数据类型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: more general cast f

22、or 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 happens cout “i = “ i endl; i =

23、 8; cout “i = “ i endl; return 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 us

24、ing namespace std; int main() cout “size of char is: “ sizeof(char) endl; 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

25、 “size of signed int is: “ sizeof(signed int) endl; cout “size of short int is: “ sizeof(short int) endl; ,变量的取值范围,变量的类型与值的范围 常用类型的取值范围,常量,常量与常量的数据类型 const double pi=3.14,const限定符,限定一个常量或者函数 方便编译器来检测非法的修改操作,运算符,运算符 +,-,*,/,%, +,-, =, , =, 结合性 优先级: see table 3-1 in page 35 in the recommended book 1.,

26、运算符的使用,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:c;,变量的赋值,赋值表达式 变量的初始化 一次声明多个变量 声明并初始化变量,无符号类型的值,无符号整数类型的回绕 unsigned short int snum; snum = 65535; cout “snum=“ snum endl; snum = snum + 1; cout

27、 “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;,练习,为表示如下数据,应该使用什么类型的变量: 年龄 姓名 工资 电话号码 身份证号码 西三环到东三环的距离,练习程序bin.cc,#include using namespace std; int ma

28、in( ) 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, GREEN, BLUE, WHITE, BLACK; color tvColor = GR

29、EEN; 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;,表达式的求值顺序,求值顺序 副

30、作用,表达式的左值与右值,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,

31、 d = c + 5); (a = 1, b = a, c = a+b, d = c + 5) = 8; cout “d=“ d endl; ,程序语句,控制语句 表达式语句 空语句 语句块,控制语句,条件判断语句 if ( ) if ( ) else 循环控制语句 while( ) do while( ); for ( ) 多路选择语句 switch( ) case: ,循环语句,for语句 dowhile语句 while语句 break语句 continue语句,分支语句,switch语句 switch(ss) case 1: break; case 2: break; default:

32、break; ;,循环语句程序例子 chengFa.cc,编写一个程序,打印出乘法口诀表 分别使用for语句,dowhile语句,while语句来实现,The output is: 1x1=1 1x2=2, 2x2=4 1x3=3, 2x3=6, 3x3=9 1x4=4, 2x4=8, 3x4=12, 4x4=16 1x5=5, 2x5=10, 3x5=15, 4x5=20, 5x5=25 1x6=6, 2x6=12, 3x6=18, 4x6=24, 5x6=30, 6x6=36 1x7=7, 2x7=14, 3x7=21, 4x7=28, 5x7=35, 6x7=42, 7x7=49 1x8

33、=8, 2x8=16, 3x8=24, 4x8=32, 5x8=40, 6x8=48, 7x8=56, 8x8=64 1x9=9, 2x9=18, 3x9=27, 4x9=36, 5x9=45, 6x9=54, 7x9=63, 8x9=72, 9x9=81,练习程序year.cc,This is your assignment. 判断输入的年份是否是闰年。,Q & A,Thank You!,The C+ Programming Language Chapter 3,函数,什么是函数 函数的基本要素 参数 返回值 函数的声明与定义 形参与值参 函数的调用,定义函数, 函数名( ) return

34、,函数定义的例子,#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 ); ,函数声明, 函数名( ); void disp( char* ); float average(int, int); flo

35、at 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( ); cout i endl; cout j endl int f

36、1( ) 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 必须先定义

37、,不支持函数原形(声明) 不支持结构控制语句,递归函数,一个函数调用它自己 如何正确的递归 必须有结束的条件 并且该条件一定能够满足,使用与不使用递归的例子程序,编写一个程序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,使用

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

39、t Area(int width, int length=1); int Area(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 ins

40、tead. cout Area(10) endl; /is this ok? ,函数参数的const限定,可以使用const限定词来修饰形参以保护实参不被修改。 const形参的意义 #include using namespace std; void disp(const int I) cout I endl; I = 100; / think about this. int main( ) disp(50); ,程序的结构,多文件结构 外部变量与内部变量 变量的作用域与可见性 头文件 静态全局变量 静态函数,多文件结构,按不同的功能模块将程序的源代码划分在多个文件中 不同源文件之间可以共享

41、变量声明与类型定义 C+多文件的划分原则,外部变量与内部变量,存储类:auto, extern, register, static, volatile. 什么是外部变量 什么是内部变量,变量的作用域与可见性,局部变量的作用域 静态局部变量的作用域 全局变量的作用域 外部变量的作用域 常量的作用域,头文件,头文件的作用 如何组织头文件 头文件的使用 编译选项,静态全局变量,何为静态全局变量 只在本源文件中可用,静态函数,定义静态函数 只在本源文件中可用,改写bigsum.cc与nbang.cc程序,将bigsum.cc与nbang.cc改写成多文件结构,每个文件只能有一个函数,改写bigsum.

42、cc与nbang.cc程序,分别将前面的函数改为静态函数,再次编译并运行所有的程序 This is your assignment,练习程序hash.cc,#include using namespace 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,输入一行文字,统计单词的个数。 为了输入的时候能包含空格,程序使用了getli

43、ne函数。 #include using namespace std; int main( ) cout “Please enter a line:“ endl; char line120; int cnt = 0; cin.getline(line, 120); int i = 0;,练习程序mywc.cc,while (istrlen(line) if (linei != ) cnt +; while (linei != ) ,Q & A,Thank You!,The C+ Programming Language Chapter 4,复杂数据类型,数组 结构 指针,数组1,由若干个同类型

44、变量组成的集合 数组的声明 数组名元素个数; 下标是数组元素到开始的偏移量 数组下标从0开始 char buf4;,数组2,数组在声明时,元素个数必须是常量或常量表达式 char buf10; int I; char bufI; /? int I = 10; char bufI; /? const int i = 10; char bufi; char bufi+1;,数组3,如果数组的声明带有初始化,可以直接对整个数组赋值 访问数组元素,使用下标操作符 int iA10; iA0 = 0; iA1 = 1; int I = 0; I = iA0 + iA1;,数组的初始化,在声明的时候就初始

45、化 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?,数组程序例子,编写一个程序,从键盘接受一个字符串,将该字符串颠倒顺序,然后打印出来,练习程序findmax.cc,#include using namespace std; int main( ) int iA = 103, 5, 68, 115, 32, 23, 66,

46、 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 = 1,2,3, 4,5,6, 7,8,9;,数组练习程序mdim.cc

47、,#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,将不同类型的相关数据信息组织在一起 是用户自定义的类型 需要先声明类型的定义才能使用 结构与数组的区别 数组只是同一个数据类型的聚集 数组本身不是一个新的数据类型,结构2,struct ; ; ; (结构变量名); struct Person char name20; unsigned long id; float salary; char address200; p1, p2;,结构的赋值1,通过取成员操作(.)来引用结构变量的元素 Person p1 = “G.W Bush“, 1000010, 1.5 , “ZhongGuan

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

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1