版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Why——WhatmostWhy——Whatmostschoolsdon'tEMIL,CollegeofMathematicsandcomputerScience,EMIEMICollegeofMathematicsandComputerScience,EMIL,EMIL,CollegeofMathematicsandcomputerScience,1WhyC&1WhyC&TIOBEIndexforApril4EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,5EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,编程语言开发者收入范围分布(2014年4月中国编程语言开发者收入范围分布(2014年4月中国6EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,编程语言开发者收入范围分布(2015年4月美国Startup编程语言开发者收入范围分布(2015年4月美国StartupCompass美国软件开发者薪资调查报7EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,CVS.※与C语言CVS.※与C语言兼※是C语言的一个超程序中※支持面向对象程8EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,HowaboutarrangementA1课程内WhyWhyWhy9EMIL,CollegeHowaboutarrangementA1课程内WhyWhyWhy9EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience, Why A2学习资C++程序设计(第2版)谭浩强A2学习资C++程序设计(第2版)谭浩强编(侧重于C++语法、面向对象的概念教C++Primer4(一本久负盛名的C++经典C语言中文网网易云课堂——面向对象程序设计——网EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,A3学习方EMIL,A3学习方EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,A4教学方初始密码A4教学方初始密码设第一次练习123456周至第十每周四学周期:每周期:不e,faahsA5学习实践辅助资A5学习实践辅助资C语言项目进阶:https://www.shiya/courses/C++入门实验:EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,PARTC++基本知识(ch1-基本语PARTC++基本知识(ch1-基本语法、基本程序设计、C++程序结函数:基于过程程序设计的基本单函数的调用,变量的作用域与存数组数组的使用与指针指针的使用,指针的作用,指针结构体与自定义类型结构体与数组、结构体与指EMIL,CollegeofMathematicsandcomputerScience,––»»––2003»–2014年8EMIL,CollegeofMathematicsandcomputerScience,C++程序初C++程序初探我们可以知道一个C++程序单位EMIL,CollegeofMathematicsandcomputerScience,C++程序初C++程序初探我们可以知道一个C++程序单位EMIL,CollegeofMathematicsandcomputerScience,C++程序初探C++程序初探我们可以知道一个C++程序单位EMIL,CollegeofMathematicsandcomputerScience,C++程序初C++程序初探我们还可以知道EMIL,CollegeofMathematicsandcomputerScience,C++程序初探我们还可以知道C++程序初探我们还可以知道EMIL,CollegeofMathematicsandcomputerScience,C++程序初探C++程序初探使用命名空间cout作为一个输出流我们还可以知道EMIL,CollegeofMathematicsandcomputerScience,2.C++程序之注释2.C++程序之注释简单注释用//,内容较长的用“//”//ThisisourfirstC++/*ThisisourfirstC++CEMIL,CollegeofMathematicsandcomputerScience,2.2C程序之数据类型(.18)布尔型2.2C程序之数据类型(.18)布尔型»»»字符型整型(int,long浮点型(float基本数»double,longdouble)»signed(表示有符号unsigned(表示无符号void4个关键short(表示短型C++并没有统一规定各类数据的数值范围和内存中所占字节long(表示长型((.20)常量分#defineper4CPPcout<<"What'surnameandcout<<"Thereare"<<100*per4CPP<<"%programmersusingC++!"<<endl<<endl;"Thereare:字符串常量&&EMIL,CollegeofMathematicsandcomputerScience,((.26)»先定义(先声明),后使longID;//stringname;//字符串类对象,变量声明时,用关键字const加以限常变量申明时必须初始EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,用#define声解EMIL,CollegeofMathematicsandcomputerScience,用#define声解C++语言声明常变量的C语言中声明符号常量的方:#define预处理指缺点是不能显式声明常量形const数据类型常变量名=初值constdoublepi=3.1415926;constdoublexsin(a);//a为变量#define constdoublepi; //错#definexsin(a错用const声逻辑类型、逻辑常量和逻辑变逻辑类型、逻辑常量和逻辑变量»–––取值范围:true和编译系统处理逻辑型数据时––boollearned_flag;//cout<<"HaveulearnedCbefore?(1orif(learned_flag)//选择 EMIL,CollegeofMathematicsandcomputerScience,2.4C++程序之运算符与表达式一元运二元2.4C++程序之运算符与表达式一元运二元运三元运需要1 需要2个操作 需要3按所要求的操作数算术运算符逻辑运算符…(共15按运算EMIL,CollegeofMathematicsandcomputerScience,(.31(.31&&附录EMIL,CollegeofMathematicsandcomputerScience, (编译系 (编译系统自动进行——隐形转换P.31图 强制类型转换(类型名)(表达式)(C版本类型名(表达式)(C++版本例如inti=floatx=4.5,yxii转换成为float,得y值为y=x-int(x);//将x的类型float类型转换成int类型,int(x)值为EMIL,CollegeofMathematicsandcomputerScience,位运算符(补充»《C程序设计》位运算符(补充»《C程序设计》(第三版)(谭浩强)第十二–参与种~(按位求反&(按位与|(按位或^(按位异或<<(左移位>>(右移位二元运EMIL,CollegeofMathematicsandcomputerScience,作用:作用:按位求反的作用是对一个二进制数的例如计算000000000001010111111111110101EMIL,CollegeofMathematicsandcomputerScience,作用:将两个操作数对应的作用:将两个操作数对应的每一位分别进行例如:计算000000100000100000000EMIL,CollegeofMathematicsandcomputerScience,作用:将两个操作数对作用:将两个操作数对应的每一位分别进行例如:计算000000100000100000011EMIL,CollegeofMathematicsandcomputerScience,或(作用:两个操或(作用:两个操作数对应的每一位进行异或,例如计算001110000101010001001EMIL,CollegeofMathematicsandcomputerScience,»–––»»–––»是将左操作数的二进制值向左移动指定的位数。左左移位后,低位补0,移出的高位右移位是将左操作数的二进制值向右移动指定的位数右移位右移后,移出的低位舍弃。如果是无符号数则0;如果是有符号数,则高位补符号位或补EMIL,CollegeofMathematicsandcomputerScience,左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,1111100000000100左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,111110000000100左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,111110000000100左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,1111100000001000左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,1111100000001000左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,11111000001000左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,11111000001000左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是1注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,11111000001000左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8左移位举例:表达式4<<1,结果为举2或2的n前提1右移位举例:表达式A>>2,值为-8,结果为-A的二进制补码是注移位运算的结果就是位运算表达式的值,参与运算的两个操值并没有变化EMIL,CollegeofMathematicsandcomputerScience,1111111000001000»» »»» »EMIL,CollegeofMathematicsandcomputerScience,3.C++程序之语//将i声明为整型的 例如:int●3.C++程序之语//将i声明为整型的 例如:int● 仅由分号组成,在程序中不作任何操作空语●例如空语;分把多条语句用“{}”括起块语句的右括号后不再需要分号● 例如:i=i+1;//i的赋值语句表达式●●●●●函数和流对象的调用EMIL,CollegeofMathematicsandcomputerScience,3.3.C++程序之输入输出语句EMIL,CollegeofMathematicsandcomputerScience,C兼容了C兼容了C语言的输入输C的输入输出分别通过函数scanf()和printf()实现这两个函数定义在头文件stdio.h通过函数getchar()和putchar()实EMIL,CollegeofMathematicsandcomputerScience,usingnamespacestd;intmain(){inta;usingnamespacestd;intmain(){inta;charprintf("a=%d,b=%f,c=%c\n",a,b,c);return0;}EMIL,CollegeofMathematicsandcomputerScience,输入:12A#include#includeEMIL,CollegeofMathematicsandcomputerScience,cout<<"Hello,"<<name<<"withID//双引号中的字符串常cout<<"Hello,"<<name<<"withID//双引号中的字符串常量照原样输注不能用一个插入运算符“<<”插入多个输出项//错cout<<a<<b<<c;//正确,a,b,c可为不同类型的数usingnamespacestd;intmain(){intcharc='a';cout<<a<<''<<b<<''<<c<<endl;return0;}4aEMIL,CollegeofMathematicsandcomputerScience,用cin输入时,系统也会根据变量的类型,从输入流中提取用cin输入时,系统也会根据变量的类型,从输入流中提取相应长度的usingnamespaceintmain(){若输c2='2'a=34charinta;return0;2若输入c2='2'a=34}注系统把空格作为数据间的分隔符(包含字符变量,与scanf不同cin自动跳过输入的空白字符(包括空格,制表,backspace和回车等符变量,可用getchar()(P.57)EMIL,CollegeofMathematicsandcomputerScience,#include#include#include»»EMIL,CollegeofMathematicsandcomputerScience,表标准输入输出流的控制符EMIL,CollegeofMathematicsandcomputerScience,控制作表标准输入输出流的控制符EMIL,CollegeofMathematicsandcomputerScience,控制作以十进以十六进制格以八进制为格setfill(设置填充字符c,c为字符常量或字符变setw(w设置输出宽度为n设置浮点数以固定的小数位数输设置浮点数以指数形式输输出数据左a对输出数据右对忽略前数据以十六进制形式输出时字母以大写表数据以十六进制形式输出时字母以小写表输出正数时给出“+”使用控制符输#include<iostream>#include<iomanip>usingnamespacestd;{doubleint使用控制符输#include<iostream>#include<iomanip>usingnamespacestd;{doubleintcout<<setiprecision(6);//恢复默认格式(精度为6)cout<<setiosflags(ios::fixed);//输出123.456789cout<<b;//输出123456cout<<hex<<b;//输出1e240return0;}注EMIL,CollegeofMathematicsandcomputerScience,setw只对其后的第一个数据起33顺序结从上到下顺序执行各语»选择结构和多分支选择结循环结反复执行某一部分的操作,周而复EMIL,CollegeofMathematicsandcomputerScience,程序EMIL,CollegeofMathematicsandcomputerScience,程序EMIL,CollegeofMathematicsandcomputerScience,问问题描述程序缺陷EMIL,CollegeofMathematicsandcomputerScience,问问题描述程序缺陷EMIL,CollegeofMathematicsandcomputerScience,问题描述问题描述程序需要将五分制成绩A、分,然后计算使用多分支选择EMIL,CollegeofMathematicsandcomputerScience,缺陷:只能计算一门问题描问题描述程序需要将五分制成绩A、分,然后计算使用多分支选择EMIL,CollegeofMathematicsandcomputerScience,缺陷:只能计算一门问问题描问题分记录n门课程绩点之方法:循环结EMIL,CollegeofMathematicsandcomputerScience,问题问题描问题分记录n门课程绩点之方法:循环结EMIL,CollegeofMathematicsandcomputerScience,问题问题描问题分记录n门课程绩点之方法:循环结EMIL,CollegeofMathematicsandcomputerScience,问题问题描问题分记录n门课程绩点之方法:循环结EMIL,CollegeofMathematicsandcomputerScience,问题描问题描//程序3.4版中n门课程绩点计算及问题分m个同学就要计算m方法:循环嵌EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeEMIL,CollegeofMathematicsandcomputerScience,EMIL,EMIL,CollegeofMathematicsandcomputerScience,»»»»当输入姓名为0,学号上交时间:5月14–––––––––EMIL,CollegeofMathematicsandcomputerScience,?3?3zhao03001016576qian03001027665sun03001039078li03001048978zhou03001059889wu030010676870EMIL,CollegeofMathematicsandcomputerScience,»»»»»»»»–––5月15日PM–EMIL,CollegeofMathematicsandcomputerScience,»»基于过程的程序设计方法的主体——函面向函数的程序设每个函数实现某一个功主函数的作用就是调用各个函主函数可以调用子函数,函数之间也可互相调EMIL,CollegeofMathematicsandcomputerScience,EMIL,EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,Thesameas或先申明函数原型,再定一般通过函数名传解析函数名形式参实际EMIL,CollegeofMathematicsandcomputerScience,»»–属于被调用函»»–属于被调用函来自于函数调用语句的实–»»–»»–»有默认形参值–带默认形参值#include<iostream>usingnamespacestd;{intmax(inta,intint//带默认形参值#include<iostream>usingnamespacestd;{intmax(inta,intint//函数声明,形参ca,b,cout<<"max(a,b,c)="<<return0;}{max(intintreturn6}max(a,b)=EMIL,CollegeofMathematicsandcomputerScience,【有默认参数的函数】【有默认参数的函数】例如area(doublea=2.6,doublearea(doublea,doublearea(double=2.6,double说明:函数省略了area(doublea=2.6,doublearea(double=2.6,double在进行函数调用时,主调函数的实参与被调函数的形参从左到右按对应默认值EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeEMIL,CollegeofMathematicsandcomputerScience,结论函数调用时现场保护和调用返回时的现结论函数调用时现场保护和调用返回时的现场恢»C++了内置函数»inline数{......//函数}–––说明:可以在声––••比如print_resultEMIL,CollegeofMathematicsandcomputerScience,例usingnamespaceinlineintmax(int,int,{inti=10,j=20,例usingnamespaceinlineintmax(int,int,{inti=10,j=20,k=30,m;return0;}{int}EMIL,CollegeofMathematicsandcomputerScience,int{cout<<name;return}EMIL,CollegeofMathematicsandcomputerScience,int{cout<<name;return}EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeEMIL,CollegeofMathematicsandcomputerScience,»在一个函数内部或者复合 »»»在一个函数内部或者复合 »»»»如:main函数中定–那个层次的–– EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeEMIL,CollegeofMathematicsandcomputerScience,存储空程存储空程序静态存储中始终自然始动态存储本函数EMIL,CollegeofMathematicsandcomputerScience,慎用全局变EMIL,EMIL,CollegeofMathematicsandcomputerScience,课程绩点计算问(人数统计版课程绩点计算问(人数统计版一次grade_computer时,count都被初 ?EMIL,CollegeofMathematicsandcomputerScience,课程绩点计算问(人数统计版课程绩点计算问(人数统计版一次grade_computer时,count都被初要在m次grade_computer——静态局部变量,静态 ?EMIL,CollegeofMathematicsandcomputerScience,课程绩点计算问(人数统计版课程绩点计算问(人数统计版一次grade_computer时,count都被初要在m次grade_computer——静态局部变量,静态 ?EMIL,CollegeofMathematicsandcomputerScience,V3.8变量的存储示意EMIL,CollegeofMathematicsandcomputerScience,(n31A( 3. )V3.8变量的存储示意EMIL,CollegeofMathematicsandcomputerScience,(n31A( 3. ) 2V3.8变量的存储示意V3.8变量的存储示意EMIL,CollegeofMathematicsandcomputerScience,. 3. ( 3静态存储静态存储»全局变量、static变存放在静态存储区,程序执行期间始终存自动存储»没有static限定的局部变自动变块执行结束后释EMIL,CollegeofMathematicsandcomputerScience,»多文件的C++»多文件的C++程序结思考几个问的全局变量inta,如何解决?不能直源文件1中定义外部变externint同时#include源文件源文件1中的全局变量intb,不源文件定义全局变量b时static限staticint#include指令是把不同文件组合在一起1、C++标准库中的头文件不在包括后缀2、用户自己编写的头文件,可以用.h以便区EMIL,CollegeofMathematicsandcomputerScience,»»上页中,源文件1中全局变量a被限定staticint,而源文件2中声明externint#include"iostream"和#include的区别为什么do...while循环结构时循环体至少要»静态变量为什么需要在编译的时候初始化»静态变量声明时是否一定需要初始化EMIL,CollegeofMathematicsandcomputerScience,»»why–»»why–•–••对于课程1来说,m–EMIL,CollegeofMathematicsandcomputerScience,数组下标从0开EMIL,数组下标从0开EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,能否合并 能float含义:三门课程100个学生course[0][87]存储的就是第一门课程第88位同学的成EMIL,CollegeofMathematicsandcomputerScience,数组的数组的数»floatcourses是一个长度为3的数组:»其中的每个元素是一个长度为100的数»每个长度为100的数组中元素为一个的数字EMIL,CollegeofMathematicsandcomputerScience,EMIL,EMIL,CollegeofMathematicsandcomputerScience,…EMIL,College…EMIL,CollegeofMathematicsandcomputerScience, …EMIL,College…EMIL,CollegeofMathematicsandcomputerScience, …[EMIL,CollegeofMathematicsandcomputerScience,数组元素按行连续存放…[EMIL,CollegeofMathematicsandcomputerScience,数组元素按行连续存放 EMIL,CollegeEMIL,CollegeofMathematicsandcomputerScience,EMIL,EMIL,CollegeofMathematicsandcomputerScience,——C下的字符数组与C++下的字符串——C下的字符数组与C++下的字符串传统方式(C兼容C++»»––string––C++语言中,一个字符的一维字符数组来表示存放时注意数••str[5]={′c′,′d′,等说明:因为声明的数组只有4个元素,而初始化时有5为数据项:′a′、′b′、′c′、′d′、′\0′。其EMIL,CollegeofMathematicsandcomputerScience,例如:对于字符串″abcd″、″hijk″、″opqr″可以声明和初始化为例如:对于字符串″abcd″、″hijk″、″opqr″可以声明和初始化为str[3][5]={{′a′,′b′,′c′,′d′,′i′,′j′,′k′,′\0′},{′o′,′p′,等等str[][5]={″abcd″,″hijk″,″opqr″注EMIL,CollegeofMathematicsandcomputerScience,将整个字符串(字符数组)字符数组大于字符串实际长度,输出遇0′EMIL,CollegeofMathematicsandcomputerScience,>//»»>//»»–strcat(char[constchar[strcpy(char[constchar[字符串比较函数strcmp(constchar[],constchar[]strlen(constchar[–––EMIL,CollegeofMathematicsandcomputerScience,注如:char注如:if(str1注如:char注如:if(str1EMIL,CollegeofMathematicsandcomputerScience,C++提供了一种C++提供了一种新的数据类型——字符串类型string:stringname;//学生姓名–string是C++标准库中声明的一个字符串//不能是–»可以用来定义变量——字符串变–每一个字符串变量都是string类的一个对字符串类和字符串变量之间的关系可暂时简单类比为整型数据类型和整型变量之间的系EMIL,CollegeofMathematicsandcomputerScience,定义:string串变//定义定义:string串变//定义string1赋值string输入输注在输入输出语句中用字符串变量名,输入输出字符串,string //定义string1为字符//从键盘输入一个字符串给字符串变量EMIL,CollegeofMathematicsandcomputerScience,stringstring赋值运算串接:string比较:==、、stringstringstring2="Language";»原因:string类内完成了上述运算的运算重载(ChapterEMIL,CollegeofMathematicsandcomputerScience,»数组元素为string类string//定义一个字符串数组,有5个»数组元素为string类string//定义一个字符串数组,有5个字符串元素并初始EMIL,CollegeofMathematicsandcomputerScience, ngLiS Wang 例:输入3个字符串,要求按字典序从小到大顺例:输入3个字符串,要求按字典序从小到大顺usingnamespacestd;Pleaseinputthreestrings:ChinaU.S.AGermany{stringstring1,string2,string3,temp;cout<<"pleaseinputthreestrings:";if{}if(string1<=string2)"<<string2<<"elseif(string1<=string3)elsecout<<string2<<"return0;}EMIL,CollegeofMathematicsandcomputerScience,Whatisfirsttestfor»»––一定由Whatisfirsttestfor»»––一定由其它类型派指向int的指针•int*指向double的指针–•double*指向整型指针的指–•intWhyhterScience,.ematicsand 53.WhyhterScience,.ematicsand 53. 53)swapb353. 53)swapb53.( 53SolutionbyEMIL,SolutionbyEMIL,CollegeofMathematicsandcomputerScience,SolutionbyijEMIL,CollegeofMathematicsandcomputerScience,3535swapb355swapb353swapbSolutionbyijEMIL,CollegeofMathematicsandcomputerScience,3535swapb355swapb353swapb353swapb53Solutionbyij数mai5j3EMIL,CollegeofMathematicsandcomputerScience, 传350x7fff5750xSolutionbyij数mai5j3EMIL,CollegeofMathematicsandcomputerScience, 传350x7fff5750x7f5759b355swapb353swapb353swapb53指针作为函指针作为函数参数时,形参和实参都为指针––––EMIL,CollegeofMathematicsandcomputerScience,int*p2int;//整型指intint*p2int;//整型指int**p2int;//整型指针的int(*p)[5];//指向一维整数int*p[4];//指向整数的一维const1、p是一个变量2、p是一个指针变的指针变量:int*p;constint*p;int*p[4],优先级高于*p;//指向一个»const数常量的指针变constint*constp指向EMIL,CollegeofMathematicsandcomputerScience,vector[i]事实上就是从位EMIL,CollegeofMathematicsandcomputerScience,»»––int»»––intint»––––––ptr++,ptr--,….//指针法EMIL,CollegeofMathematicsandcomputerScience,EMIL,EMIL,CollegeofMathematicsandcomputerScience,——用数组名作为实参和形参——用数组名(地址)作为实参,用指针变量作为形参EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofEMIL,CollegeofMathematicsandcomputerScience,C++语言中不能将数C++语言中不能将数组作为函数参数进行传可以通过传递指向初始元素的指针来达到数组作为参数进行传递的目EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,EMIL,CollegeofMathematicsandcomputerScience,两步走两步走数组申请时必须指定其长float目前解决方法定义了全局变量max_student浪费空间或者空间受理想情况:动态malloc函数(C语言malloc函数申返回指向该空间的指总共有多少门课程EMIL,CollegeofMathematicsandcomputerScience,constconstfloat(*course)[100]传(写操作(只读操作EMIL,CollegeofMathematicsandcomputerScience,constconstfloat(*course)[100]传(写操作(只读操作constfloatEMIL,CollegeofMathematicsandcomputerScience,const*––•*const–•*constconst*––•*const–•*const–•const*const–EMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*p=&bpEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*intconstpEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*intconstp不可变,因此p=&bpintconst*pp=&apEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*intconstp不可变,因此p=&bpintconst*pp=&apEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*constintconst*pEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const实例a变量abintb指针p是指向整型的const*constintconst*p不可变,因此p=&bpconstintconst*pp=&apEMIL,CollegeofMathematicsandcomputerScience,实例a变量abintb指针p是指向整型的const*实例a变量abintb指针p是指向整型的const*constintconst*p不可变,因此p=&bpconstintconst*pp=&ap EMIL,CollegeofMathematicsandcomputerScience,指向常量的指int⌘常指int指向常量的指int⌘常指intconstint*const*const必须初始化以改变可改变EMIL,CollegeofMathematicsandcomputerScience,⌘引»»»&引用名=变在声明引用时需要同⌘引»»»&引用名=变在声明引用时需要同时初始或&引用名(变量名变量名是被引用变量的名指被引用变量“&”是引用被引用的变量可以是任一类型的变比如intintra是为整数a的引EMIL,CollegeofMathematicsandcomputerScience,为一个变量声明了引用之后,引用就成了这为一个变量声明了引用之后,引用就成了这个变量的引6-{inta=0;int&ra=a;return0;}EMIL,CollegeofMathematicsandcomputerScience,»»––引用的出现,可以将引用作为形参,参数传递时»»––引用的出现,可以将引用作为形参,参数传递时•EMIL,CollegeofMathematicsandcomputerScience,例要求将变量i和j的值互实参、形参为usingnamespacestd;例要求将变量i和j的值互实参、形参为usingnamespacestd;{voidswap(int,intinti=3,j=5;swap(i,j);//实参为变量′\t′<<cout<<″i=return0;}//形参为变量{swap(inta,intinttemp;}EMIL,CollegeofMathematicsandcomputerScience,6-形参为指针,实参usingnamespacestd;{void6-形参为指针,实参usingnamespacestd;{voidswap(int*,int*);inti=3,j=5;swap(&i,cout<<″i=return0;//实参为变量名地′\t′<<″j=}{swap(int*p1,intinttemp;}EMIL,CollegeofMathematicsandcomputerScience,6-形参为引用,实参为变量名(引用调用usingnamespace6-形参为引用,实参为变量名(引用调用usingnamespacestd;{voidswap(int&,int&);inti=3,j=5;swap(i,//实参为变量cout<<″i=return0;′\t′<<″j=}{swap(int&a,intinttemp;}EMIL,CollegeofMathematicsandcomputerScience,例usingnamespacestd;{&fun(int&i);inta;}{}EMIL,CollegeofMathematicsandcomputerScience,2求引用的求引用的地址,实际上返回的是被引用变量引用初始化后其值不能改变,即不允许把为程序中,对引用重新赋值的意义是给被引»»»要注意区别引用声明符&和地址运算符EMIL,CollegeofMathematicsandcomputerScience,»如果程序需要先指向一个对象,后又指向另一»»usingnamespacestd;»如果程序需要先指向一个对象,后又指向另一»»usingnamespacestd;空引{int&rp=fun();int*p=&rp;deletep;//释放内存后,引用rp是一个空引}EMIL,CollegeofMathematicsandcomputerScience,{int*p=newint;{cout<<"{int*p=newint;{cout<<"内存分配错误//系统函数,用于终止程序的运}int&rp=*p;returnrp;}上例能编译运行。但它确实出现了空引用,这是不对的EMIL,CollegeofMathematicsandcomputerScience,Why»Why»难以反映他们之间的内在–––系»EMIL,CollegeEMIL,CollegeofMat
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年跨境电商供应链采购合同范本
- 2025年度地铁广告牌拆除与智能化更新合同
- 2025年度人力资源合同管理及员工关系维护协议
- 2025年度股份抵押贷款合同范本修订升级版
- 2025年度电饭煲定制生产合同(个性化定制版)
- 2025年度国际货物采购合同中的产品溯源与质量保证
- 2025年度广告物料设计与印刷合同
- 2025年度国际物流运输合同模板汇编
- 2025年荒山造林项目承包与生态补偿金结算合同
- 2025年度医院护理部护士人员招聘服务合同
- 成品移动公厕施工方案
- 2025年度部队食堂食材采购与质量追溯服务合同3篇
- 新人教版一年级下册数学教案集体备课
- 绘本 课件教学课件
- 大型央国企信创化与数字化转型规划实施方案
- pcn培训培训课件
- 过错方财产自愿转让协议书(2篇)
- 牧场物语-矿石镇的伙伴们-完全攻略
- ISO 22003-1:2022《食品安全-第 1 部分:食品安全管理体系 审核与认证机构要求》中文版(机翻)
- 农业生产质量安全风险评估与监控方案
- 人教版六年级上册解方程练习300道及答案
评论
0/150
提交评论