C与C++区别
|
C |
C++ |
| 语法 |
过程化的编程语言 |
具有面向对象范式的编程语言 |
| 编程风格 |
注重过程化的编程风格,主要关注算法和函数的设计 |
支持面向对象的编程风格,可以使用类、对象和封装等概念进行程序设计 |
| 标准库 |
C标准库相对简单,提供了基本的输入输出、字符串处理等功能 |
C++标准库比C标准库更加丰富和功能强大,包含了大量的容器、算法和其他常用的功能模块 |
| 异常处理 |
没有内置的异常处理机制,通常使用返回错误码的方式来处理错误 |
C++支持异常处理机制,可以捕获和处理程序中的异常情况 |
| 兼容性 |
C++是C的超集,也就是说,任何一个符合C语法规则的程序也是一个合法的C++程序。C++可以调用C语言编写的函数,并且可以与C语言的代码共享。 |
|
| 选择 |
C++相对于C来说更加强大和灵活,适用于更复杂的应用场景。但对于一些简单的、性能要求较高的程序,C语言可能更加适合。选择使用哪种语言,取决于具体的需求和编程的目标。 |
|
“helloworld”的编写
步骤:

.vscode文件夹
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
| { "version": "0.2.0", "configurations": [ { "name": "C/C++ Runner: Debug Session", "type": "cppdbg", "request": "launch", "args": [], "stopAtEntry": false, "externalConsole": false, "cwd": "${fileDirname}", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "MIMode": "gdb", "miDebuggerPath": "D:/mingw64/bin/gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
|
settings.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
| { "files.associations": { "iostream": "cpp", "ostream": "cpp", "array": "cpp", "atomic": "cpp", "*.tcc": "cpp", "deque": "cpp", "string": "cpp", "vector": "cpp", "algorithm": "cpp", "memory_resource": "cpp", "string_view": "cpp", "memory": "cpp", "random": "cpp", "fstream": "cpp", "initializer_list": "cpp", "sstream": "cpp", "stdexcept": "cpp", "tuple": "cpp", "type_traits": "cpp", "utility": "cpp", "cstdlib": "cpp", "exception": "cpp", "cmath": "cpp", "functional": "cpp", "istream": "cpp", "new": "cpp", "typeinfo": "cpp", "list": "cpp", "unordered_map": "cpp", "numeric": "cpp", "streambuf": "cpp", "system_error": "cpp", "iosfwd": "cpp", "cctype": "cpp", "clocale": "cpp", "cstdarg": "cpp", "cstddef": "cpp", "cstdint": "cpp", "cstdio": "cpp", "cstring": "cpp", "ctime": "cpp", "cwchar": "cpp", "cwctype": "cpp", "iterator": "cpp", "map": "cpp", "optional": "cpp", "set": "cpp", "iomanip": "cpp", "limits": "cpp" }, "C_Cpp.errorSquiggles": "disabled"
}
|
tasks.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
| { "version": "2.0.0", "tasks": [ { "label": "C/C++: g++.exe build active file", "type": "process", "command": "D:/mingw64/bin/g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:/mingw64/bin/" }, "group": "build", "detail": "compiler: D:/mingw64/bin/g++.exe", "problemMatcher": { "owner": "cpp", "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } } ] }
|
1 2 3 4 5 6 7
| #include <iostream>
int main() { std::cout << "hello world!" << std::endl; return 0; }
|
命名空间
C++中的命名空间是用来避免命名冲突的一种机制。当在一个程序中使用了不同的库或者代码模块,可能会存在相同的函数、类、变量等命名,这时候可以使用命名空间来区分它们。
在C++中,使用关键字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
| #include <iostream>
namespace A { void func() { std::cout << "This is function A" << std::endl; } }
namespace B { void func() { std::cout << "This is function B" << std::endl; } } int main() { A::func(); B::func(); return 0; }
|
通过使用命名空间,我们可以将具有相同名称的函数或者其他实体进行区分,避免了命名冲突的问题。
在C++中,std 是C++标准库(Standard Library)中的命名空间(Namespace)。
C++标准库中的函数、类、变量等被放在了 std 命名空间中,通过 std:: 前缀来访问这些内容。
语法符号
注释
与C一样
关键字
| auto |
false |
static |
using |
if |
goto |
static_cast |
| bool |
true |
public |
namespace |
else |
and |
const_cast |
| char |
enum |
protected |
inline |
for |
not |
dynamic_cast |
| int |
union |
private |
new |
while |
or |
reinterpret_cast |
| short |
struct |
virtual |
delete |
do |
xor |
static_assert |
| long |
class |
override |
this |
switch |
return |
register |
| float |
wchar_t |
final |
nullptr |
case |
try |
explicit |
| double |
sizeof |
operator |
void |
default |
catch |
extern |
| signed |
typeid |
const |
friend |
break |
throw |
|
| unsigned |
typedef |
constexpr |
template |
continue |
noexcept |
|
标识符
与C一样
1 2 3 4
| int myVar; void myFun(); class MyClass{}; MyClass myObject;
|
数据类型
与C基本一致
布尔类型
作用:布尔数据类型代表真或假的值
bool类型只有两个值:
true — 真(本质是1)
false — 假(本质是0)
bool类型占==1个字节==大小
复合数据类型:
引用:为对象或变量起一个别名
类:用于封装数据和方法的用户自定义数据类型
常量与变量
与C一样
运算符
与C基本一致
输入输出
| C |
C++ |
| scanf和printf函数进行输入输出 |
使用cin和cout对象进行输入输出 |
| 使用%d、%f等格式化字符来表示不同类型的数据 |
使用>>和<<运算符来输入输出不同类型的数据 |
| 布尔运算符的真值为0以外的所有值,结果为1(真)或0(假) |
布尔运算符的真值只能是true或false,结果为1或0 |
流程控制语句
与C一样
函数
与C基本一致
函数的重载
函数重载(overload)是指在同一个作用域中可以定义多个函数,这些函数的名称相同但是参数列表不同。通过参数列表的不同,编译器可以区分出调用哪个函数。
在C语言中,是通过函数名字区分函数的
在C++中,是通过函数签名(函数名,形参列表)
函数重载的规则如下:
1、函数名称必须相同;
2、参数列表不同(参数个数、顺序、类型);
3、与返回值无关。
内联函数
内联函数是指在函数声明前加上关键字”inline”的函数
作用:告诉编译器将该函数的定义插入到调用它的地方,而不是通过函数调用的方式执行
1 2 3 4 5 6 7 8 9
| inline int add(int a, int b) { return a + b; } int main() { int result = add(3, 4); return 0; }
|
优点:可以减少函数调用的开销。
适用场景:函数体较短、频繁调用的情况
结构体、指针、数组
C++中的结构体
与C语言中的结构体类似,但是可以写函数
1 2 3 4 5 6 7 8 9 10 11
| struct Student{ string name; int age; double score; void study() { cout << name << " is studying." << endl; } };
|
C++中的指针
与C一样
C++中的数组
与C一样
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 69
| #include <iostream> #include <string.h> using namespace std; struct Student{ string name; int age; double score;
void study() { cout << name << "在学习" << endl; } }; void inputStudent(Student *stu,int size) { for (int i = 0; i < size; i++) { cout << "请输入第" << i+1 <<"个学生姓名" << endl; cin >> (stu+i)->name; cout << "请输入第" << i+1 <<"个学生年龄" << endl; cin >> (stu+i)->age; cout << "请输入第" << i+1 <<"个学生成绩" << endl; cin >> (stu+i)->score; } return; }
void sortByScore(Student* stu,int size) { Student temp{}; for (int i = 0; i < size; i++) { for (int k = 0; k < size - i -1; k++) { if (stu[k].score > stu[k+1].score) { temp = stu[k]; stu[k] = stu[k+1]; stu[k+1] = temp; } } } return; }
void printfStudentAll(Student* stu,int size) { for (int i = 0; i < size; i++) { cout << "请输入第" << i+1 << "个学生姓名" << (stu+i)->name << "学生年龄" << (stu+i)->age << "学生成绩" << (stu+i)->score << endl; } return; } int main() { Student std[3]; int lenth = sizeof(std)/sizeof(std[0]); cout << lenth << endl; inputStudent(std,lenth); printfStudentAll(std,lenth); for (int i = 0; i < lenth; i++) { (std+i)->study(); } sortByScore(std,lenth); printfStudentAll(std,lenth); return 0; }
|