《2BasicFacilities1》由会员分享,可在线阅读,更多相关《2BasicFacilities1(29页珍藏版)》请在金锄头文库上搜索。
1、2 Basic facilities-Shifting from C to C+1The 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 is used as output operator.Keyword cout is the standard output stream.2.1 stream I/O2 #include vo
2、id f( ) int m; cin m; cout “m=“ m endl; scanfprintfExample 2.1:32.2 ConstantThe 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; /errorvoid f( ) model = 200; /error v2+; /errorExample 2.2:4Point
3、ers and Constantvoid 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; /errorpointer to constantconstant pointer const pointer to constantExample 2.3:52.3 ReferencesA refere
4、nce is an alternative name for an object. X& void f( ) int i = 1; int& r = i; int x = r; r = 2; int i = 1;int& r1 = i;int& r2;/errorExample 2.4:6#include void main() int iOne; int& r = iOne; iOne=5; coutiOne:iOneendl; coutr:rendl; cout&iOne&iOneendl; cout&r&rendl; int iTwo=8; r = iTwo; coutiOne:iOne
5、endl; coutiTwo:iTwoendl; coutr:rendl; cout&iOne&iOneendl; cout&iTwo&iTwoendl; cout&r&rendl;Example 2.5:7Result:iOne:5r:5&iOne 0x0065FDF4&r 0x0065FDF4iOne:8iTwo:8r:8&iOne 0x0065FDF4&iTwo 0x0065FDEC&r 0x0065FDF48Reference variable as parametersvoid increment(int aa) aa+; void f( ) int x = 1; increment
6、(x); cout x;Example 2.6:&9void 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;10void swapr(int &a, int &b);void swapp(int *p, int *q);void swapr(int &a,int &b) int temp; temp = a; a = b; b = temp;void swapp(int *p,in
7、t *q) int temp; temp = *p; *p = *q; *q = temp;11Reference as value-returningfloat 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:12Reference as left-hand valueWhat is a left-hand v
8、alue?#include int a10;int &Arr(int i);return ai;void main() Arr(4)=50; /左值 couta4;aiArr()The value is on the left-hand side of an assignmentExample 2.9:13#include int a10;int &array(int i);void main() int i,number; a0=0; a1=1; cinnumber; for(i=2;inumber;i+) array(i)=array(i-2)+array(i-1); coutarray(
9、i)=array(i)endl; int &array(int i) return ai;Example 2.10:Result:10array(2)=1array(3)=2array(4)=3array(5)=5array(6)=8array(7)=13array(8)=21array(9)=34142.4 Function DeclarationsElem* next_elem();char* strcpy(char* to, const char* from);void exit(int);double sqrt(double); / a declarationdouble sqrt(d
10、ouble s) / a definition / double sr1=sqrt(2);(name,value-returning type,parameter)152.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:16void main() int val1,val2; coutval1val2; cout“The max value you entered is ”
11、 y) retrun x; else return y; x = val2, y = val2 if(xy) retrun x;else return y;Example 2.12:17Note:1 内联函数可以在一开始仅声明一次 2 内联函数必须在被调用之前声明或定义3 只适用于小函数,不能有递归,switch,while等复杂语句182.6 Overloaded Function NamesUsing the same name for operations on different types is called overloading. void print(int); void pr
12、int(const char*);void f( ) print(23); print(“asdd”); orvoid print_float(float);void print_int(int);void print_char(char);19A 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 double3 Match using standard conversions; int to doubl
13、e double to int double to long double T* to void* derived* to Base* int to unsigned int4 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
14、,short s,float f) print(c); print(i); print(s); print(f); print(a); print(49); print(0); print(“a”); Example 2.12:21Ambiguity: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:222.7
15、 Default ArgumentsA default argument is type checked at thetime 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:23The declaration of default argumentsvoid point(int =3, int=4); /declara
16、tionvoid 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);24Default argument and overloaded function
17、 namevoid Draw(int r=0 ,int x=0 ,int y=0);void Draw(int r);/confused?void main() Draw(20);Example 2.15:252.8 NamespacesA 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
18、 to express that fact.namespace Awanda();namespace Bwanda();A:wanda();B:wanda();26Avoiding 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 str
19、ing; Example 2.16:27Exercises:1. Read the following program and finish it:#include int& put(int n); /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)end
20、l; coutget(2)endl; put(12)=1; /out of range282. 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