2 Basic Facilities

上传人:sx****84 文档编号:243482419 上传时间:2024-09-24 格式:PPT 页数:29 大小:116.50KB
返回 下载 相关 举报
2 Basic Facilities_第1页
第1页 / 共29页
2 Basic Facilities_第2页
第2页 / 共29页
2 Basic Facilities_第3页
第3页 / 共29页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,2 Basic facilities,-Shifting from C to C+,1,The iostream library defines input/output for every built-in type.,The operator is used as an input operator;,Keyword,cin,is the standard input stream.,The operator m;,cout “m=“ m endl;,scanf,printf,Example 2.1:,3,2.2 Constant,The keyword,const,express notion that a value does not change directly.,A constant must be initialized,.,const int model = 90;,const int v = 1,2,3,4;,const int x; /error,void f( ), model = 200; /error,v2+; /error,Example 2.2:,4,Pointers and Constant,void f(char* p),char s = “Gorm”;,const char* pc = s;,pc3 = g; /error,pc = p; /ok,char* const cp = s;,cp3 = a; /ok,cp = p; /error,const char* const cpc = s;,cpc3 = a; /error,cpc = p; /error,pointer to constant,constant pointer,const pointer to constant,Example 2.3:,5,2.3 References,A,reference,is an alternative name for an object.,X&,void f( ),int i = 1;,int,int x = r;,r = 2;,int i = 1;,int,int/error,Example,2.4:,6,#include ,void main(),int iOne;,int,iOne=5;,coutiOne:iOneendl;,coutr:rendl;,cout,cout,int iTwo=8;,r = iTwo;,coutiOne:iOneendl;,coutiTwo:iTwoendl;,coutr:rendl;,cout,cout,cout,Example,2.5:,7,Result:,iOne:5,r:5,&iOne 0x0065FDF4,&r 0x0065FDF4,iOne:8,iTwo:8,r:8,&iOne 0x0065FDF4,&iTwo 0x0065FDEC,&r 0x0065FDF4,8,Reference variable as parameters,void increment(int aa) aa+; ,void f( ),int x = 1;,increment(x);,cout x;,Example,2.6:,&,9,void swapv(int a,int b),int temp;,temp = a;,a = b;,b = temp;,Example 2.7:,#include ,void main(),int a = 10;,int b = 20;,swapv(a,b);,coutab;,10,void swapr(int ,void swapp(int *p, int *q);,void swapr(int &a,int &b),int temp;,temp = a;,a = b;,b = temp;,void swapp(int *p,int *q),int temp;,temp = *p;,*p = *q;,*q = temp;,11,Reference as value-returning,float temp;,float fn1(float r),temp = r*r*3.14;,return temp;,float &fn2(float r),temp = r*r*3.14;,return temp;,void main(),float a = fn1(5.0);,float b = fn2(5.0);,临时变量,Example 2.8:,12,Reference as left-hand value,What is,a left-hand value,?,#include ,int a10;,int ,return ai;,void main(), Arr(4)=50; /左值,coutnumber;,for(i=2;inumber;i+),array(i)=array(i-2)+array(i-1);,coutarray(i)=array(i)endl;,int &array(int i), return ai;,Example 2.10:,Result:,10,array(2)=1,array(3)=2,array(4)=3,array(5)=5,array(6)=8,array(7)=13,array(8)=21,array(9)=34,14,2.4 Function Declarations,Elem* next_elem();,char* strcpy(char* to, const char* from);,void exit(int);,double sqrt(double); / a declaration,double sqrt(double s) / a definition, / ,double sr1=sqrt(2);,(name,value-returning type,parameter),15,2.5 Inline Function,#include ,inline,int d(int x)return x*2;,void main( ),for (int i = 1; i3; i+),coutd(i)endl;,coutd(1+2)endl;,Example 2.11:,16,void main(),int val1,val2;,coutval1val2;,cout“The max value you entered is ” y) retrun x;,else return y; ,x = val2, y = val2,if(xy) retrun x;,else return y;,Example 2.12:,17,Note:,1 内联函数可以在一开始仅声明一次,2 内联函数必须在被调用之前声明或定义,3 只适用于小函数,不能有递归,switch,while等复杂语句,18,2.6 Overloaded Function Names,Using the same name for operations on different types is called,overloading,.,void print(int);,void print(const char*);,void f( ), print(23);,print(“asdd”); ,or,void print_float(float);,void print_int(int);,void print_char(char);,19,A set of criteria are tried in order:,1 Exact match;,2 Match using promotions;,bool,to,int,char,to,int,short,to,int,float,to,double,3 Match using standard conversions;,int,to,double,double,to,int,double,to,long double,T*,to,void*,derived*,to,Base*,int,to,unsigned int,4 Match using user-defined conversions;,5 Match using the ellipsis() in a function declaration;,20,void print(int);,void print(const char*),void print(double);,void print(long);,void print(char);,void h(char c,int i,short s,float f),print(c);,print(i);,print(s);,print(f);,print(a);,print(49);,print(0);,print(“a”);,Example 2.12:,21,Ambiguity:,Declaring too few overloaded version of a function can lead to ambiguities.,void print(double);,void print(long);,void f(),print(1L);,print(1.0);,print(1);,Example 2.13:,22,2.7 Default Arguments,A default argument is type checked at the,time of the function declaration and,evaluated at the time of the call.,void print(int value, int,base=10,);,void f( ),print(31);,print(31,10);,print(31,16);,Example 2.14:,23,The declaration of default arguments,void point(int =3, int=4); /declaration,void point(int x,int y) /definition, ,Default arguments may be provided for trailing,arguments only.,int f(int,int =0,char* =0);,int g(int =0,int =0,char*);,int h(int =0,int,char* =0);,void func(int a,int b=2,int c=3,int d=4);,func(12,12),;,func(2,15, ,20);,24,Default argument and overloaded function name,void Draw(int r=0 ,int x=0 ,int y=0);,void Draw(int r);,/confused?,void main(),Draw(20);,Example 2.15:,25,2.8 Namespaces,A namespace is a mechanism for expressing logical grouping, that is, if some declarations logically belong together according to some criteria, they can be put in a common namespace to express that fact.,namespace A,wanda();,namespace B,wanda();,A:wanda();,B:wanda();,26,Avoiding Name Clashes,/my.h:,char f(char);,int f(int);,class string;,/your.h,char f(char);,int f(int);,class string;,namespace My,char f(char);,int f(int);,class string;,namespace Your,char f(char);,int f(int);,class string;,Example 2.16:,27,Exercises:,1. Read the following program and finish it:,#include ,int /put value into the array,int get(int n); /obtain a value from the array,int vals10;,int error=-1;,void main(),put(0)=10; / put value into the array,put(1)=20; put(2)=30;,coutget(0)endl;,coutget(1)endl;,coutget(2)endl;,put(12)=1; /out of range,28,2. Write a function that swaps two strings.,Using,char*&,as the parameters.,3. Write a program that have three overloading,function,display(),to produce them output.,the first function returns a,double,type.,the second function returns a,int,type.,the third function returns a,char,type.,29,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!