#include<iostream>// an inline function definitioninlinedoublesquare(double x){return x * x;}intmain(){usingnamespace std;double a, b;double c =13.0;a =square(5.0);b =square(4.5+7.5);// can pass expressionscout <<"a = "<< a <<", b = "<< b <<"\n";cout <<"c = "<< c;cout <<", c squared = "<<square(c++)<<"\n";cout <<"Now c = "<< c <<"\n";// cin.get();return0;}
#include<iostream>voidswapr(int& a,int& b);// a, b are aliases for intsvoidswapp(int* p,int* q);// p, q are addresses of intsvoidswapv(int a,int b);// a, b are new variablesintmain(){usingnamespace std;int wallet1 =300;int wallet2 =350;cout <<"wallet1 = $"<< wallet1;cout <<" wallet2 = $"<< wallet2 << endl;cout <<"Using references to swap contents:\n";swapr(wallet1, wallet2);// pass variablescout <<"wallet1 = $"<< wallet1;cout <<" wallet2 = $"<< wallet2 << endl;cout <<"Using pointers to swap contents again:\n";swapp(&wallet1,&wallet2);// pass addresses of variablescout <<"wallet1 = $"<< wallet1;cout <<" wallet2 = $"<< wallet2 << endl;cout <<"Trying to use passing by value:\n";swapv(wallet1, wallet2);// pass values of variablescout <<"wallet1 = $"<< wallet1;cout <<" wallet2 = $"<< wallet2 << endl;// cin.get();return0;}voidswapr(int& a,int& b)// use references{int temp;temp = a;// use a, b for values of variablesa = b;b = temp;}voidswapp(int* p,int* q)// use pointers{int temp;temp =*p;// use *p, *q for values of variables*p =*q;*q = temp;}voidswapv(int a,int b)// try using values{int temp;temp = a;// use a, b for values of variablesa = b;b = temp;}
void swapr(int & a, int & b); //pass variables void swapp(int * p, int * q); //pass addresses of variables void swapv(int a, int b); //pass values of variables 按引用傳遞swapr和按值傳遞swapv看起來相同。只能通過原型或函數定義才能知道swapr是按引用傳遞。然而地址運算符&使得按地址傳遞swapp。 3.引用的屬性個特別之處
#include<iostream>doublecube(double a);doublerefcube(double&ra);int main (){usingnamespace std;double x =3.0;cout <<cube(x);cout <<" = cube of "<< x << endl;cout <<refcube(x);cout <<" = cube of "<< x << endl;// cin.get();return0;}doublecube(double a){a *= a * a;return a;}doublerefcube(double&ra){ra *= ra * ra;return ra;}
#include<iostream>constint ArSize =80;char*left(constchar* str,int n =1);intmain(){usingnamespace std;char sample[ArSize];cout <<"Enter a string:\n";cin.get(sample,ArSize);char*ps =left(sample,4);cout << ps << endl;delete[] ps;// free old stringps =left(sample);cout << ps << endl;delete[] ps;// free new string// cin.get();// cin.get();return0;}// This function returns a pointer to a new string// consisting of the first n characters in the str string.char*left(constchar* str,int n){if(n <0)n =0;char* p =newchar[n+1];int i;for(i =0; i < n && str[i]; i++)p[i]= str[i];// copy characterswhile(i <= n)p[i++]='\0';// set rest of string to '\0'return p;}
#include<iostream>unsignedlongleft(unsignedlong num,unsigned ct);char*left(constchar* str,int n =1);intmain(){usingnamespace std;char* trip =(char*)"Hawaii!!";// test valueunsignedlong n =12345678;// test valueint i;char* temp;for(i =1; i <10; i++){cout <<left(n, i)<< endl;temp =left(trip,i);cout << temp << endl;delete[] temp;// point to temporary storage}// cin.get();return0;}// This function returns the first ct digits of the number num.unsignedlongleft(unsignedlong num,unsigned ct){unsigned digits =1;unsignedlong n = num;if(ct ==0|| num ==0)return0;// return 0 if no digitswhile(n /=10)digits++;if(digits > ct){ct = digits - ct;while(ct--)num /=10;return num;// return left ct digits}else// if ct >= number of digitsreturn num;// return the whole number}// This function returns a pointer to a new string// consisting of the first n characters in the str string.char*left(constchar* str,int n){if(n <0)n =0;char* p =newchar[n+1];int i;for(i =0; i < n && str[i]; i++)p[i]= str[i];// copy characterswhile(i <= n)p[i++]='\0';// set rest of string to '\0'return p;}