A.头文件和空间名
1.头文件的区别
a.c语言的头文件可以省略".h"但要在前面加个c,如"stdio.h"->cstdio"
#include<stdio.h>//写法1
#include<cstdio>//写法2
//在cpp中,可以有两种写法,c语言只能用写法1
2.空间名
a.空间的定义:namespace 空间名{}
例:
//命名空间"GG"
namespace GG
{
int a;
}
//命名空间"MM"
namespace MM
{
int a;
}
b.使用命名空间:命名空名字::
例:
cout<<MM::a<<endl;//输出命名空间MM中的变量a
cout<<GG::a<<endl;//输出命名空间GG中的变量a
c.using语法:using namespace 空间名;
例:
using namespace MM;
使用该语法后在作用域内可以省略"作用域:"
B.基本的输入和输出
1.需要包含的头文件:iostream
#include<iostream>
2.输入
cin 注:输入缓冲区默认省略空格,回车
cin>>a;//输入一个变量
cin>>a>>b;//输入两个变量
3.输出
cout
cout<<a;//输出变量a
cout<<"hello"<<endl//输出字符串"hello","endl"表示换行,也可以用\n
cout<<"\n"//换行
cout<<a<<" "<<b;//输出变量a和b
cout<<endl;//换行
C.C++新的数据类型
1.auto
用于自动推断类型
auto a=1;//a就是int类型因为1是int
auto a=2.1f;//a就是float类型因为2.1是float
//还可以用于推断结构体,函数等类型
2.引用
a.语法:类型& 引用名=要引用的类型
可以理解为起别名,改变"别名"相当于改变本身
int a=5
int& aa=a;
aa=10
cout<<a;//结果是10
b.右值引用:类型&& 引用名=右值
等号右边必须为右值不可以是左值
int &&bb=10;
3.bool
只有两个值:0(false) 1(true)
非0为1
bool b1;
b1=false;
cout<<b1<<endl;//结果为0
D.C++的函数思想
1.内敛函数
a.用法为函数前加关键字:inline
b.作用为将函数以二进制存储,从而牺牲大小使函数调用更快,推荐给短小精悍的函数用
inline int add(int a,int b)
{
return a+b;
}
2.函数的重载
a.允许出现两个一样的函数名,但是其参数个数,位置不同
int add(int a,int b)
{
return a+b;
}
add(float a,int b)
{
return a+b;
}
add(int a,float b)
{
return a+b;
}
add(int a,int b,int c)
{
return a+b+c;
}
3.函数的默认参数
给函数默认的参数,必须从右往左
add(int a=3,int b=2)
{
return a+b;
}
add(int a,int b=1)
{
return a+b;
}
欢迎各位大佬补充