//使用域运算符访问被隐藏的全局变量
#i nclude <iostream>
#i nclude <iomanip>
#i nclude <string>
using namespace std;
int VAR=10;
void main()
{
int VAR=5;
cout<<VAR<<endl; //局部
cout<<::VAR<<endl; //全局
}
===========
//使用命名空间可以消除变量命名冲突
#i nclude <iostream>
#i nclude <iomanip>
#i nclude <string>
using namespace std;
namespace Output
{
const int MAXLEN=128;
int ivar=10;
void PutoutText(const char* pchData) //定义一个输出变量
{
if(pchData!=NULL) //判断指针是否为空
{
cout<<"PutoutText的命名空间:"<<pchData<<endl;
}
}
}
namespace Windows
{
typedef unsigned int UINT; //自定义一个类型
void PutoutText(const char* pchData) //定义一个输出变量
{
if(pchData!=NULL) //判断指针是否为空
{
cout<<"Windows的命名空间:"<<pchData<<endl;
}
}
}
void main()
{
//PutoutText("Welcome to China!"); //error
Output::PutoutText("Welcome to China!");
Windows::PutoutText("Welcome to China!");
using namespace Output;
PutoutText("Welcome to China!");
}
=======
//命名空间是可以嵌套的
#i nclude <iostream>
#i nclude <iomanip>
#i nclude <string>
using namespace std;
namespace Output
{
const int MAXLEN=128;
int ivar=10;
void PutoutText(const char* pchData) //定义一个输出变量
{
if(pchData!=NULL) //判断指针是否为空
{
cout<<"PutoutText的命名空间:"<<pchData<<endl;
}
}
namespace Output2
{
void PutoutText2(const char* pchData) //定义一个输出变
量
{
if(pchData!=NULL) //判断指针是否为空
{
cout<<"PutoutText的命名空
间:"<<pchData<<endl;
}
}
}
}
void main()
{
using namespace Output;
PutoutText("Welcome haha!");
using namespace Output::Output2;
PutoutText2("Welcome output2");
}
=============
//命名空间可无名,这里相当于只能在该文件中使用的全局对象. 但注意不能和全
局变量同名
#i nclude <iostream>
#i nclude <iomanip>
#i nclude <string>
using namespace std;
namespace
{
const int MAXLEN=128;
int ivar=10;
void PutoutText(const char* pchData) //定义一个输出变量
{
if(pchData!=NULL) //判断指针是否为空
{
cout<<"PutoutText的命名空间:"<<pchData<<endl;
}
}
}
void main()
{
PutoutText("Welcome haha!");
}
=========
//函数模板
#i nclude <iostream>
#i nclude <iomanip>
#i nclude <string>
using namespace std;
template <class type> //定义一个模板类型
type Sum(type xvar, type yvar)
{
return xvar+yvar;
}
void main()
{
int iret=Sum(10,20);
cout<<iret<<endl;
double iret2=Sum(10.5,20.2); //double iret2=Sum(10.5,20); //err
类型不同是不行的.
cout<<iret2<<endl;
}
======
//函数模板
#i nclude <iostream>
#i nclude <iomanip>
#i nclude <string>
using namespace std;
template <class type,int len> //定义一个模板类型
type Max(type array[len])
{
type ret=array[0];
for(int i=1;i<len;i++)
{
ret=(ret>array[i]?ret:array[i]);
}
return ret;
}
void main()
{
int array[5]={1,2,3,6,5};
int ret=Max<int,5>(array);
cout<<ret<<endl;
double array2[3]={1.2,3.5,6.7};
double ret2=Max<double,5>(array2);
cout<<ret2<<endl;
}
========
//函数模板也是可以重载的
#i nclude <iostream>
#i nclude <iomanip>
#i nclude <string>
using namespace std;
template <class type> //定义一个模板类型
type Sum(type xvar, type yvar) //两数求和
{
return xvar+yvar;
}
template <class type>
type Sum(type array[],int len) //数组求和
{
type ret=0;
for(int i=0;i<len;i++)
{
ret+=array[i];
}
return ret;
}
void main()
{
int iret=Sum(10,20);
cout<<iret<<endl;
int array[5]={1,2,3,4,5};
int iret2=Sum(array,5);
cout<<iret2<<endl;
}
==========