From 4945f41af760e49d62f6f8f2415bb1e1ac1ec5b8 Mon Sep 17 00:00:00 2001 From: flykhan Date: Mon, 24 Jul 2023 21:05:15 +0800 Subject: [PATCH] =?UTF-8?q?day1:=20C++=E7=AE=80=E4=BB=8B,=20::=E4=BD=9C?= =?UTF-8?q?=E7=94=A8=E5=9F=9F,=20namespace=20=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4,=20using=20=E5=A3=B0=E6=98=8E,=20using=20=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=8C=87=E4=BB=A4,=20=E7=B1=BB=E5=9E=8B=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2,=20struct=20=E7=B1=BB=E5=9E=8B=E5=8A=A0=E5=BC=BA,=20b?= =?UTF-8?q?ool=20=E5=85=B3=E9=94=AE=E5=AD=97,=20=E4=B8=89=E7=9B=AE?= =?UTF-8?q?=E8=BF=90=E7=AE=97=E7=AC=A6=E5=A2=9E=E5=BC=BA,=20const=20?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day1/d1.cpp | 9 +++++++++ day1/d10.cpp | 14 +++++++++++++ day1/d11.c | 11 ++++++++++ day1/d11.cpp | 14 +++++++++++++ day1/d12.cpp | 37 ++++++++++++++++++++++++++++++++++ day1/d2.cpp | 13 ++++++++++++ day1/d3.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ day1/d4.cpp | 16 +++++++++++++++ day1/d5.cpp | 34 +++++++++++++++++++++++++++++++ day1/d6.cpp | 23 +++++++++++++++++++++ day1/d7.c | 15 ++++++++++++++ day1/d7.cpp | 16 +++++++++++++++ day1/d8.cpp | 27 +++++++++++++++++++++++++ day1/d9.cpp | 18 +++++++++++++++++ 14 files changed, 304 insertions(+) create mode 100644 day1/d1.cpp create mode 100644 day1/d10.cpp create mode 100644 day1/d11.c create mode 100644 day1/d11.cpp create mode 100644 day1/d12.cpp create mode 100644 day1/d2.cpp create mode 100644 day1/d3.cpp create mode 100644 day1/d4.cpp create mode 100644 day1/d5.cpp create mode 100644 day1/d6.cpp create mode 100644 day1/d7.c create mode 100644 day1/d7.cpp create mode 100644 day1/d8.cpp create mode 100644 day1/d9.cpp diff --git a/day1/d1.cpp b/day1/d1.cpp new file mode 100644 index 0000000..8c8ed10 --- /dev/null +++ b/day1/d1.cpp @@ -0,0 +1,9 @@ +#include + +using namespace std; + +int main() +{ + cout << "Hello, World!" << endl; + return 0; +} \ No newline at end of file diff --git a/day1/d10.cpp b/day1/d10.cpp new file mode 100644 index 0000000..bec9041 --- /dev/null +++ b/day1/d10.cpp @@ -0,0 +1,14 @@ +// bool 类型 +#include + +using namespace std; + +int main() +{ + bool flag = 2; // 非 0 即 true,0 为 false + if (flag) + cout << "true" << endl; + else + cout << "false" << endl; + return 0; +} \ No newline at end of file diff --git a/day1/d11.c b/day1/d11.c new file mode 100644 index 0000000..071f5cf --- /dev/null +++ b/day1/d11.c @@ -0,0 +1,11 @@ +// c 中三目运算表达式返回的是 变量的值 +// c++ 中三目运算表达式返回的是 变量 (对照 d11.cpp) +#include + +int main() +{ + int a = 10, b = 20; + (a > b ? a : b) = 100; // 不能这样写,因为 c 语言中三目运算表达式返回的是变量的值 + printf("a = %d, b = %d\n", a, b); + return 0; +} \ No newline at end of file diff --git a/day1/d11.cpp b/day1/d11.cpp new file mode 100644 index 0000000..33524e8 --- /dev/null +++ b/day1/d11.cpp @@ -0,0 +1,14 @@ +// c 中三目运算表达式返回的是 变量的值 (对照 d11.c) +// c++ 中三目运算表达式返回的是 变量 +#include + +using namespace std; + +int main() +{ + int a = 10, b = 20; + (a > b ? a : b) = 100; + cout << "a = " << a << endl; + cout << "b = " << b << endl; + return 0; +} \ No newline at end of file diff --git a/day1/d12.cpp b/day1/d12.cpp new file mode 100644 index 0000000..28ea163 --- /dev/null +++ b/day1/d12.cpp @@ -0,0 +1,37 @@ +// const +// c++ 中 const 在定义时不会创建内存空间,取地址时会创建内存空间 +// 或者把它定义为 extern 时,也会创建内存空间 + +#include +#include + +using namespace std; + +int main() +{ + /* + const int n = 10; // 不会创建内存空间,相当于宏定义 + printf("n addr = %p\n", &n); // 取 const 变量的地址时会创建内存空间 + return 0; + */ + + /* + // C++ 中 const 的特性:在定义时不会创建内存空间,取地址时会创建内存空间 + const int x = 10; // 在符号表中创建 x,即没有在栈中开辟空间 + int *p = (int *)&x; // 在栈中开辟空间,p 指针指向,空间的值是 x 符号表中的数据 + *p = 100; // 修改的是栈中的空间,而不是符号表中的数据 + cout << "x = " << x << endl; + cout << "*p = " << *p << endl; + */ + + // 如果 const 变量初始化为一个变量时,则会在栈中开辟空间 + int x = 20; // 在栈中开辟空间,存储常量 20 + const int X = x; // 在符号表中创建 X,并且赋值变量,此时在栈中开辟空间,存储常量 20 + int *p = (int *)&X; // p 指向了 x 的地址,但是 x 是 const,所以 p 不能修改 x 的值 + *p = 100; // 会修改栈中的空间,而不是符号表中的数据,因此 X 的值会改变 + cout << "x = " << x << endl; + cout << "X = " << X << endl; + cout << "*p = " << *p << endl; // *p = 100 + + return 0; +} \ No newline at end of file diff --git a/day1/d2.cpp b/day1/d2.cpp new file mode 100644 index 0000000..ef70a6e --- /dev/null +++ b/day1/d2.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int x = 200; +int main() +{ + int x = 100; + cout << "x = " << x << endl; + // ::x 表示全局的 x + cout << "全局的 x = " << ::x << endl; + return 0; +} \ No newline at end of file diff --git a/day1/d3.cpp b/day1/d3.cpp new file mode 100644 index 0000000..66f22a9 --- /dev/null +++ b/day1/d3.cpp @@ -0,0 +1,57 @@ +// 命名空间 +#include +using namespace std; + +namespace A +{ + // 声明变量成员,可以指定初始化 + int x = 10; +} + +namespace B +{ + int x = 20; +} + +namespace C +{ + // 命名空间的嵌套 + namespace AA + { + int x = 100; + } + namespace BB + { + int x = 200; + } +} + +namespace D +{ + int x = 1; + void show_x() // 定义函数 + { + cout << "D namespace x: " << x << endl; + } + int add(int y); // 声明函数 +} + +int D::add(int y) +{ + // 可以返回当前命名空间的所有成员 + return x + y; +} + +int main() +{ + cout << "A x = " << A::x << endl; + cout << "B x = " << B::x << endl; + cout << "C-AA x = " << C::AA::x << endl; + cout << "C-BB x = " << C::BB::x << endl; + // 调用 D 的 show_x() 函数 + D::show_x(); + // 调用 D 的 add(int) 函数 + cout << "D::add(100) = " << D::add(100) << endl; + + return 0; +} \ No newline at end of file diff --git a/day1/d4.cpp b/day1/d4.cpp new file mode 100644 index 0000000..853d5d4 --- /dev/null +++ b/day1/d4.cpp @@ -0,0 +1,16 @@ +// 无名命名空间 +// 无名命名空间的作用域为当前文件 +#include + +using namespace std; + +namespace +{ + int x = 10; +} + +int main(int argc, char const *argv[]) +{ + cout << "x = " << x << endl; // x 在当前文件中任意位置都可以访问 + return 0; +} \ No newline at end of file diff --git a/day1/d5.cpp b/day1/d5.cpp new file mode 100644 index 0000000..a6c7c76 --- /dev/null +++ b/day1/d5.cpp @@ -0,0 +1,34 @@ +// 命名空间的使用 +#include + +using namespace std; // 当前位置向下,可以访问 std 命名空间中的所有成员 + +namespace A +{ + int a = 10; +} + +namespace B +{ + int a = 20; +} + +namespace C +{ + int a = 30; +} + +int main(int argc, char const *argv[]) +{ + using namespace A; + cout << "A::a = " << a << endl; // 10 + + // using namespace B; + using B::a; // 替换了当前位置的 a + cout << "B::a = " << a << endl; // 20 + + // using namespace C; + cout << "C::a = " << C::a << endl; // 30 + + return 0; +} \ No newline at end of file diff --git a/day1/d6.cpp b/day1/d6.cpp new file mode 100644 index 0000000..44bb10b --- /dev/null +++ b/day1/d6.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +// 函数的重载,和函数名、返回值类型、参数列表有关 +// 函数名相同,参数列表不同(个数、类型、顺序),构成重载 +namespace A +{ + int add(int a, int b) { return a + b; } + float add(int a, float b) { return a * 2.0f + b; } + double add(int a, double b) { return a * 3.0 + b; } + int add(int a, int b, int c) { return a + b + c; } +} + +int main() +{ + // 目的:调用 A 命名空间中的 add 函数 + using A::add; + // 调用函数时,编译器会根据参数列表的类型,自动匹配对应的函数 + cout << "add(1, 1.5f) = " << add(1, 1.5f) << endl; + cout << "add(1, 1.5) = " << add(1, 1.5) << endl; + cout << "add(1, 3) = " << add(1, 3) << endl; + return 0; +} \ No newline at end of file diff --git a/day1/d7.c b/day1/d7.c new file mode 100644 index 0000000..42bf5bb --- /dev/null +++ b/day1/d7.c @@ -0,0 +1,15 @@ +#include + +// c 语言中可以使用无类型的函数参数 +// C++ 中不允许使用无类型的函数参数 (对照 d7.cpp) +void show(x) +{ + printf("x = %d\n", (int)x); +} + +int main() +{ + show(20); + show(15.0); + return 0; +} \ No newline at end of file diff --git a/day1/d7.cpp b/day1/d7.cpp new file mode 100644 index 0000000..eb95d6f --- /dev/null +++ b/day1/d7.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +// c 语言中可以使用无类型的函数参数 (对照 d7.c) +// C++ 中不允许使用无类型的函数参数 +void show(int x) +{ + cout << "x = " << (int)x << endl; +} + +int main() +{ + show(20); + show(15.0); + return 0; +} \ No newline at end of file diff --git a/day1/d8.cpp b/day1/d8.cpp new file mode 100644 index 0000000..1f9fd57 --- /dev/null +++ b/day1/d8.cpp @@ -0,0 +1,27 @@ +// C++ 中不同类型的变量之间赋值时,需要明确的使用强制类型转换 +#include +#include +#include + +using namespace std; + +typedef enum COLOR +{ + GREEN = 1, + RED = 5, + BLUE // 6 +} color; + +int main() +{ + int a = 129; + char b = a; // 隐式类型转换(大类型转换为小类型)(不安全) + cout << "b = " << (int)b << endl; // -127 + + // C++ 中必须使用强制类型转换,明确数据类型 + char *p = (char *)malloc(32); // malloc 返回值是 void *,需要强制类型转换 + // char *p = malloc(32); // malloc 返回值是 void *,需要强制类型转换 + strcpy(p, "hello world"); + cout << "p = " << p << endl; // p 为指针,输出的是地址的内容 + return 0; +} \ No newline at end of file diff --git a/day1/d9.cpp b/day1/d9.cpp new file mode 100644 index 0000000..d26267d --- /dev/null +++ b/day1/d9.cpp @@ -0,0 +1,18 @@ +// C++ 中使用 结构体 不需要加 struct +#include + +using namespace std; + +struct S +{ + int id; + char name[30]; +}; + +int main() +{ + // struct S s1 = {1, "张三"}; // C 语言中使用结构体需要加 struct + S s1 = {1, "张三"}; // C++ 中使用结构体不需要加 struct + cout << "s1.id = " << s1.id << "\ns1.name = " << s1.name << endl; + return 0; +} \ No newline at end of file