一.C++的63个关键字
asm | do | if | return | try | continue |
auto | double | inline | short | typedef | for |
bool | dynamic_cast | int | signed | typeid | public |
break | else | long | sizeof | typename | throw |
case | enum | mutable | static | union | wchar_t |
catch | explicit | namespace | static_cast | unsigned | default |
char | export | new | struct | using | friend |
class | extern | operator | switch | virtual | register |
const | false | private | template | void | true |
const_cast | float | protected | this | volatile | while |
delete | goto | reinterpret_cast |
注:C语言有32个关键字
二.命名空间
C语言中,在同一个作用域中,不允许出现同名变量,而在C++的一个项目工程中,需要多人协作完成,其中可能就会出现命名重复等相关问题,为解决这一问题,C++引入了namespace关键字,即命名空间
定义:namespace+自定义空间名
访问空间命名的两种方式
(1)使用using namespace+自定义空间名称
(2)自定义空间名称+::变量名称or函数
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using std::cout;
using std::endl;
namespace mise
{
void push()
{
cout<<"hello C++"<<endl;
}
}
int main()
{
mise::push();
return 0;
}
or
using std::cout;
using std::endl;
namespace mise
{
void push()
{
cout<<"hello C++"<<endl;
}
}
using mise::push;
int main()
{
push();
return 0;
}
打印结果:注:std是一个命名空间的名字,它是standard的缩写
using namespace std;相当全部展开了std里的函数、全局变量、类
为了方便,一般我们不直接使用,而是部分展开std里的内容,像using std::cout、using std::endl等等
三.输入和输出
(1)流插入
<<符号一种表示左移符号,另一种表示流插入
流插入 自动识别类型
int i=100;
char* str=“hello C++”;
char ch="\n";
cout << str << i << ch << endl;
printf("%s%d%c", str,i,ch);
//cout<<a与printf("%d",a)的效果相同
(2)流提取
char ch = '\n';
int i=100;
cin>>i>>ch;
scanf("%d%c",&i,&ch);
注:C++的输入和输出是自动识别变量类型的;
cout和cin是全局的流对象,endl是特殊的C++符号,表示换行输出;
使用cout标准输出对象(控制台)和cin标准输入对象(键盘)时,必须包含<iostream>头文件;
i<<1表示向左移动一位,与cout搭配使用表示流插入
四.缺省参数
(1)概念:缺省参数是声明或定义函数时为函数的参数指定一个缺省值,在调用该函数时,如果没有指定实参则采用该形参的缺省值,否则使用指定的实参
#include<iostream>
using std::cout;
using std::endl;
void Func(int a = 0)
{
cout << a << endl;
}
int main()
{
Func(1);
Func();
return 0;
}
打印结果:
(2)缺省函数的分类
全缺省参数
void Func(int a = 10, int b = 20, int c = 30)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
半缺省参数
void Func(int a, int b = 10, int c = 20)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
注意:1.半缺省参数必须从右到左依次给,不能间隔着给;
2.缺省参数不能在函数的声明和定义中同时出现;
//a.h
void Func(int a = 10);
//a.cpp
void Func(int a = 20)
{}
注意:如果生命与定义位置同时出现,恰巧两个位置提供的值不同,那编译器就无法确定到底该用那个缺省值。
3.缺省值必须是常量或者全局变量;
4.C语言不支持(编译器不支持)
五.函数重载
概念:函数重载是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数的形参列表(参数个数 或 类型 或 类型顺序)不同,常用来处理实现功能类似数据类型不同的问题。
#include<iostream>
using namespace std;
// 1、参数类型不同
int Add(int left, int right)
{
cout << "int Add(int left, int right)" << endl;
return left + right;
}
double Add(double left, double right)
{
cout << "double Add(double left, double right)" << endl;
return left + right;
}
// 2、参数个数不同
void f()
{
cout << "f()" << endl;
}
void f(int a)
{
cout << "f(int a)" << endl;
}
// 3、参数类型顺序不同
void f(int a, char b)
{
cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
cout << "f(char b, int a)" << endl;
}
int main()
{
Add(10, 20);
Add(10.1, 20.2);
f();
f(10);
f(10, 'a');
f('a', 10);
return 0;
}
运行结果: