目录
<初识C++>《C++》之学习
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?——By 作者:新晓·故知
1. C++关键字(C++98)
2. 命名空间
2.1 命名空间定义
2.2 命名空间使用
3. C++输入&输出
4. 缺省参数 ?
4.1 缺省参数概念
4.2 缺省参数分类
5. 函数重载
5.1 函数重载概念
后记:●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教!
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?——By 作者:新晓·故知
<初识C++(1)>《C++初阶》
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?——By 作者:新晓·故知
“追风赶月莫停留,平芜尽处是春山”。?

(此图链接:4.函数缺省讲解)? (附注:此图狠狠共情了......)
1. C++关键字(C++98)
C++总计63个关键字,C语言32个关键字

2. 命名空间
在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作 用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字 污染,namespace关键字的出现就是针对这种问题的。
举例1:
在C语言编译器下:
解析:
在C语言中,rand是一个库函数,但C语言中的变量命名规则没有规定要和库名区别,因此再次包含头文件“#include<stdlib.h>”展开后会出错。
?
?在C语言中发生命名冲突,而在C++中引入namespace解决了这个问题:

?rand被隔离在命名空间,不会发生命名冲突!
命名冲突发生在同一个域里面,因此同一个域不能有同名变量。
举例2:
若同时打印全局变量,局部变量 ,局部优先打印:

若使用全局变量,需要加上作用域限定符“::”?

?在C++中,使用“::”进行访问:

举例3:

?举例4:

类型名Node相同,合并后会冲突!
?解决:



?注:
命名空间只能定义在全局!

?
但是展开命名空间不能随便使用!



??


?注:



?全部展开有冲突风险:
?解决1:指定命名空间(繁琐)
2.1 命名空间定义
定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名空间的成员。
//1. 普通的命名空间
namespace N1 // N1为命名空间的名称
{
// 命名空间中的内容,既可以定义变量,也可以定义函数
int a;
int Add(int left, int right)
{
return left + right;
}
}
//2. 命名空间可以嵌套
namespace N2
{
int a;
int b;
int Add(int left, int right)
{
return left + right;
}
namespace N3
{
int c;
int d;
int Sub(int left, int right)
{
return left - right;
}
}
}
//3. 同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间中。
namespace N1
{
int Mul(int left, int right)
{
return left * right;
}
}



注意:一个命名空间就定义了一个新的作用域,命名空间中的所有内容都局限于该命名空间中
2.2 命名空间使用
命名空间中成员该如何使用呢?比如:
namespace N
{
int a = 10;
int b = 20;
int Add(int left, int right)
{
return left + right;
}
int Sub(int left, int right)
{
return left - right;
}
}
int main()
{
printf("%d\n", a); // 该语句编译出错,无法识别a
return 0;
}
命名空间的使用有三种方式:
1.加命名空间名称及作用域限定符
int main()
{
printf("%d\n", N::a);
return 0;
}
2.使用using将命名空间中成员引入
using N::b;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
return 0;
}
3.使用using namespace 命名空间名称引入
using namespce N;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
Add(10, 20);
return 0;
}
?3. C++输入&输出
说明:
1.
使用
cout
标准输出
(
控制台
)
和
cin
标准输入
(
键盘
)
时,必须
包含
< iostream >
头文件
以及
std
标准命名空间。
注意:早期标准库将所有功能在全局域中实现,声明在
.h
后缀的头文件中,使用时只需包含对应头文件即可,后来将其实现在std
命名空间下,为了和
C
头文件区分,也为了正确使用命名空间,规定
C++
头文件不带.h
;旧编译器
(vc 6.0)
中还支持
<iostream.h>
格式,后续编译器已不支持,因此
推荐
使用
<iostream>+std
的方式。
2.
使用
C++
输入输出更方便,不需增加数据格式控制,比如:整形
--%d
,字符
--%c
#include <iostream>
using namespace std;
int main()
{
int a;
double b;
char c;
cin >> a;
cin >> b >> c;
cout << a << endl;
cout << b << " " << c << endl;
return 0;
}



4. 缺省参数 ?

附注:
祝福天下人有情人终成眷属!
(其实,作者连备胎都没有资格,此图狠狠共情了......? ?长叹吾生之余,亦诚挚祝福各位!)
4.1 缺省参数概念
缺省参数是
声明或定义函数时
为函数的
参数指定一个默认值
。在调用该函数时,如果没有指定实参则采用该默认值,否则使用指定的实参
void TestFunc(int a = 0)
{
cout<<a<<endl;
}
int main()
{
TestFunc(); // 没有传参时,使用参数的默认值
TestFunc(10); // 传参时,使用指定的实参
}
4.2 缺省参数分类
全缺省参数
void TestFunc(int a = 10, int b = 20, int c = 30)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
半缺省参数
void TestFunc(int a, int b = 10, int c = 20)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
?
注意:
1.
半缺省参数必须
从右往左依次
来给出,不能间隔着给
2.
缺省参数不能在函数声明和定义中同时出现
3.
缺省值必须是常量或者全局变量
4. C
语言不支持(编译器不支持)
//a.h
void TestFunc(int a = 10);
// a.c
void TestFunc(int a = 20)
{}
// 注意:如果生命与定义位置同时出现,恰巧两个位置提供的值不同,那编译器就无法确定到底该用那
个缺省值。
#include <iostream>
using namespace std;
void TestFunc(int a = 10, int b = 20, int c = 30)
{
cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "c= " << c << endl << endl;
}
int main()
{
TestFunc();
TestFunc(1);
TestFunc(1,2);
TestFunc(1,2,3);
return 0;
}

?半缺省的意义举例:

#include <iostream>
#include<cassert>
using namespace std;
//半缺省的意义举例
struct Stack
{
int* a;
int size;
int capacity;
};
void StackInit(struct Stack* ps, int n = 4)
{
assert(ps);
ps->a = (int*)malloc(sizeof(int) * n);
ps->size = 0;
ps->capacity = n;
}
int main()
{
Stack st;
//不赋值,就默认开辟4个空间
StackInit(&st);
//根据需要赋值开辟指定空间
StackInit(&st,100);
return 0;
}
?注:
1.不允许同时在函数声明、函数定义处赋值,防止不同赋值歧义
2.允许在函数声明处赋值,在函数定义处不赋值
3.不允许在函数声明处不赋值,在函数定义处赋值,因为与编译、链接有关,预处理首先是函数的声明
5. 函数重载
自然语言中,一个词可以有多重含义,人们可以通过上下文来判断该词真实的含义,即该词被重载了。
比如:以前有一个笑话,国有两个体育项目大家根本不用看,也不用担心。一个是乒乓球,一个是男足。前者是“
谁也赢不了!
”
,后者是
“
谁也赢不了!
”
5.1 函数重载概念
函数重载
:
是函数的一种特殊情况,
C++
允许在
同一作用域中
声明几个功能类似
的同名函数
,这些同名函数的
形参列表
(
参数个数 或 类型 或 顺序
)
必须不同
,常用来处理实现功能类似数据类型不同的问题
?
?
?在C语言中,若交换int、double型的数,需要定义两个不同的交换函数。但C++支持,就方便很多!
后记: ●由于作者水平有限,文章难免存在谬误之处,敬请读者斧正,俚语成篇,恳望指教!
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?——By 作者:新晓·故知
|