From 220ea31d680d29ef7287dcc31b4b4357b56c6280 Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 25 Jul 2023 20:05:25 +0800 Subject: [PATCH] =?UTF-8?q?day2:=20const=20=E5=92=8C#define=20=E7=9A=84?= =?UTF-8?q?=E5=8C=BA=E5=88=AB,=20=E5=BC=95=E7=94=A8(reference)=20=E3=80=90?= =?UTF-8?q?=E9=87=8D=E8=A6=81=E3=80=91,=20=E5=86=85=E8=81=94=E5=87=BD?= =?UTF-8?q?=E6=95=B0,=20=E5=87=BD=E6=95=B0=E7=9A=84=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E5=8F=82=E6=95=B0,=20=E5=87=BD=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E5=8D=A0=E4=BD=8D=E5=8F=82=E6=95=B0,=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E9=87=8D=E8=BD=BD=E5=92=8Cextern=20c,=20=E7=B1=BB=E4=B8=8E?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=A6=82=E5=BF=B5,=20=E9=9D=A2?= =?UTF-8?q?=E5=90=91=E5=AF=B9=E8=B1=A1=E7=A8=8B=E5=BA=8F=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ day2/ce1.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++ day2/d1.cpp | 30 +++++++++++++++++ day2/d10.cpp | 30 +++++++++++++++++ day2/d11/area.c | 6 ++++ day2/d11/area.h | 15 +++++++++ day2/d11/main.cpp | 12 +++++++ day2/d12.cpp | 26 +++++++++++++++ day2/d13.cpp | 44 +++++++++++++++++++++++++ day2/d14.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++ day2/d15.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++ day2/d2.cpp | 23 +++++++++++++ day2/d2_2.cpp | 21 ++++++++++++ day2/d3.cpp | 23 +++++++++++++ day2/d4.cpp | 28 ++++++++++++++++ day2/d5.cpp | 57 ++++++++++++++++++++++++++++++++ day2/d6.cpp | 29 +++++++++++++++++ day2/d6_2.cpp | 30 +++++++++++++++++ day2/d7.cpp | 28 ++++++++++++++++ day2/d8.cpp | 26 +++++++++++++++ day2/d9.cpp | 30 +++++++++++++++++ 21 files changed, 702 insertions(+) create mode 100644 day2/ce1.cpp create mode 100644 day2/d1.cpp create mode 100644 day2/d10.cpp create mode 100644 day2/d11/area.c create mode 100644 day2/d11/area.h create mode 100644 day2/d11/main.cpp create mode 100644 day2/d12.cpp create mode 100644 day2/d13.cpp create mode 100644 day2/d14.cpp create mode 100644 day2/d15.cpp create mode 100644 day2/d2.cpp create mode 100644 day2/d2_2.cpp create mode 100644 day2/d3.cpp create mode 100644 day2/d4.cpp create mode 100644 day2/d5.cpp create mode 100644 day2/d6.cpp create mode 100644 day2/d6_2.cpp create mode 100644 day2/d7.cpp create mode 100644 day2/d8.cpp create mode 100644 day2/d9.cpp diff --git a/README.md b/README.md index 2655caf..794b4d3 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,5 @@ # 千锋 C++阶段 #### day1: C++简介, ::作用域, namespace 命名空间, using 声明, using 编译指令, 类型转换, struct 类型加强, bool 关键字, 三目运算符增强, const 增强 + +#### day2: const 和#define 的区别, 引用(reference) 【重要】, 内联函数, 函数的默认值参数, 函数的占位参数, 函数重载和 extern "c", 类与对象的概念, 面向对象程序设计案例 diff --git a/day2/ce1.cpp b/day2/ce1.cpp new file mode 100644 index 0000000..cb5573b --- /dev/null +++ b/day2/ce1.cpp @@ -0,0 +1,78 @@ +// Classroom exercises: 课堂练习 +// ce +// 类的定义和使用 +#include +#include + +using namespace std; + +class Person +{ +public: + void init(const char *iname, int iage); + void setName(const char sname[]); + void setAge(int sage); + const char *getname(); + const int getAge(); + const void show(); + +private: + char name[32]; + int age; +}; + +void Person::init(const char *iname, int iage) +{ + strcpy(this->name, iname); + if (iage > 0 && iage < 100) + this->age = iage; + else + { + this->age = 0; + cout << "年龄超出范围,拒绝赋值!" << endl; + } +} + +void Person::setName(const char sname[]) +{ + strcpy(this->name, sname); +} + +void Person::setAge(int sage) +{ + if (sage > 0 && sage < 100) + this->age = sage; + else + { + cout << "年龄超出范围,拒绝赋值!" << endl; + } +} + +const char *Person::getname() +{ + return this->name; +} + +const int Person::getAge() +{ + return this->age; +} + +const void Person::show() +{ + cout << "name = " << this->name << ", age = " << this->age << endl; +} + +int main() +{ + Person p1; + p1.setAge(20); + p1.setName("flykhan"); + p1.show(); + + Person p2; + p2.init("hello", 0); + p2.show(); + + return 0; +} diff --git a/day2/d1.cpp b/day2/d1.cpp new file mode 100644 index 0000000..0ddc71b --- /dev/null +++ b/day2/d1.cpp @@ -0,0 +1,30 @@ +// 引用的使用 +// 引用的本质是指针常量,所以引用的空间大小和指针相同,都是 8 个字节 +// 引用的本质是指针常量,所以引用的指向不能改变,但是指向的值可以改变 +// 引用的本质是指针常量,所以引用在定义时必须初始化,且不能再引用其他变量 +// int a = 10; +// int &aa = a; // int * const aa = &a; +#include + +using namespace std; + +int main() +{ + // 定义引用时,必须给初始化值,另外,定义之后引用不能再引用其他变量 + int a = 10; + int &b = a; // 对 a 的引用 + b += 20; // 相当于 a += 20 + cout << "a: " << a << " b: " << b << endl; + + int &c = a; + cout << "a: " << a << " b: " << b << " c: " << c << endl; + + int &d = b; + cout << "a: " << a << " b: " << b << " c: " << c << " d: " << d << endl; + + int x = 100; + c = x; + cout << "c: " << c << endl; + + return 0; +} diff --git a/day2/d10.cpp b/day2/d10.cpp new file mode 100644 index 0000000..9aec061 --- /dev/null +++ b/day2/d10.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +void f(int a, int) +{ + cout << "f(int, int )" << endl; +} + +void f(double, int) +{ + cout << "f(double, int)" << endl; +} + +void f(int a) +{ + cout << "f(int)" << endl; +} + +void f(double a) +{ + cout << "f(double)" << endl; +} + +int main() +{ + f(1); + f(1.0, 2); + return 0; +} diff --git a/day2/d11/area.c b/day2/d11/area.c new file mode 100644 index 0000000..beb818a --- /dev/null +++ b/day2/d11/area.c @@ -0,0 +1,6 @@ +#include "area.h" + +double S(double r) +{ + return r * r * 3.1415926; +} \ No newline at end of file diff --git a/day2/d11/area.h b/day2/d11/area.h new file mode 100644 index 0000000..9f2db4f --- /dev/null +++ b/day2/d11/area.h @@ -0,0 +1,15 @@ +#ifndef __AREA_H__ +#define __AREA_H__ + +// 条件编译 +#if __cplusplus +extern "C" +{ +#endif + double S(double r); + +#if __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/day2/d11/main.cpp b/day2/d11/main.cpp new file mode 100644 index 0000000..4f45d42 --- /dev/null +++ b/day2/d11/main.cpp @@ -0,0 +1,12 @@ +#include +#include "area.h" + +using namespace std; + +int main() +{ + double r = 2.5; + double s = S(r); + cout << "S(2.5) = " << s << endl; + return 0; +} diff --git a/day2/d12.cpp b/day2/d12.cpp new file mode 100644 index 0000000..82c9661 --- /dev/null +++ b/day2/d12.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +// 增强的结构体(c 中结构体中只有变量) +struct Person +{ + int pid; + int age; + void show() // C++ 的结构体中可以加函数 + { + // 可以访问同一结构体内的成员变量 + cout << "pid = " << pid << ", age = " << age << endl; + } +}; + +int main() +{ + Person p1 = {1001, 21}; + p1.show(); + + Person p2 = {1002, 19}; + p2.show(); + + return 0; +} diff --git a/day2/d13.cpp b/day2/d13.cpp new file mode 100644 index 0000000..b474d39 --- /dev/null +++ b/day2/d13.cpp @@ -0,0 +1,44 @@ +// 类 +#include +#include +using namespace std; + +namespace SA +{ + class Student + { + public: + int sid = 14; // 默认值设定,需要 c++11 标准以上 + char name[32] = "hello"; + + private: + float score = 90; + + public: + void showScore(); // 声明类方法 + void addScore(float n) { this->score = n; } + }; + + // 实现类中的方法(函数) + void Student::showScore() + { + cout << "sid = " << this->sid << ", name = " << this->name << ", score = " << this->score << endl; + } +}; + +using namespace SA; + +int main() +{ + + Student s1; + strcpy(s1.name, "flykhan"); + s1.sid = 1001; + s1.addScore(100); + s1.showScore(); + + Student s2; + s2.showScore(); + + return 0; +} diff --git a/day2/d14.cpp b/day2/d14.cpp new file mode 100644 index 0000000..56a1c64 --- /dev/null +++ b/day2/d14.cpp @@ -0,0 +1,81 @@ +// 立方体类与应用 +#include + +using namespace std; + +class Cube +{ +public: + void init(int l, int w, int h) + { + this->a = l; + this->b = w; + this->c = h; + } + int S(); // 面积 + int V(); // 体积 + void show(); + bool isEqual(Cube &tmp); + +private: + int a; // 长 + int b; // 宽 + int c; // 高 +}; + +int Cube::S() +{ + return 2 * (a * b + a * c + b * c); +} + +int Cube::V() +{ + return a * b * c; +} + +void Cube::show() +{ + cout << "S = " << S() << ", V = " << V() << endl; +} + +bool Cube::isEqual(Cube &tmp) +{ + // if (this->a == tmp.a && this->b == tmp.b && this->c == tmp.c) + if (this->S() == tmp.S() && this->V() == tmp.V()) + return true; + else + return false; +} + +bool equalCube(Cube &c1, Cube &c2) +{ + if (c1.S() == c2.S() && c1.V() == c2.V()) + return true; + return false; +} + +const char *resBool(bool b) // bool 数字值转换人类可读 +{ + if (b != 0) + return "true"; + return "false"; +} + +int main() +{ + Cube c1, c2, c3; + c1.init(2, 3, 4); + cout << "c1: " << endl; + c1.show(); + + c2.init(3, 4, 5); + cout << "c2: " << endl; + c2.show(); + + c3.init(4, 2, 3); + + cout << "Cube::isEqual(c2) = " << resBool(c1.isEqual(c2)) << endl; + cout << "equalCube(c1, c2) = " << resBool(equalCube(c1, c3)) << endl; + + return 0; +} diff --git a/day2/d15.cpp b/day2/d15.cpp new file mode 100644 index 0000000..f0959a5 --- /dev/null +++ b/day2/d15.cpp @@ -0,0 +1,83 @@ +// 点和圆的关系 +#include +#include + +using namespace std; + +enum Pos_e +{ + onCircle = 4, // 点在圆上 + inCircle, // 点在圆内 + outCircle // 点在圆外 +}; + +class Point +{ +public: + int x, y; +}; + +class AdvCircle +{ +public: + int init(int x0, int y0, int r) + { + this->r = r; + o.x = x0; + o.y = y0; + } + + Pos_e at(Point &point); + +private: + int r; + Point o; // 圆心点 +}; + +Pos_e AdvCircle::at(Point &point) +{ + int distence = pow((point.x - this->o.x), 2) + pow((point.y - this->o.y), 2); + int rr = r * r; + if (distence == rr) + { + return onCircle; + } + else if (distence < rr) + { + return inCircle; + } + else + return outCircle; +} + +string posToString(Pos_e pos) +{ + switch (pos) + { + case onCircle: + return "在圆上"; + break; + case inCircle: + return "在圆内"; + break; + case outCircle: + return "在圆外"; + break; + default: + break; + } +} + +int main() +{ + AdvCircle c1; + c1.init(0, 0, 2); + + Point p1; + p1.x = 1; + p1.y = 1; + + cout << "c1.at(p1) " << posToString(c1.at(p1)) << endl; + + return 0; +} diff --git a/day2/d2.cpp b/day2/d2.cpp new file mode 100644 index 0000000..93fe286 --- /dev/null +++ b/day2/d2.cpp @@ -0,0 +1,23 @@ +// 引用相当于变量的别名 +#include + +using namespace std; + +int main() +{ + int arr[5] = {1, 2, 3, 4, 5}; + + // 给 arr 设置一个别名(引用) + // 先定义 5 个元素数组类型的别名 + typedef int ArrRef[5]; + ArrRef &a = arr; + + // 通过别名操作数组 + for (int i = 0; i < 5; i++) + { + cout << a[i] << " "; + } + cout << endl; + + return 0; +} diff --git a/day2/d2_2.cpp b/day2/d2_2.cpp new file mode 100644 index 0000000..61a958e --- /dev/null +++ b/day2/d2_2.cpp @@ -0,0 +1,21 @@ +// 数组引用的方法二 +#include + +using namespace std; + +int main() +{ + int arr[5] = {1, 2, 3, 4, 5}; + + // 直接定义 5 个元素数组类型的引用 + int(&a)[5] = arr; + + // 通过别名操作数组 + for (int i = 0; i < 5; i++) + { + cout << a[i] << " "; + } + cout << endl; + + return 0; +} diff --git a/day2/d3.cpp b/day2/d3.cpp new file mode 100644 index 0000000..7371f14 --- /dev/null +++ b/day2/d3.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +// 引用传递: 会改变实参的值 +// 值传递: 不会改变实参的值 +void cumsum(int &total, int n) // total 是引用传递,n 是值传递 +{ + total += n; +} + +int main() +{ + int total = 0; + int n = 0; + while (cin >> n) + { + cumsum(total, n); + cout << "total = " << total << endl; + if (total > 100) + break; + } +} \ No newline at end of file diff --git a/day2/d4.cpp b/day2/d4.cpp new file mode 100644 index 0000000..add5246 --- /dev/null +++ b/day2/d4.cpp @@ -0,0 +1,28 @@ +#include +#define N 10 // 定义常量 + +using namespace std; + +typedef int TenArr[N]; + +TenArr &new_arr() +{ + static int m[N] = {0}; // 全局静态区存储,程序结束后才释放 + for (int i = 0; i < N; i++) + { + m[i] = i; + } + return m; +} + +int main() +{ + TenArr &p = new_arr(); + int i = 0; + while (i < N) + { + cout << "p[" << i << "] = " << p[i++] << endl; + } + + return 0; +} diff --git a/day2/d5.cpp b/day2/d5.cpp new file mode 100644 index 0000000..8f2cda1 --- /dev/null +++ b/day2/d5.cpp @@ -0,0 +1,57 @@ +#include + +#define N 7 + +using namespace std; + +void sort(int (&arr)[N]) +{ + for (int i = 0; i <= N - 1; i++) + { + for (int j = 0; j <= N - i - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + arr[j] ^= arr[j + 1]; + arr[j + 1] ^= arr[j]; + arr[j] ^= arr[j + 1]; + // int temp = arr[j]; + // arr[j] = arr[j + 1]; + // arr[j + 1] = temp; + } + } + } +} + +void sort(int arr[], int size) +{ + for (int i = 0; i < size - 1; i++) + { + for (int j = 0; j < size - i - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } +} + +int main() +{ + int nums[] = {1, 5, 2, 0, 9, 10, 7}; + int size = sizeof(nums) / sizeof(nums[0]); + + sort(nums); + // sort(nums, size); + + for (int i = 0; i < size; i++) + { + cout << nums[i] << "\t"; + } + cout << endl; + + return 0; +} diff --git a/day2/d6.cpp b/day2/d6.cpp new file mode 100644 index 0000000..3c7afa7 --- /dev/null +++ b/day2/d6.cpp @@ -0,0 +1,29 @@ +// 指针的引用 +// 为指针取个别名,相当于'指针的指针' +#include +#include + +using namespace std; + +struct STU +{ + int sid; + float score; +}; + +void set_score(STU *&q, int sid, float score) +{ + q->sid = sid; + q->score = score; +} + +int main() +{ + STU *p = (STU *)malloc(sizeof(STU)); + + set_score(p, 1, 99); + + cout << "sid = " << p->sid << ", score = " << p->score << endl; + + return 0; +} diff --git a/day2/d6_2.cpp b/day2/d6_2.cpp new file mode 100644 index 0000000..4c83117 --- /dev/null +++ b/day2/d6_2.cpp @@ -0,0 +1,30 @@ +// 指针的引用 +// 为指针取个别名,相当于'指针的指针' +#include +#include + +using namespace std; + +struct STU +{ + int sid; + float score; +}; + +// 指针的指针 +void set_score(STU **q, int sid, float score) +{ + (*q)->sid = sid; + (*q)->score = score; +} + +int main() +{ + STU *p = (STU *)malloc(sizeof(STU)); + + set_score(&p, 2, 98); + + cout << "sid = " << p->sid << ", score = " << p->score << endl; + + return 0; +} diff --git a/day2/d7.cpp b/day2/d7.cpp new file mode 100644 index 0000000..d96b1fa --- /dev/null +++ b/day2/d7.cpp @@ -0,0 +1,28 @@ +// 常量引用 +// 相当于 常量指针常量 +// 用法: const 数据类型 &变量名 = 其他变量或常量 + +// 注意: +// 一般的引用不能赋值常量(字面量),但是 const 引用可以赋值常量 +#include + +using namespace std; + +int main() +{ + + int x = 10; + const int &x1 = x; + + // error: assignment of read - only reference ‘x1’ + x1 -= 5; // const 修饰的引用不能修改内容 + cout << "x = " << x << endl; + + const int &x2 = 50; + cout << "x2 = " << x2 << endl; + + // error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ + int &x3 = 'a'; // 常量不能赋值给非常量引用 + cout << "x3 = " << x3 << endl; + return 0; +} diff --git a/day2/d8.cpp b/day2/d8.cpp new file mode 100644 index 0000000..5d7775b --- /dev/null +++ b/day2/d8.cpp @@ -0,0 +1,26 @@ +// 内联函数 +// 函数前面添加了 inline 关键字,则此函数为内联函数 +// 内联函数只能在当前文件中使用,相当于函数前面加 static + +// 内联函数一般用于替换 有参的宏,有参宏经常会出错,而且参数是无数据类型 +// 每一次使用内联函数时,都会像有参宏一样,展开一次(内联函数不入栈,运行效率高,编译时会提前处理) +#include + +using namespace std; + +#define ADD(x, y) x + y +// 内联函数 +inline int add(int a, int b) +{ + return a + b; +} + +int main() +{ + int ret1 = ADD(10, 20) * 10; + cout << "ret = " << ret1 << endl; + + int ret2 = add(10, 20) * 10; + cout << "ret = " << ret2 << endl; + return 0; +} diff --git a/day2/d9.cpp b/day2/d9.cpp new file mode 100644 index 0000000..a5d286f --- /dev/null +++ b/day2/d9.cpp @@ -0,0 +1,30 @@ +// 函数的默认参数 + +#include + +using namespace std; + +bool isSs(int n = 10) // 判断 n 是否为质数 +{ + int i = 2; + for (; i < n / 2; i++) + { + if (n % i == 0) + return false; // 0 为假 + } + return true; // 非 0 为真 +} + +double area(double r = 1.0) // 求面积 +{ + return 3.14 * r * r; +} + +int main() +{ + cout << "isSS(7) is " << isSs(7) << endl; + cout << "isSS() is " << isSs() << endl; + cout << "area() is " << area() << endl; + cout << "area(2) is " << area(2) << endl; + return 0; +}