文章目录
一、 C++的概念
1、 C语言是结构化和模块化的语言,适合处理较小规模的程序。对于复杂的问题,规模较大的程序,需要高度的抽象和建模时,C语言则不合适。
2、为了解决软件危机, 20世纪80年代,计算机界提出了OOP(object oriented programming:面向对象)思想,支持面向对象的程序设计语言应运而生。
3、1982年,Bjarne Stroustrup博士在C语言的基础上引入并扩充了面向对象的概念,发明了一种新的程序语言:C++。
4、C++是基于C语言而产生的,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行面向对象的程序设计。
二、 C++的发展史
2.1 C++语言排行榜
2.2 C++语言发展历程
阶段 | 内容 |
---|---|
C with classes | 类及派生类、公有和私有成员、类的构造和析构、友元、内联函数、赋值运算符重载等 |
C++1.0 | 添加虚函数概念,函数和运算符重载,引用、常量等 |
C++2.0 | 更加完善支持面向对象,新增保护成员、多重继承、对象的初始化、抽象类、静态成员以及const成员函数 |
C++3.0 | 进一步完善,引入模板,解决多重继承产生的二义性问题和相应构造和析构的处理 |
C++98 | C++标准第一个版本,绝大多数编译器都支持,得到了国际标准化组织(ISO)和美国标准化协会认可,以模板方式重写C++标准库,引入了STL(标准模板库) |
C++03 | C++标准第二个版本,语言特性无大改变,主要:修订错误、减少多异性 |
C++05 | C++标准委员会发布了一份计数报告(Technical Report,TR1),正式更名C++0x,即:计划在本世纪第一个10年的某个时间发布 |
C++11 | 增加了许多特性,使得C++更像一种新语言,比如:正则表达式、基于范围for循环、auto关键字、新容器、列表初始化、标准线程库等 |
C++14 | 对C++11的扩展,主要是修复C++11中漏洞以及改进,比如:泛型的lambda表达式,auto的返回值类型推导,二进制字面常量等 |
C++17 | 在C++11上做了一些小幅改进,增加了19个新特性,比如:static_assert()的文本信息可选,Fold表达式用于可变的模板,if和switch语句中的初始化器等 |
C++20 | 自C++11以来最大的发行版,引入了许多新的特性,比如:模块(Modules)、协程(Coroutines)、范围(Ranges)、概念(Constraints)等重大特性,还有对已有特性的更新:比如Lambda支持模板、范围for支持初始化等 |
C++23 | 制定ing |
三、
3.1 C++关键字
C++语言中总计有63个关键字,C语言中仅有32个关键字,接下来我们来了解一下C++中都有哪些具体的关键字。
3.2 命名空间
3.2.1 C语言变量名冲突代码回顾
#include <stdio.h>
int a = 0;
int main()
{
int a = 2;
printf("%d\n", a);//变量名冲突时,局部优先
return 0;
}
结果如下:
#include <stdio.h>
int a = 0;
int main()
{
//int a = 2;
printf("%d\n", a);//有局部局部优先,有全局无局部全局优先
return 0;
}
结果如下:
3.2.2.1 引入域后变量名冲突
#include <iostream>
//int a = 0;
namespace space
{
int a = 10;
}
int main()
{
//int a = 2;
printf("%d\n", a);
return 0;
}
结果如下:
3.2.2.2 学习小贴士
由上述3个代码我们可以发现,编译器在进行代码编译时优先搜索局部变量,再搜索全局变量,当展开命名空间域或者指定访问命名空间域时访问域内。域作用限定域在不指定的情况下是不会进行搜索的。
3.2.3 展开命名空间域
#include <iostream>
//int a = 0;
namespace space
{
int a = 10;
}
using namespace space;//展开命名空间域访问
int main()
{
//int a = 2;
printf("%d\n", a);
return 0;
}
结果如下:
#include <iostream>
int a = 0;
namespace space
{
int a = 10;
}
int main()
{
int a = 2;
printf("%d\n", a);
printf("%d\n", ::a);
printf("%d\n", space::a);//指定访问命名空间域
return 0;
}
结果如下:
3.2.4 域作用限定域代码示例
由上述代码我们可以联想到之前我们在C语言中曾经学习过的内容:当局部变量和全局变量名字冲突的情况下,局部变量优先。那么,在C++语言中,如果我们想要直接访问全局变量该怎么办呢?
接下来我们将介绍一下这个问题的答案:使用域作用限定域::
int a = 0;
int main()
{
int a = 1;
printf("%d\n", a);
printf("%d\n", ::a);//域作用限定域::,当左边为空白时默认访问全局变量。
return 0;
}
结果如下:
3.2.5 命名空间冲突错误代码示例
#include <stdio.h>
#include <stdlib.h>
//这个库中定义了rand函数,就相当于有了一个全局的rand
//和下面定义的rand全局变量冲突,所以不能直接用using namespace展开,而要用指定访问命名空间域
int rand = 0;
int main()
{
printf("%d\n", rand);
return 0;
}
结果如下:
3.2.6 命名空间域解决冲突代码示例
#include <iostream>
namespace space
{
int rand = 0;
}
int main()
{
printf("%d\n", space::rand);
return 0;
}
结果如下:
3.2.7 同时访问代码示例
#include <stdio.h>
#include <stdlib.h>
namespace space
{
int rand = 0;
}
int main()
{
printf("%d\n", space::rand);
printf("0x%p\n", rand);
return 0;
}
结果如下:
3.2.8 命名空间内部设定
namespace bit
{
// 命名空间中可以定义变量/函数/类型
int rand = 10;
int Add(int left, int right)
{
return left + right;
}
//注意:一个命名空间就定义了一个新的作用域,命名空间中的所有内容都局限于该命名空间中
struct Node
{
struct Node* next;
int val;
};
}
3.2.9 命名空间嵌套
#include <iostream>
namespace N1
{
int a = 1;
int b;
int Add(int left, int right)
{
return left + right;
}
namespace N2
{
int a = 2;
int c;
int d;
int Sub(int left, int right)
{
return left - right;
}
}
}
int main()
{
printf("%d\n", N1::a);
printf("%d\n", N1::N2::a);
return 0;
}
结果如下:
3.4 学习小贴士
在C/C++中,变量、函数和类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,这可能会导致很多冲突。
使用命名空间的目的是:对标识符的名称进行本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的。
3.3 C++输入&输出
#include <iostream>
#include <vector>
#include <list>
int main()
{
std::cout << "hello world!" << std::endl;
std::cout << "hello world!" << std::endl;
std::cout << "hello world!" << std::endl;
std::list<int> It;
return 0;
}
结果如下:
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main()
{
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
list<int> It;
return 0;
}
结果如下:
#include <iostream>
#include <vector>
#include <list>
using std::cout;
using std::endl;
int main()
{
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
cout << "hello world!" << endl;
std::list<int> It;
return 0;
}
结果如下:
3.4 缺省参数
3.4.1 缺省参数的定义
缺省参数是声明或定义函数时为函数的参数指定一个缺省值。
在调用该函数时,如果没有指定实参则采用该形参的缺省值,否则使用指定的实参。
3.4.2 缺省参数代码示例(一)
#include <iostream>
using namespace std;
void Func(int a = 0)
{
cout << a << endl;
}
int main()
{
Func();
// 没有传参时,使用参数的默认值
Func(10);
// 传参时,使用指定的实参
return 0;
}
结果如下:
3.4.2 缺省参数代码示例(二)
#include <iostream>
using namespace std;
void Func(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
int main()
{
Func();
Func(1);
Func(1,2);
Func(1,2,3);
return 0;
}
结果如下:
#include <iostream>
using namespace std;
//半缺省:从右往左缺省
void Func(int a, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
int main()
{
Func(1);
Func(1,2);
Func(1,2,3);
return 0;
}
结果如下:
3.4.3 学习小贴士
- 半缺省参数必须从右往左依次来给出,不能间隔着赋值。
- 缺省参数不能在函数声明和定义中同时出现。
- 缺省值必须是常量或者全局变量
3.5 函数重载
3.5.1 函数重载的概念
函数重载是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数的形参列表(参数个数或类型或类型顺序)不同,常用来处理实现功能类似数据类型不同的问题。
3.5.1 函数重载代码示例(一)
// 1、参数类型不同
#include <iostream>
using std::cout;
using std::endl;
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;
}
int main()
{
cout << Add(1, 2) << endl;
cout << Add(1.1, 2.2) << endl;
return 0;
}
结果如下:
3.5.1 函数重载代码示例(二)
// 2、参数个数
#include <iostream>
using std::cout;
using std::endl;
void f()
{
cout << "f()" << endl;
}
void f(int a)
{
cout << "f(int a)" << endl;
}
int main()
{
f();
f(1);
return 0;
}
结果如下:
3.5.1 函数重载代码示例(三)
// 3、参数类型顺序不同
#include <iostream>
using std::cout;
using std::endl;
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()
{
f(10, 'a');
f('a', 10);
return 0;
}
结果如下: