c++远征01启航篇

本课程是 c++ 的初级课程,是在 c 语言基础上的一个延伸,讲述了包括新增数据类型、命名空间等内容,最后通过一个通俗易懂的例子讲所述知识点融会贯通,以达到知识灵活运用,最终得以升华的目的。

C++远征之起航篇-慕课网

1 c++ 简介

c++那些事儿

# 诞生地: 贝尔实验室(Bell Lab)

c++ 之父:比亚尼.斯特劳斯特鲁普博士
为人低调,有问必答。

c++ 社区排行榜
TIOBE Index | TIOBE - The Software Quality Company

c++ 语言的应用领域

快、省

  • 嵌入式
  • 游戏编程
  • 网络编程
  • 系统编程

c和c++的关系

  • c 是 c++ 的子集
  • c++ 是从 c 的基础上发展而来的
  • c 语言 面向过程
  • c++支持 面向过程+ 面向对象

2 c++ IDE 环境搭建

什么是 IDE?
IDE(integrated development environment) ,集成卡法环境,一般包括:

  • 编辑器
  • 编译器
  • 调试器
  • 图形用户界面工具

工欲善其事,必先利其器

3 c++ 之初体验

1
2
3
4
5
6
7
#include<iostream> // 包含 c++ 标准输入输出库
using namespace std; // 使用 std namespace
int main()
{

cout<<"Hello C++"<<endl; // 输出语句
return 0;
}

4 c++ 语言新特性

4.1 c++ 的特性

  • 新的数据类型
  • 新的初始化方法
  • 随用随定义

新的数据类型

c语言数据类型

c++ 中新的数据类型: bool

相比 c 逻辑判断的方式,使用 bool 类型语义更加明确,程序更易读。

c or c++ 逻辑类型
c 没提供 非0 0
c++ bool true false

c语言

1
2
3
4
5
6
7
int flag = 0;
if (flag == 1) {
// to do
}
else {
// to do
}

c++

1
2
3
4
5
6
7
bool flag = 0;
if (flag) {
// to do
}
else {
// to do
}

新的初始化方法

c 语言提供的初始化方法

1
2
// 复制初始化 
int x = 1024;

c++ 提供两种初始化方法

  • 复制初始化
  • 直接初始化
1
2
// 直接初始化
int x(1024)

随用随定义

  • c 语言: 所有变量定义必须位于函数体的最前面
  • c++ :所有变量随用随定义

c语言

1
2
3
4
5
6
7
8
9
int main () {
int c1 = 3;
int v2 = 4;

v1 = v1 + 2;
v2 = v2 + v1;

return 0;
}

c++

1
2
3
4
5
6
7
8
9
int main () {
int c1 = 3;
v1 = v1 + 2;

int v2 = 4;
v2 = v2 + v1;

return 0;
}

4.2 练习(略)

4.3 c++ 的输入输出方式

c 语言的 I/O 方式

输入过程

输出过程

c++ 语言的输入输出方式

  • 不用关注占位符
  • 不用关注数据类型
  • 不容易出现问题

输入过程

输出过程

cin 语法形式

  • 使用 >> 指定写入哪个变量
1
2
cin >> x;
cin >> x >> y;

cout 语法形式

  • 使用 << 来拼接
  • endl 代表回车,可以省略
1
2
3
cout << x << endl;

cout << "x + y = " << x + y << endl;

4.4 练习(略)

4.5 c++ 新特性以及输入输出演示

以 mac 环境为例。

项目目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ tree -a -I .git
.
├── .DS_Store
├── .vscode
│ ├── c_cpp_properties.json # 配置 include path(激活代码补全和代码导航)
│ ├── launch.json # 配置 debugging 时需要执行的程序
│ └── tasks.json # 配置编译过程
└── JK_C-PLUS-PLUS
├── .DS_Store
├── build
│ └── IODemo.out
└── l01_setSail
└── 0405
└── IODemo.cpp

c_cpp_properties.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
"configurations": [
{
"name": "Mac",
"includePath": ["/usr/include"],// include path
"browse" : {
"limitSymbolsToIncludedHeaders" : true,
"databaseFilename" : ""
}
},
{
"name": "Linux",
"includePath": ["/usr/include"],
"browse" : {
"limitSymbolsToIncludedHeaders" : true,
"databaseFilename" : ""
}
},
{
"name": "Win32",
"includePath": ["c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"],
"browse" : {
"limitSymbolsToIncludedHeaders" : true,
"databaseFilename" : ""
}
}
]
}

launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/JK_C-PLUS-PLUS/build/IODemo.out",// 要 debug 的程序的路径
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
"preLaunchTask": "g++", // 每次 debug 前都应用 tasks 中的 g++ 任务编译一次
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
},
{
"name": "C++ Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceRoot}/build/IODemo.out",
"processId": "${command.pickProcess}",
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
]
}

tasks.json

1
2
3
4
5
6
7
8
9
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"args": ["${workspaceRoot}/JK_C-PLUS-PLUS/l01_setSail/0405/IODemo.cpp", "-o", "${workspaceRoot}/JK_C-PLUS-PLUS/build/IODemo.out"],// g++ 命令参数
"showOutput": "always"
}

调试运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Launching: '/Users/tonyearth/Projects/C-PLUS-PLUS_STUDY/JK_C-PLUS-PLUS/build/IODemo.out'
Working directory: '/Users/tonyearth/Projects/C-PLUS-PLUS_STUDY'
1 arguments:
argv[0] = '/Users/tonyearth/Projects/C-PLUS-PLUS_STUDY/JK_C-PLUS-PLUS/build/IODemo.out'
请输入一个整数:
2358
4466
2358
936
请输入一个布尔值(0或1):
0
false
Process exited with status 0

[进程已完成]

4.6 c++ 之 namespace

注意:命名空间的名字不可以重复!

A 程序库

1
2
3
4
5
namespace A {
int x = 0;
void f1();
void f2();
}

B 程序库

1
2
3
4
5
namespace B {
int x = 2;
void f1();
void f3();
}

使用

1
2
cout << A::x << endl;// 使用 A 库的 x 变量
B::f1();// 使用 B 库的 f1 函数

4.7 练习(略)

4.8 namespace 的演示

说明: 调用某些命名空间下的变量或函数时,有两种方式

指定默认的命名空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdlib.h>
#include <iostream>

using namespace std; // std 这个命名空间是在 iostream 中定义的
using namespace B;

namespace A
{
int x = 1;
void fun()
{

cout << "A" << endl;
}
}

namespace B
{
int x = 2;
void fun()
{

cout << "B" << endl;
}
void fun2()
{

cout << "BB" << endl;
}
}

int main(void)
{

cout << A::x << endl;
fun(); // "B::" 可以省略
fun2();
return 0;
}

每次调用都指定命名空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdlib.h>
#include <iostream>

namespace A
{
int x = 1;
void fun()
{

std::cout << "A" << std::endl;
}
}

namespace B
{
int x = 2;
void fun()
{

std::cout << "B" << std::endl;
}
void fun2()
{

std::cout << "BB" << std::endl;
}
}

int main(void)
{

std::cout << A::x << std::endl;
B::fun();
B::fun2();
return 0;
}

4.9 章节练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <stdlib.h>
using namespace std;
namespace myNum //填写命名空间的关键字
{
int x = 105;
}
int main()
{

// 使用bool类型定义isOdd,作为状态位
bool isFlag = false;

if(myNum::x % 2 == 0)
{
//改变状态位的值,使其为false
isFlag = false;
}
else
{
//改变状态位的值,使其为true
isFlag = true;
}
// 判断状态位的值
if(isFlag)
{
// 如果状态位的值为true,则打印变量x是奇数
cout << x << "是奇数" << endl;
}
else
{
// 如果状态位的值为false,则打印变量x是偶数
cout << x << "是偶数" << endl;
}
return 0;
}

5 综合练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*****************************************
* 知识点:命名空间 输入输出 bool
*******************************************/

#include <iostream>
using namespace std;

namespace CommonA
{
// 找出一个整型数组中的最大值或最小值
int getMaxOrMin(int *arr, int count, bool isMax)
{

int temp = arr[0];
for(int i = 1; i < count; i++) {
if(isMax)
{
if(temp < arr[i])
{
temp = arr[i];
}
}
else
{
if(temp > arr[i])
{
temp = arr[i];
}
}

}
return temp;
}

}

int main(void)
{

int arr1[4] = {3, 5, 9, 2};
bool isMax = false;

// 指定输出最大值还是最小值(0: 最小值, 1:最大值)
cin >> isMax;

cout << CommonA::getMaxOrMin(arr1, 4, isMax) << endl;
return 0;
}