目录
1. 前言:C++ 与 C语言
2. C++对于C语言语法的完善与补充
2.1 命名冲突与命名空间
2.1.1 命名空间的定义
namespace space1//[自定义名称]
{
//变量。。。。
int a;
//函数。。。。
int Add(int num1, int num2)
{}
//自定义类型。。。。
typedef struct Node
{}Node;
}
补充(命名空间可以嵌套使用):
namespace space1
{
namespace space2
{
int a;
}
}
2.1.2 调用方式
//调用命名空间内的资源
//变量
space::a = 1;
//函数
int ret = space::Add(1, 2);
//自定义类型
space1::Node node1;
//嵌套命名空间的调用方式
space1::space2::a = 1;
//打开命名空间,打开命名空间后可以不使用后缀访问其内资源
using namespace space;
using namespace space1::space2;
//引入space1空间中的a
using space1::a;
a = 2;
using space1::space2::a;
补充:
using namespace std;
//cout为继承ostream类型的对象
// << 为流插入操作符
//endl为行结束标志
cout << "hello world" << endl;
2.3 补充:流的概念
int num1 = 0;
//从键盘上读取数据将其存放入变量num中
cin >> num1;
int num2, num3;
//可连续读取
//回车或空格间隔
cin >> num2 >> num3;
//可连续插入
cout << num1 << ' ' << num2 << ' ' << num3 << endl;
2.4 缺省参数
2.4.1 缺省参数的使用
全缺省:
void Print(int a = 0, int b = 0, int c = 0)
{
cout << a << ' ' << b << ' ' << c << endl;
}
半缺省:
//示例1:(正确)
void Print(int a = 0, int b = 0, int c)
{
cout << a << ' ' << b << ' ' << c << endl;
}
//示例2:(错误)
void Print(int a = 0, int b, int c = 0)
{
cout << a << ' ' << b << ' ' << c << endl;
}
//示例3:(错误)
void Print(int a, int b = 0, int c = 0)
{
cout << a << ' ' << b << ' ' << c << endl;
}
2.5 函数重载
2.5.1 什么是函数重载
//示例1:
void swap1(int* num1, int* num2)
{
int tmp = *num1;
*num1 = *num2;
*num2 = tmp;
}
void swap2(char* str1, char* str2)
{
char tmp = *str1;
*str1 = *str2;
*str2 = tmp;
}
//示例2:
int Add1(int num1, int num2)
{
return num1 + num2;
}
int Add1(int num1, int num2, int num3)
{
return num1 + num2 + num3;
}
函数重载与编译器的函数名修饰规则:
2.5.2 函数重载的使用
参数类型不同(构成):
//构成重载
void swap(int* left, int* right)
{
int tmp = *left;
*left = *right;
*right = tmp;
}
void swap(char* left, char* right)
{
char tmp = *left;
*left = *right;
*right = tmp;
}
//调用方式:
int a = 10;
int b = 20;
swap(&a, &b);
char c1 = 'a';
char c2 = 'b';
swap(&c1, &c2);
参数个数不同(构成):
int Add(int nums1, int nums2)
{
return nums1 + nums2;
}
int Add(int nums1, int nums2, int nums3)
{
return nums1 + nums2 + nums3;
}
//调用方式:
cout << Add(10, 20) << endl;
cout << Add(10, 20, 30) << endl;
参数数量与类型相同,返回值不同(不构成):
int f()
{
return 10;
}
void f()
{
cout << 10 << endl;
}
2.5.3 特殊情况:函数重载与缺省参数的冲突
//记作Print1
void Print(int a = 0)
{
cout << a << endl;
}
//记作Print2
void Print()
{
cout << 0 << endl
}
2.6 引用与指针
2.6.1 什么是引用
int a = 10;
int& b = a;
b++;
cout << a << endl;
2.6.2 引用的使用方式
int a = 10;
//定义方式
int& b = a;
//报错,必须初始化
int& c;
2.6.3 const常引用
//常变量
const int a = 10;
//常引用
const int& b = a;
const int& b = 10;
double c = 3.14;
//精度丢失
const int& d = c;
//3
2.6.4 引用的应用场景
void swap(int& num1, int& num2)
{
int tmp = num1;
num1 = num2;
num2 = tmp;
}
int& Add(int num1, int num2)
{
static int sum = 0;
sum = num1 + num2;
return sum;
}
int& Add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
//err
int ret = Add(10, 20);
2.6.5 补充
2.7 内联函数
2.7.1 什么是内联函数
inline int Add(int num1, int num2)
{
return num1 + num2;
}
2.7.2 内联函数的调用机制
2.7.3 C++与宏
枚举示例:
//宏
//#define day 0
//枚举可调试
enum Day
{
Mon,
Tues,
Feb,
Thu,
Fir,
Sat,
Sun
}
Day day = Tue;
cout << "today is " << day << endl;
2.8 关键字auto
int i = 10;
auto a = &i;
auto* b = &i;
auto& c = i;
//得到变量的类型
cout << typeid(a).name() << endl;
cout << typeid(b).name() << endl;
cout << typeid(c).name() << endl;
*a = 20;
*b = 30;
c = 40;
//err
int Add(auto num1, auto num2)
{
return num1 + num2;
}
//err
auto arr[] = {1,2,3,4,5,6};
2.9 范围for
int arr[] = {1,2,3,4,5,6};
//普通for循环
int i = 0;
for(i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
cout << arr[i] << endl;
}
//范围for
for(auto e : arr)
{
cout << e << endl;
}
for(auto e : arr)
{
e *= 2;
}
for(auto e : arr)
{
cout << e << endl;
}
for(auto& e : arr)
{
e *= 2;
}
2.10 指针空值nullptr
void f(int* p)
{
cout << "int*" << endl;
}
void f(int p)
{
cout << "int" << endl;
}
f(NULL);
运行结果: