# cpp_code **Repository Path**: XiuxinCode/cpp_code ## Basic Information - **Project Name**: cpp_code - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-14 - **Last Updated**: 2026-03-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Modern C++ (C++17/20) 教学演示项目 [![C++20](https://img.shields.io/badge/C%2B%2B-20-blue.svg)](https://en.cppreference.com/w/cpp/20) [![CMake](https://img.shields.io/badge/CMake-3.20+-green.svg)](https://cmake.org/) 这是一个基于 **CLion + CMake** 的 Modern C++ 教学演示项目,按照 C++ 基础语法模块进行拆分,便于学习和查找。 ## 📁 项目目录结构 ``` . ├── CMakeLists.txt # CMake 构建配置文件 ├── README.md # 本文件 ├── .gitignore # Git 忽略规则 ├── docs/ # 文档目录 │ ├── task1.md # 现代 C++ 最佳实践清单 │ └── task2.md # C++ 基础语法说明 ├── include/ # 头文件目录(按模块拆分) │ ├── 01_BasicSyntax.h # 基础语法:变量、类型、运算符 │ ├── 02_IO.h # 输入输出 │ ├── 03_ControlFlow.h # 控制结构 │ ├── 04_Functions.h # 函数 │ ├── 05_ArraysAndStrings.h # 数组与字符串 │ ├── 06_PointersAndReferences.h # 指针与引用 │ ├── 07_ClassesAndObjects.h # 类与对象 │ ├── 08_Namespaces.h # 命名空间 │ ├── 09_ExceptionHandling.h # 异常处理 │ └── 10_FileIO.h # 文件操作 └── src/ # 源代码目录(与头文件对应) ├── main.cpp # 程序入口,提供交互菜单 ├── 01_BasicSyntax.cpp ├── 02_IO.cpp ├── 03_ControlFlow.cpp ├── 04_Functions.cpp ├── 05_ArraysAndStrings.cpp ├── 06_PointersAndReferences.cpp ├── 07_ClassesAndObjects.cpp ├── 08_Namespaces.cpp ├── 09_ExceptionHandling.cpp └── 10_FileIO.cpp ``` ## 🚀 快速开始 ### 环境要求 - **编译器**:支持 C++20 的编译器(MSVC 2019+ / GCC 10+ / Clang 12+) - **构建工具**:CMake 3.20 或更高版本 - **IDE**:CLion(推荐)或 Visual Studio 2019+ ### 构建项目 #### 使用 CLion(推荐) 1. 打开 CLion 2. 选择 `File` → `Open`,选择项目根目录 3. CLion 会自动加载 CMake 配置 4. 点击运行按钮即可构建并运行 #### 使用命令行 ```bash # 创建构建目录 mkdir build cd build # 生成构建文件 cmake .. # 构建项目 cmake --build . # 运行程序 ./ModernCppDemo ``` ## 📚 模块说明 | 编号 | 模块 | 对应 task2.md | 核心内容 | |:----:|------|---------------|----------| | 01 | **基础语法** | 三、变量与数据类型
五、运算符 | 变量类型、`auto`、`constexpr`、各类运算符 | | 02 | **输入输出** | 四、输入与输出 | `cin`/`cout`、格式化输出 | | 03 | **控制结构** | 六、控制结构 | `if-else`、`switch`、`for`、`while`、`do-while` | | 04 | **函数** | 七、函数 | 函数定义、重载、默认参数、参数传递 | | 05 | **数组与字符串** | 八、数组与字符串 | C数组、`std::array`、`std::vector`、C字符串、`std::string` | | 06 | **指针与引用** | 九、指针与引用 | 指针运算、引用、指针vs引用、`const`指针 | | 07 | **类与对象** | 十、结构与类 | 类定义、构造函数、封装、继承、多态 | | 08 | **命名空间** | 十一、命名空间 | `namespace`、`using`、嵌套命名空间 | | 09 | **异常处理** | 十三、错误处理 | `try-catch`、自定义异常、`noexcept` | | 10 | **文件操作** | 十二、`` | 文本文件、二进制文件、文件位置操作 | ## 🎮 使用方式 运行程序后会显示交互菜单: ``` ======================================== Modern C++ 教学演示项目 ======================================== 请选择要运行的演示模块: 0. 运行所有演示 1. 基础语法 (变量、类型、运算符) 2. 输入输出 (iostream) 3. 控制结构 (if/switch/for/while) 4. 函数 (定义、重载、参数传递) 5. 数组与字符串 6. 指针与引用 7. 类与对象 (面向对象) 8. 命名空间 9. 异常处理 10. 文件操作 q. 退出 请输入选项 (0-10, q): ``` ## 📖 各模块详细内容 ### 01. 基础语法 (`01_BasicSyntax`) ```cpp // 变量与类型 int age = 18; float pi = 3.14f; auto x = 42; // 类型推导 constexpr int N = 100; // 编译期常量 // 运算符 // 算术: + - * / % // 关系: == != > < >= <= // 逻辑: && || ! // 赋值: = += -= *= /= ``` ### 02. 输入输出 (`02_IO`) ```cpp // 基础 I/O std::cin >> x; std::cout << "value: " << x << std::endl; // 格式化输出 std::cout << std::setw(10) << std::setprecision(2) << pi; ``` ### 03. 控制结构 (`03_ControlFlow`) ```cpp // if-else if (condition) { ... } else { ... } // switch switch (value) { case 1: ...; break; default: ...; } // 循环 for (int i = 0; i < n; i++) { ... } while (condition) { ... } do { ... } while (condition); // 范围 for (C++11) for (const auto& item : container) { ... } ``` ### 04. 函数 (`04_Functions`) ```cpp // 函数定义 int add(int a, int b) { return a + b; } // 函数重载 double add(double a, double b) { return a + b; } // 默认参数 void greet(const std::string& name = "Guest"); // 参数传递 void byValue(int x); // 值传递 void byRef(int& x); // 引用传递 void byPointer(int* x); // 指针传递 ``` ### 05. 数组与字符串 (`05_ArraysAndStrings`) ```cpp // C 风格数组 int arr[5] = {1, 2, 3, 4, 5}; // std::array (C++11) std::array arr = {1, 2, 3, 4, 5}; // std::vector (动态数组) std::vector vec = {1, 2, 3}; vec.push_back(4); // C 风格字符串 char str[] = "hello"; // std::string std::string s = "hello"; s += " world"; ``` ### 06. 指针与引用 (`06_PointersAndReferences`) ```cpp int x = 10; int* p = &x; // 指针 int& r = x; // 引用 *p = 20; // 通过指针修改 r = 30; // 通过引用修改 // const 指针 const int* p1; // 指向常量的指针 int* const p2; // 常量指针 const int* const p3; // 指向常量的常量指针 ``` ### 07. 类与对象 (`07_ClassesAndObjects`) ```cpp class Person { public: Person(const std::string& name, int age); void sayHello() const; private: std::string name_; int age_; }; // 继承 class Student : public Person { // ... }; ``` ### 08. 命名空间 (`08_Namespaces`) ```cpp namespace math { constexpr double PI = 3.14159; int add(int a, int b); } // 使用 int result = math::add(1, 2); using namespace math; ``` ### 09. 异常处理 (`09_ExceptionHandling`) ```cpp try { // 可能抛出异常的代码 throw std::runtime_error("error"); } catch (const std::exception& e) { // 处理异常 std::cerr << e.what() << std::endl; } ``` ### 10. 文件操作 (`10_FileIO`) ```cpp // 写入 std::ofstream out("file.txt"); out << "Hello" << std::endl; // 读取 std::ifstream in("file.txt"); std::string line; std::getline(in, line); ``` ## 🎯 学习建议 1. **按顺序学习**:建议按照编号顺序 01→10 学习 2. **对比阅读**:每个模块的头文件(`.h`)和实现文件(`.cpp`)对照阅读 3. **动手实验**:修改代码,观察输出变化 4. **参考文档**:结合 `docs/task2.md` 中的语法说明 ## 📝 代码规范 - 使用 **花括号初始化** `{}` - 优先使用 `std::array` 和 `std::vector` 替代 C 风格数组 - 使用 `std::string` 替代 C 风格字符串 - 使用智能指针管理动态内存(本项目暂未涉及,参见 task1.md) ## 📄 许可证 本项目使用 MIT 许可证。 --- **Happy Coding! 🎉**