Skip to content

Commit f4324cf

Browse files
authored
Merge pull request #86 from excniesNIED/master
feat(cpp): Add C++ related changes
2 parents 675ce2d + aafe8dd commit f4324cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2738
-2543
lines changed

.cache/plugin/git-committers/page-authors.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# C++ 基本语法
2+
3+
语法指的是在编程语言中编写语句的规则和规定。它们也可以被看作是定义编程语言结构的语法规则。
4+
5+
[**C++ 语言**](https://www.geeksforgeeks.org/c-plus-plus/) 也有其自身功能所对应的语法。不同的语句有不同的语法来规定其用法,但 C++ 程序也有一些贯穿所有程序的基本语法规则。
6+
7+
## C++ 语法
8+
9+
我们可以通过下面的程序来学习 **C++ 基本语法**
10+
11+
```cpp
12+
#include <iostream>
13+
14+
// 标准命名空间
15+
using namespace std;
16+
17+
// 主函数
18+
int main() {
19+
20+
// 声明变量
21+
int num1 = 24;
22+
int num2 = 34;
23+
24+
int result = num1 + num2;
25+
26+
// 输出
27+
cout << result << endl;
28+
29+
// 返回语句
30+
return 0;
31+
}
32+
```
33+
34+
**输出**
35+
36+
```
37+
58
38+
```
39+
40+
上面的程序展示了一个包含头文件、主函数、命名空间声明等基本元素的 C++ 程序。让我们逐一理解它们。
41+
42+
### 1. 头文件
43+
44+
[头文件](https://www.geeksforgeeks.org/header-files-in-c-c-with-examples/) 包含了我们在程序中使用的函数和宏的定义。在 **第 1 行**,我们使用 **#include <iostream>** 语句告诉编译器包含 **iostream** 头文件库,该库存储了我们用于输入和输出的 `cin``cout` 标准输入/输出流的定义。**#include** 是一个预处理器指令,我们用它来导入头文件。
45+
46+
### 2. 命名空间
47+
48+
C++ 中的[命名空间](https://www.geeksforgeeks.org/namespace-in-c/)用于提供一个作用域或一个区域,在其中我们可以定义标识符。在 **第 2 行**,我们使用了 **using namespace std** 语句,用以指定我们将使用标准命名空间,所有标准库函数都在该命名空间中定义。
49+
50+
### 3. 主函数
51+
52+
**第 3 行**,我们将主函数定义为 **int main()**[函数](https://www.geeksforgeeks.org/functions-in-cpp/)是任何 C++ 程序中最重要的部分。程序的执行总是从 `main` 函数开始。所有其他函数都由 `main` 函数调用。在 C++ 中,`main` 函数需要返回一个值来表示执行状态。
53+
54+
### 4. 代码块
55+
56+
代码块是被 **{ }** 大括号括起来的一组语句。`main` 函数的主体是从 **第 4 行****第 9 行**,被 **{ }** 括起来的部分。
57+
58+
### 5. 分号
59+
60+
你可能已经注意到,上面代码中的每条语句后面都有一个 ( **;** ) 分号符号。它用于终止程序中的每一行语句。
61+
62+
### 6. 标识符
63+
64+
我们使用[标识符](https://www.geeksforgeeks.org/c-identifiers/)来命名[**变量**](https://www.geeksforgeeks.org/cpp-variables/)、函数和其他用户定义的数据类型。标识符可以由大写和小写字母、下划线和数字组成。第一个字符必须是下划线或字母。
65+
66+
### 7. 关键字
67+
68+
在 C++ 编程语言中,有一些保留字在 C++ 程序中具有特殊的含义。它们不能用作标识符。例如,我们程序中使用的 **int****return****using** 就是一些[关键字](https://www.geeksforgeeks.org/keywords-in-c/)
69+
70+
### 8. 基本输出 cout
71+
72+
在第 7 行,我们使用了 [`cout`](https://www.geeksforgeeks.org/cout-in-c/) 流对象(在 [`` 头文件](https://www.geeksforgeeks.org/basic-input-output-c/)中声明)将两个数的和打印到标准输出流(stdout)。
73+
74+
### 9. 注释
75+
76+
你可以在代码中看到带有两个正斜杠 (//) 和一些文本的内容,这在 C++ 程序中被称为[**注释**](https://www.geeksforgeeks.org/cpp-comments/)。注释提供了关于代码或特定代码行的信息,以提高可读性。
77+
78+
## C++ 中的面向对象编程
79+
80+
C++ 编程语言同时支持面向过程编程和面向对象编程。上面的例子是基于面向过程编程范式的。所以让我们用另一个例子来讨论 [C++ 中的面向对象编程](https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/)
81+
82+
```cpp
83+
#include <iostream>
84+
using namespace std;
85+
86+
class Calculate {
87+
88+
// 访问修饰符
89+
public:
90+
91+
// 数据成员
92+
int num1 = 50;
93+
int num2 = 30;
94+
95+
// 成员函数
96+
void addition() {
97+
int result = num1 + num2;
98+
cout << result << endl;
99+
}
100+
};
101+
102+
int main() {
103+
104+
// 对象声明
105+
Calculate add;
106+
107+
// 调用成员函数
108+
add.addition();
109+
110+
return 0;
111+
}
112+
```
113+
114+
**输出**
115+
116+
```
117+
80
118+
```
119+
120+
### 1. 类
121+
122+
[](https://www.geeksforgeeks.org/c-classes-and-objects/)是一种用户定义的数据类型。一个类有它自己的属性(数据成员)和行为(成员函数)。在 **第 3 行**,我们声明了一个名为 **Calculate** 的类,它的主体从 **第 3 行** 扩展到 **第 7 行**
123+
124+
### 2. 类成员
125+
126+
类中的属性或数据由数据成员定义,而操作这些数据成员的函数被称为成员函数。
127+
128+
在上面的例子中,`num1``num2` 是数据成员,而 `addition()` 是一个操作这两个数据成员的成员函数。这里有一个关键字 **public**,它是一个**访问修饰符**[访问修饰符](https://www.geeksforgeeks.org/access-modifiers-in-c/)决定了谁可以访问这些数据成员和成员函数。
129+
130+
### 3. 对象
131+
132+
[对象](https://www.geeksforgeeks.org/c-classes-and-objects/)是类的一个实例。类本身只是一个模板,不会被分配任何内存。要使用类中定义的数据和方法,我们必须创建该类的一个对象。

docs/resource/programming/CPP/Basics/cpp-comments.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626

2727
**语法:**
2828

29-
```
30-
cpp// 单行注释
29+
```cpp
30+
// 单行注释
3131
```
3232

3333
**示例:**
3434

3535
- C++
3636

37-
```
38-
cpp// C++ 程序演示单行注释
37+
```cpp
38+
// C++ 程序演示单行注释
3939
#include <iostream>
4040
using namespace std;
4141
int main() {
@@ -57,8 +57,8 @@ GFG!
5757

5858
**语法:**
5959

60-
```
61-
cpp/*
60+
```cpp
61+
/*
6262
多行注释
6363
.
6464
.
@@ -70,8 +70,8 @@ cpp/*
7070

7171
- C++
7272

73-
```
74-
cpp// C++ 程序演示多行注释
73+
```cpp
74+
// C++ 程序演示多行注释
7575
#include <iostream>
7676
using namespace std;
7777
int main() {

docs/resource/programming/CPP/Basics/cpp-keywords.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ C++ 是一种强大的语言。在 [C++](https://www.geeksforgeeks.org/c-plus-pl
2222

2323
关键字(也称为 ***保留字***)对 [C++ 编译器](https://www.geeksforgeeks.org/compiling-with-g-plus-plus/) 有特殊的含义,并且总是以小写字母书写。关键字是语言用于特定目的的词汇,如 ***void******int******public*** 等。它不能用作变量名、函数名或任何其他标识符。总共有 95 个保留关键字。以下是一些常用 C++ 关键字的表格。
2424

25-
| ***\*C++ Keyword\**** | | | |
25+
| **C++ Keyword** | | | |
2626
| :-------------------: | ------------------------------------------------------------ | --------- | ------------------------------------------------------------ |
2727
| asm | double | new | [switch](https://www.geeksforgeeks.org/switch-statement-cc/) |
2828
| auto | else | operator | template |

docs/resource/programming/CPP/Basics/cpp-tokens.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
建议选择有效且相关的标识符名称,以编写可读性强且易于维护的程序。关键字不能作为标识符,因为它们是用于执行特定任务的保留字。在以下示例中,“first_name” 是一个标识符。
2121

22-
```
23-
cppstring first_name = "Raju";
22+
```cpp
23+
string first_name = "Raju";
2424
```
2525

2626
定义标识符名称时必须遵循以下规则:
@@ -62,8 +62,8 @@ break, try, catch, char, class, const, continue, default, delete, auto, else, fr
6262

6363
**语法:**
6464

65-
```
66-
cppconst data_type variable_name = value;
65+
```cpp
66+
const data_type variable_name = value;
6767
```
6868

6969
1. 使用‘#define’预处理指令定义常量
@@ -72,8 +72,8 @@ cppconst data_type variable_name = value;
7272

7373
**语法:**
7474

75-
```
76-
cpp// constant_Name 在程序中出现的地方都会被其值替换
75+
```cpp
76+
// constant_Name 在程序中出现的地方都会被其值替换
7777
#define constant_Name value
7878
```
7979
@@ -85,14 +85,14 @@ cpp// constant_Name 在程序中出现的地方都会被其值替换
8585
8686
**声明字符串的语法:**
8787
88-
```
89-
cppstring variable_name;
88+
```cpp
89+
string variable_name;
9090
```
9191

9292
用双引号(“”)初始化字符串对象。
9393

94-
```
95-
cppstring variable_name = "This is string";
94+
```cpp
95+
string variable_name = "This is string";
9696
```
9797

9898
## 5. 特殊符号
@@ -131,8 +131,8 @@ C++ 运算符是用于对操作数(如变量、常量或表达式)执行操
131131

132132
**语法:**
133133

134-
```
135-
cppExpression1 ? Expression2 : Expression3;
134+
```cpp
135+
Expression1 ? Expression2 : Expression3;
136136
```
137137

138138
如果 ‘Expression1’ 为 true,则执行 ‘Expression2’,否则执行 ‘Expression3’

docs/resource/programming/CPP/Basics/writing-first-c-program-hello-world-example.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ C++ 中的 Hello World 程序是一个基本程序,用于演示编码过程的
1212

1313
- C++
1414

15-
```
16-
cpp// C++ 程序显示 "Hello World"
15+
```cpp
16+
// C++ 程序显示 "Hello World"
1717
// 输入输出函数的头文件
1818
#include <iostream>
1919

docs/resource/programming/CPP/ControlStatements/c-c-if-else-statement-with-examples.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ Outside if-else block
104104

105105
## **语法**
106106

107-
```
108-
arduino复制代码if (condition)
107+
```arduino
108+
if (condition)
109109
{
110110
// 当条件为真时执行该代码块
111111
}
@@ -136,8 +136,8 @@ else
136136

137137
下面的程序演示了 C++ 中 if else 语句的使用。
138138

139-
```
140-
cpp复制代码#include <iostream>
139+
```cpp
140+
#include <iostream>
141141
using namespace std;
142142

143143
int main() {
@@ -157,7 +157,7 @@ int main() {
157157
**输出**
158158

159159
```
160-
csharp复制代码i is 20
160+
i is 20
161161
Outside if-else block
162162
```
163163

docs/resource/programming/CPP/ControlStatements/cpp-for-loop.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ for 循环的各个部分:
4545

4646
C++ 中 for 循环的语法如下:
4747

48-
```
49-
cpp复制代码for ( initialization; test condition; updation)
48+
```cpp
49+
for ( initialization; test condition; updation)
5050
{
5151
// for 循环体
5252
}
@@ -388,8 +388,8 @@ Printing vector elements:
388388

389389
- C++
390390

391-
```
392-
cpp复制代码// 演示在 for 循环中使用多个变量
391+
```cpp
392+
// 演示在 for 循环中使用多个变量
393393
#include <iostream>
394394
using namespace std;
395395

@@ -410,7 +410,7 @@ int main() {
410410
**输出**
411411

412412
```
413-
yaml复制代码iteration 1
413+
iteration 1
414414
m is: 1
415415
n is: 1
416416
iteration 2
@@ -441,8 +441,8 @@ n is: 9
441441

442442
- C++
443443

444-
```
445-
cpp复制代码// 演示无限循环的 C++ 程序
444+
```cpp
445+
// 演示无限循环的 C++ 程序
446446
#include <iostream>
447447
using namespace std;
448448

@@ -460,7 +460,7 @@ int main() {
460460
**输出**
461461

462462
```
463-
复制代码gfg
463+
gfg
464464
gfg
465465
.
466466
.
@@ -476,8 +476,8 @@ C++ 中的[**范围基**](https://www.geeksforgeeks.org/range-based-loop-c/) for
476476

477477
**语法:**
478478

479-
```
480-
cpp复制代码for ( range_declaration : range_expression ) {
479+
```cpp
480+
for ( range_declaration : range_expression ) {
481481
// 在此处编写循环语句
482482
}
483483
```
@@ -491,8 +491,8 @@ cpp复制代码for ( range_declaration : range_expression ) {
491491

492492
- C++
493493

494-
```
495-
cpp复制代码// 演示 C++ 中范围基 for 循环的程序
494+
```cpp
495+
// 演示 C++ 中范围基 for 循环的程序
496496
#include <iostream>
497497
#include <vector>
498498
using namespace std;
@@ -523,7 +523,7 @@ int main() {
523523
**输出**
524524

525525
```
526-
复制代码打印数组元素
526+
打印数组元素
527527
10 20 30 40 50
528528
打印向量元素:
529529
10 20 30 40 50

docs/resource/programming/CPP/ControlStatements/cpp-loops.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,19 +477,19 @@ This loop will run forever.
477477
...................
478478
```
479479

480-
***\*Time complexity:\**** O(infinity) as the loop will run forever.
480+
**Time complexity:** O(infinity) as the loop will run forever.
481481

482-
***\*Space complexity:\**** O(1)
482+
**Space complexity:** O(1)
483483

484-
### ***\*Using While loop:\****
484+
### **Using While loop:**
485485

486486
- C++
487487

488488
```
489489
#include <iostream>``using` `namespace` `std;` `int` `main()``{` ` ``while` `(1)`` ``cout << ``"This loop will run forever.\n"``;`` ``return` `0;``}
490490
```
491491

492-
***\*Output:\****
492+
**Output:**
493493

494494
```
495495
This loop will run forever.

0 commit comments

Comments
 (0)