***关键字:***asm do if return try continueauto double inline short typedef forbool dynamic_cast int signed typeid publicbreak else long sizeof typename throwcase enum mutable static union wchar_tcatch explicit namespace static_cast unsigned defaultchar export new struct using friendclass extern operator switch virtual registerconst false private template void trueconst_cast float protected this volatile whiledelete goto reinterpret_cas
命名空间:解决名字冲突(名字污染);
命名空间定义:1.普通命名空间;
2.嵌套;
3.同一文件中可以包含多个相同名称的命名空间,编译器会将其合并成一个;
命名空间的使用:1.命名空间中某个成员在文件中偶尔被用到 N::a;2.命名空间中某个成员在文件中多次被用到using N::a;N命名空间中的成员就相当于当前文件中的全局变量;
全缺省:
半缺省:
函数重载一词多义函数
**重载的条件:**相同的作用域,函数名字必须相同,参数列表必须不同在程序编译期间对函数的参数进行推演,根据推演结果选择合适的函数调用函数重载与返回值类型是否相同没有关系
//#include<iostream>
//using std::cout;
//using namespace std;
/*//命名空间---解决名字冲突
#include<stdio.h>
#include<stdlib.h>
namespace N1
{
int a = 10;
int b = 20;i
nt add(int left, int right)
{
return left + right;
}
}
namespace N2
{
int a = 30;
int b = 40;
int sub(int left, int right)
{
return left - right;}
namespace N3
{
int a = 10;
int b = 20;
int mul(int left, int right)
{
return left * right;
}
}
}
//同一个工程中可以定义多个相同名字的命名空间
//编译器最终会吧多个命名空间合并成一个
namespace N1
{
int c = 10;
int d = 20;
int div(int left, int right)
{
if (right == 0)
{
exit(1);
}
return left / right;
}
}
//int a = 50;
using N1::a;
//方式一:命名空间名字::成员名字
//方式二:using 命名空间名字::成员名字;可能造成命名冲突问题
// 场景:当前命名空间中个别成员多次在某个文件中被使用
int main()
{
int a = 60;
printf("%d", a);
//printf("%d", ::a);
printf("%d", N1::a);
printf("%d", N1::a);
printf("%d", N1::a);
printf("%d", N1::a);
printf("%d", N1::a);
return 0;
}
//方式三:场景:当前命名空间中成员在某个文件中应用比较多
//缺陷:冲突率比价高
using namespace N1;int main()
{
printf("%d %d %d", a, b, add(a, d));
return 0;}
*//*int main()
{
int a = 0;
cin >> a;
double d;
cin >> a >> d;
cout << 10 << " " << 12.34 << "\n";
cout << "hello word" << std::endl;
return 0;
}
*//*//全缺省
//从左往右
void testfunc(int a = 1, int b = 2, int c=3)
{
cout << a << " " << b << " " << c << endl;
}
int main()
{
testfunc(10, 20);
testfunc(10);
testfunc();
return 0;
}
//半缺省
//只能从右往左依次给出,不能跳着給
void testfunc(int a, int b = 2, int c = 3)
{
cout << a << " " << b << " " << c << endl;
}
int main()
{
testfunc(10, 20,30);
testfunc(10,20);
testfunc(10);
return 0;
}
*/
//通用add函数
//函数重载的条件:相同的作用域,函数名字必须相同,参数列表必须不同
int Add(int left, int right)
{
return left + right;
}
double Add(double left, double right)
{
return left + right;
}
char Add(char left, char right)
{
return left + right;
}
int main()
{
Add(10, 20);
Add(1.24, 2.45);
Add('a', 'b');
return 0;
}
参数推演–在编译器阶段对函数实参类型进行推演,根据推演结果决定调用哪一个函数
如没有合适类型匹配的重载函数: 1.尝试隐式类型转换—直接调用
2.编译失败