《C++捷径教程》读书笔记--Chapter 8--函数,第二部分:引用,重载和默认参数
来源:互联网 发布:淘宝助理批量删除宝贝 编辑:程序博客网 时间:2024/05/16 02:12
//--《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;
}
- 《C++捷径教程》读书笔记--Chapter 8--函数,第二部分:引用,重载和默认参数
- 《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第二部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第二部分)
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第二部分)
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第二部分)
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第二部分)
- 《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第一部分)
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第一部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第三部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第四部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第五部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第六部分)
- C++中的引用变量、默认参数和函数重载
- 《C++捷径教程》读书笔记--Chapter 13--运算符的重载(完结)
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
- 终于可以歇一下了 稍微松一口气
- [SQL]最简单的分页显示语句
- 风吹的声音很好听
- JBuilder不支持Mobile 3D的开发吗?
- Visual C++ 6.0技术内幕笔记(二)
- 《C++捷径教程》读书笔记--Chapter 8--函数,第二部分:引用,重载和默认参数
- 纪念第一次登陆
- 今天装完了 Mysql 5.0 和 sql 2005 Express 。
- C#操作注册表
- 怒...!CSDN 应该出来给大家一个说法了!
- .Net WinForm学习笔记
- 彻底的被CSDN的blog系统给打败了,换地方!
- 我的BLOG终于开张了。。。
- sql exceise