C语言对C语言的扩充.ppt
概述,C+是一门混合型面向对象程序设计语言,它兼容了C语言并弥补了其缺陷,增加了面身对象的能力。其中,改造后的C语言是面向对象部分的基础。,一般语法的改进指针和引用函数的改进类和对象构造函数和析构函数,C+程序的开发过程,C+语言是一种高级程序设计语言,它的开发过程与其他高级语言程序开发过程类似,一般要经过四个步骤:,执行,编辑,编译,链接,编辑是指把按照C+语法规则编写的程序代码通过编辑器(Borland C+,Visual C+,Turbo C+等)输入计算机,并存盘。在存盘时,C+源文件的扩展名为.CPP。编译将编辑好的C+源程序通过编译器转换为目标文件(OBJ文件)。即生成该源文件的目标代码。,C+程序的开发过程,链接将用户程序生成的多个目标代码文件(.obj)和系统提供的库文件(.lib)中的某些代码连接在一起,生成一个可执行文件(.exe)。执行把生成的可执行文件运行,在屏幕上显示运行结果。用户可以根据运行结果来判断程序是否出错。,C+程序的开发过程,C+程序的开发过程,书写格式C+语言程序的书写格式自由度高,灵活性强,随意性大,如一行内可写一条语句,也可写几条语句;一个语句也可分写在多行内。不过应采用适当的格式书写,便于人们阅读和理解。为了增加程序的可读性和利于理解,编写程序时按如下要点书写:(1)一般情况下每个语句占用一行。(2)不同结构层次的语句,从不同的起始位置开始,即在同一结构层次中的语句,缩进同样的字数。(3)表示结构层次的大括弧,写在该结构化语句第一个字母的下方,与结构化语句对齐,并占用一行。(4)适当加些空格和空行。,C+程序的开发过程,C+语言程序由以下基本部分组成。1.函数一个C+程序是由若干个函数构成的。函数分为库函数(标准函数)和自定义函数。库函数一般是由系统提供的。一个完整的C+语言程序只有一个主函数。2.预处理命令预处理命令以位于行首的符号“#”开始,C+提供的预处理有宏定义命令、文件包含命令和条件编译命令三种。,2.1 C+程序的开发过程,3.程序语句 一条完整的语句必须以分号“;”结束。程序语句有如下几类:(1)说明语句用来说明变量的类型和初值。如下面语句是把变量说明为浮点数。float a,b,c;又如下面语句是把变量sum说明为整型变量,并赋初值为零。int sum=0;,C+程序的开发过程,(2)表达式语句由一个表达式构成一个语句,用以描述算术运算、逻辑运算、或产生某种特定动作,在任何表达式最后加一个分号就构成了一个语句。如下例由赋值表达式加“;”就构成一个赋值表达式语句。(3)程序控制语句用来描述语句的执行条件与执行顺序的语句,C+语言的控制语句有9种,如下页所示。其语句中的括号()表示其中是条件,表示内嵌的语句。,C+程序的开发过程,if()else条件语句for()循环语句while()循环语句do while()循环语句continue结束本次循环语句break中止循环式(switch语句)switch多分支选择语句goto转移语句return从函数返回语句,C+程序的开发过程,(4)复合语句复合语句是一种十分重要的语句,由大括号和把一些说明和语句组合在一起,使它们在语法上等价于一个简单语句;可由若干简单语句或复合语句组成。例如:if(ab)c=a-b;d=c*a;else c=a+b;d=c*b;,2.1 C+程序的开发过程,2.1 C+语言的特点,C+是 Bjarne Stroustrup 在贝尔实验室开发的一种语言,是C语言的超集和扩展。C+保留了C语言的有效性、灵活性、便于移植等特点,又添加了对面向对象的支持。由C语言扩展而来,全面兼容C语言,2.2 C+语言文件扩展名,C语言源文件扩展名.cC+语言源文件扩展名.cpp相关头文件扩展名.h,2.3 注释符号,2.4名字空间,C+里引入namespace的目的就是为了避免污染全局名字空间,简单地说,就是为了避免和减少命名冲突。一旦一个程序写大了,就很难避免重名,特别是多人合作的情况下。解决相同的函数名,或变量名,或者两个不同的库里面有相同的函数名,相当于一个文件域,2.4名字空间定义,namespace ns1 float a,b,c;fun1().使用域内成员,需要使用域操作符号“:”ns1:a;ns1:fun1();,2.4名字空间分层嵌套定义,namespace ns1 namespace ns2 class matrix 访问matrix,可写成n1:n2:matrix。,全局名字空间,2.4名字空间使用,使用关键字usingusing ns1:ns2:martix 以后在再次使用martix,可以直接使用使用using namespace 名字空间名空间所有成员都可以被直接使用using namespace std c+所有组件都在std中,通过该语句则可以使用标准+库所有成员,注意此时若使用include包含头文件,要去掉头文件扩展名,2.5c+语言的输入输出,+,见:p18 例2.1,2.6 变量的定义,变量初始化,C+支持两种形式的初始化第一种形式是使用赋值操作符的显式语法形式 int ival=1024;string project=Fantasia 2000;在隐式形式中初始值被放在圆括号中 int ival(1024);string project(Fantasia 2001);在这两种情况中ival 都被初始化为1024 而project 的初始值为Fantasia 2000,2.6 变量的定义,变量定义可以位于程序中的任何地方,2.6 变量的定义,允许直接使用结构体名定义变量 struct student int no;float math;student wang;,2.7 强制类型转换,强制转换类型,C的强制类型转换法(类型名)表达式函数表示法(仅限简单类型)类型名(表达式)int i=int(1.35);,动态内存分配(Dynamic memory allocation),静态与动态内存分配的两个主要区别是 静态对象是有名字的变量,直接对其进行操作。而动态对象是没有名字的变量,通过指针间接地对它进行操作。静态对象的分配与释放由编译器自动处理,程序员需要理解这一点,但不需要做任何事情。相反,动态对象的分配与释放必须由程序员显式地管理,相对来说比较容易出错它通过new 和delete 两个运算符来完成。,.8动态内存的分配与释放,动态内存分配(Dynamic memory allocation),malloc(),calloc()和free().,C,C+,.8动态内存的分配与释放,new 运算符动态分配堆内存 使用形式:指针变量=new 类型(常量);指针变量=new 类型 表达式;作用:从堆分配一块“类型”大小的存储空间,返回首地址 其中:“常量”是初始化值,可缺省 创建数组对象时,不能为对象指定初始值 delete 运算符释放已分配的内存空间 使用形式:delete 指针变量;delete 指针变量;其中:“指针变量”必须是一个 new 返回的指针,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p1,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p2,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p3,4.5.1 new 和 delete 操作符,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p3,p4,4.5.1 new 和 delete 操作符,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p3,p4,4.5.1 new 和 delete 操作符,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p3,p4,p1,4.5.1 new 和 delete 操作符,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p3,p4,p1,4.5.1 new 和 delete 操作符,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p3,p4,p2,p1,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p3,p4,p2,p1,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p4,p2,p1,p3,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p4,p2,p1,p3,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p2,p1,p3,p4,2.8 new和delete操作符,int*p1=new int;char*p2=new char;float*p3=new float;int*p4=new int 4;delete p1;delete p2;delete p3;delete p4;,p2,p1,p3,p4,2.8 new和delete操作符,#includevoid main()int*p=NULL;p=new int(89);/初始化存储区 if(p=NULL)cout allocation faiuren;return;cout*p;delete p;,例用 new 算符建立简单对象,2.8 new和delete操作符,#includevoid main()int*p=NULL;p=new int(89);/初始化存储区 if(p=NULL)cout allocation faiuren;return;cout*p;delete p;,例用 new 算符建立简单对象,2.8 new和delete操作符,#includevoid main()int*p=NULL;p=new int(89);/初始化存储区 if(p=NULL)cout allocation faiuren;return;cout*p;delete p;,p,89,例用 new 算符建立简单对象,2.8 new和delete操作符,#includevoid main()int*p=NULL;p=new int(89);/初始化存储区 if(p=NULL)cout allocation faiuren;return;cout*p;delete p;,例用 new 算符建立简单对象,4.5.1 new 和 delete 操作符,#includevoid main()int*p=NULL;p=new int(89);/初始化存储区 if(p=NULL)cout allocation faiuren;return;cout*p;delete p;,89,对象无名只能通过指针访问,例用 new 算符建立简单对象,2.8 new和delete操作符,#includevoid main()int*p=NULL;p=new int(89);/初始化存储区 if(p=NULL)cout allocation faiuren;return;cout*p;delete p;,p,89,例用 new 算符建立简单对象,2.8 new和delete操作符,#includevoid main()int*p=NULL;p=new int(89);/初始化存储区 if(p=NULL)cout allocation faiuren;return;cout*p;delete p;,89,89,例用 new 算符建立简单对象,p,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,|,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101|,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102 103|,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102 103 104 105 106 107 108 109|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102 103 104 105 106 107 108 109|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102 103 104 105 106 107 108 109|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102 103 104 105 106 107 108 109|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,100 101 102 103 104 105 106 107 108 109|,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,以下标方式访问动态数组,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,i 是偏移量p 指针本身值不变,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,跟踪指针赋初值,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,跟踪指针移动,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,p,100101102103104105106107108109,间址访问元素,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,100101102103104105106107108109,for(i=0;i 10;i+)cout*p;p+;,注意结束循环后,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,100101102103104105106107108109,for(i=0;i 10;p+)cout*p;,注意结束循环后,for(i=0;i 10;i+)cout*p;p+;,释放什么空间?,2.8 new和delete操作符,#includevoid main()int*p=NULL,*t;int i;p=new int 10;if(p=NULL)cout allocation faiuren;return;for(i=0;i 10;i+)pi=100+i;cout endl;for(t=p;t p+10;t+)cout*t;cout endl;delete p;,例用 new 算符建立动态数组,100101102103104105106107108109,内存泄漏,4.5.1 new 和 delete 操作符,2.8 new和delete操作符,for(i=0;i 10;i+)cout*p;p+;,2.9 作用域运算符(:),作用运算符指定作用域,2.10 引用,引用描述一种特殊的数据类型,用于声明两个以上的变量名,指向同一地址.引用运算符 则a为b的一个引用,b为被引用对象,a,b指向同一地址空间,a相当于b的一个别名,绰号。对a的操作,就会b引起操作,反之亦然。,2.10 引用,例2.5:,2.10引用,说明 1)声明多个引用型变量,每个变量名前都冠以&。2)引用不是变量,本身不能被修改,对引用的存取都是对它所引用的变量存取。3)声明引用时必须要进行初始化,除非引用做参数或返回值。4)不能声明引用的引用,也不能声明数组元素类型为引用数组。,2.10 引用,说明 因为指针也是个变量,可以声明指针变量的引用。int*a;int*,2.10 引用,5)指针与引用区别 指针的值是某一变量的内存单元地址,引用则与初始化它的变量具有相同的内存单元地址.指针是个变量,可以重新赋值,引用必须要初始化并且只能跟被引用对象相同的地址,不能指向其他地址.,2.10 引用,例2.6,2.10 引用,参数传递:值传递 void Func