//--《C++捷徑教程》讀書筆記--Chapter 8--函數,第二部分:引用,重載和預設參數
//--Chapter 8--函數,第二部分:引用,重載和預設參數
//--11/15/2005 Tues.
//--Computer Lab
//--Liwei
//--程式#1 值傳遞
#include <iostream>
using namespace std;
int sqr_it(int x);
int main()
{
int t=10;
cout<<sqr_it(t)<<' '<<t<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
int sqr_it(int x)
{
x=x*x;
return x;
}
//--程式#2 指標傳遞
#include <iostream>
using namespace std;
void swap(int *x,int *y);
int main()
{
int i,j;
i=10;
j=20;
cout<<"Initial values of i and j: ";
cout<<i<<' '<<j<<endl;
swap(&j,&i);
cout<<"Swapped values of i and j: ";
cout<<i<<' '<<j<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
//--程式#3 引用參數
#include <iostream>
using namespace std;
void f(int &i);
int main()
{
int val=1;
cout<<"Old value for val: "<<val<<endl;
f(val);
cout<<"New value for val: "<<val<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void f(int &i)
{
i=100;
}
//--程式#4 引用
#include <iostream>
using namespace std;
void swap(int &x,int &y);
int main()
{
int i,j;
i=10;
j=20;
cout<<"Initial values of i and j: ";
cout<<i<<' '<<j<<endl;
swap(j,i);
cout<<"Swapped values of i and j: ";
cout<<i<<' '<<j<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
//--程式#5 返回一個引用
#include <iostream>
using namespace std;
double &f();
double val=100.0;
int main()
{
double newval;
cout<<f()<<endl;
newval=f();
cout<<newval<<endl;
f()=99.1;
cout<<f()<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
double &f()
{
return val;
}
//--程式#6 返回引用
#include <iostream>
using namespace std;
double &change_it(int i);
double vals[]={1.1,2.2,3.3,4.4,5.5};
int main()
{
int i;
cout<<"Here are the original values: ";
for(i=0; i<5; i++)
cout<<vals[i]<<' ';
cout<<endl;
change_it(1)=5298.23;
change_it(3)=-98.8;
cout<<"Here are the changed values: ";
for(i=0; i<5; i++)
cout<<vals[i]<<' ';
cout<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
double &change_it(int i)
{
return vals[i];
}
//--程式#7 一個簡單的有界數組
#include <iostream>
using namespace std;
int &put(int i);
int get(int i);
int vals[10];
int error=-1;
int main()
{
put(0)=10;
put(1)=20;
put(9)=30;
cout<<get(0)<<' ';
cout<<get(1)<<' ';
cout<<get(9)<<' '<<endl;
put(12)=12;
int xxx=get(12);
cout<<xxx<<' '<<error<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
int &put(int i)
{
if(i>=0 && i<10)
return vals[i];
else{
cout<<"Bounds put() error!/n";
return error;//有錯誤
}
}
int get(int i)
{
if(i>=0 && i<10)
return vals[i];
else{
cout<<"Bounds get() error!/n";
return error;
}
}
// 使用引用變數的限制:
// .不能引用一個引用變數
// .不能建立引用數組
// .不能建立指向引用的指標
// .在位域中不使用引用
//--程式#8 獨立引用
#include <iostream>
using namespace std;
int main()
{
int j,k;
int &i=j;//獨立引用
j=10;
cout<<j<<" "<<i<<endl;
k=121;
i=k;
cout<<j<<' '<<i<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程式#9 函數重載
#include <iostream>
using namespace std;
void f(int i);
void f(int i, int j);
void f(double k);
int main()
{
f(10);
f(10,20);
f(12.23);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void f(int i)
{
cout<<"in f(int) , i is :"<<i<<endl;
}
void f(int i,int j)
{
cout<<"in f(int,int), i is:"<<i;
cout<<", j is: "<<j<<endl;
}
void f(double k)
{
cout<<"int f(double), k is :"<<k<<endl;
}
//--程式#10 函數重載
#include <iostream>
using namespace std;
int myabs(int i);
double myabs(double d);
long myabs(long l);
int main()
{
cout<<myabs(-10)<<endl;
cout<<myabs(-11.0123)<<"/n";
cout<<myabs(-9L)<<'/n';
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
int myabs(int i)
{
cout<<"Using integer myabs():";
if(i<0)
return -i;
else
return i;
}
double myabs(double d)
{
cout<<"Using double myabs(): ";
if(d<0.0)
return -d;
else
return d;
}
long myabs(long l)
{
cout<<"Using long myabs(): ";
if(l<0)
return -l;
else
return l;
}
//--程式#11 預設參數
#include <iostream>
using namespace std;
void clrscr(int size=25);
int main()
{
int i;
for(i=0; i<30; i++)
cout<<i<<endl;
clrscr();
for(i=0; i<30; i++)
cout<<i<<endl;
clrscr(10);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void clrscr(int size)
{
for(; size; size--)
cout<<'/n';
}
//--程式#12 預設參數 函數strcat()的定製版本
#include <iostream>
#include <cstring>
using namespace std;
void mystrcat(char *s1, char *s2, int len=-1);
int main()
{
char str1[80]="This is a test";
char str2[80]="0123456789";
mystrcat(str1,str2,5);
cout<<str1<<endl;
strcpy(str1,"this is a test");
mystrcat(str1,str2);
cout<<str1<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void mystrcat(char *s1, char *s2, int len)
{
while(*s1)
s1++;
if(len==-1)
len=strlen(s2);
while(*s2 && len){
*s1=*s2;
s1++;
s2++;
len--;
}
*s1='/0';
}
//--程式#13 重載的歧異性
#include <iostream>
using namespace std;
float myfunc(float i);
double myfunc(double i);
int main()
{
cout<<myfunc(10.1)<<" ";
cout<<myfunc(10);//error
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
float myfunc(float i)
{
return i;
}
double myfunc(double i)
{
return -i;
}
//--程式#14 又一個歧異性錯誤
#include <iostream>
using namespace std;
char myfunc(unsigned char ch);
char myfunc(char ch);
int main()
{
cout<<myfunc('c')<<endl;
cout<<myfunc(-66)<<endl;
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
char myfunc(unsigned char ch)
{
return ch-1;
}
char myfunc(char ch)
{
return ch+1;
}
//--程式#15 歧異性錯誤
#include <iostream>
using namespace std;
int myfunc(int i);
int myfunc(int i,int j=1);
int main()
{
cout<<myfunc(4,5)<<" ";
cout<<myfunc(10);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
int myfunc(int i)
{
return i;
}
int myfunc(int i, int j)
{
return i*j;
}