课程代码仓库初始化:Part1 (144) + Part3 (29) 全量生成与验证

把川大 wiki「02.Qt方向」课程代码整理为 CMake 管理的可编译运行项目。

Part1 C/C++ 强化 (p01/, 144 目标, host gcc/g++ 构建, 全绿):
- 修复全部编译失败:补 <cstring>/<fstream>/<iomanip>/<vector> 等缺失头
  (common_fix + infer_stl_includes 自动检测注入)、修正 pause() 壳注入
  (改检测已转换后的 pause 调用)、namespace 提升出函数体、const 成员函数
  正确性、dedupe 不再破坏函数重载集、K&R 隐式 int / 动态异常规范等
  C++17 移除特性的可编译等价改写。
- 目录采用深缩写 p01/ch04/s03/ (原 part1_cpp/ch04_class_object/03_ctor_dtor)。

Part3 Qt 桌面 (p03/, 29 目标, 隔离 Qt 5.14.2 构建, 全绿):
- gen_part3.py: Qt 外壳模板 (main+QApplication+show, QTimer 自动退出便于
  offscreen 验证)、AUTOMOC + .moc include、rewrite_* 处理散文混入/重载/
  资源依赖、生成占位 png/gif + .qrc + rundata 样例数据。
- 修正 QLable 笔误、全角分号、.→->、Windows 路径、QApplication 阻塞事件
  循环改为 QCoreApplication 等。

任务1 概念/版本验证:
- tools/std_probe.py: 跨 -std= 编译探针 (--pedantic-errors 让已废除特性硬失败)。
- tools/demo_versions/: K&R 隐式 int、三目左值、void* 转换、enum 赋整、
  const 经指针、动态异常规范 共 6 个跨版本演示。
- docs/VERSION_NOTES.md: 每条「原文说法→C17/C++17 是否成立→何版本成立→已验证」。

文档: 顶层 README、docs/SOURCE_PAGES.md (页面→目录映射)、
docs/ERRATA.md (Part1 修正)、docs/ERRATA_part3.md (Part3 修正)。
另修复 tools/run.sh / run_qt.sh 的 ${1:?...} 引号语法 bug。

隔离 Qt 验证: ldd 0 系统 Qt 引用; 173/173 目标全绿; p03 offscreen 运行通过。
This commit is contained in:
张宗平
2026-06-30 14:16:55 +08:00
commit 13b3ebbe14
287 changed files with 16532 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
# Auto-generated by tools/gen_part1.py
add_subdirectory(ch02)
add_subdirectory(ch03)
add_subdirectory(ch04/s01)
add_subdirectory(ch04/s02)
add_subdirectory(ch04/s03)
add_subdirectory(ch04/s04)
add_subdirectory(ch04/s05)
add_subdirectory(ch04/s06)
add_subdirectory(ch04/s07)
add_subdirectory(ch04/s08)
add_subdirectory(ch05)
add_subdirectory(ch06)
add_subdirectory(ch07)
add_subdirectory(ch08)
add_subdirectory(ch09/s02)
add_subdirectory(ch09/s03)
add_subdirectory(ch09/s04)
+7
View File
@@ -0,0 +1,7 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch02
add_executable(p1c02_01 hello_world.cpp)
set_target_properties(p1c02_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c02_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c02_01 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch02 DEPENDS p1c02_01)
+21
View File
@@ -0,0 +1,21 @@
// ============================================================================
// p1c02_01 — 2.1.1 c++ hello world
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954438:0] 2.1.1 c++ hello world ---
#include<iostream>
using namespace std;
int main(){
cout << "hello world" << endl;
return EXIT_SUCCESS;
}
// --- main ---
+181
View File
@@ -0,0 +1,181 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch03
add_executable(p1c03_mymodule extern_c_module_main.c)
set_target_properties(p1c03_mymodule PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_compile_options(p1c03_mymodule PRIVATE -Wall -Wextra)
add_executable(p1c03_01 scope_resolution_operator.cpp)
set_target_properties(p1c03_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_01 PRIVATE -Wall -Wextra)
add_executable(p1c03_02 scope_resolution_operator_2.cpp)
set_target_properties(p1c03_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_02 PRIVATE -Wall -Wextra)
add_executable(p1c03_03 namespace_syntax.cpp)
set_target_properties(p1c03_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_03 PRIVATE -Wall -Wextra)
add_executable(p1c03_04 namespace_syntax_2.cpp)
set_target_properties(p1c03_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_04 PRIVATE -Wall -Wextra)
add_executable(p1c03_05 namespace_syntax_3.cpp)
set_target_properties(p1c03_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_05 PRIVATE -Wall -Wextra)
add_executable(p1c03_06 namespace_syntax_4.cpp)
set_target_properties(p1c03_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_06 PRIVATE -Wall -Wextra)
add_executable(p1c03_07 namespace_syntax_5.cpp)
set_target_properties(p1c03_07 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_07 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_07 PRIVATE -Wall -Wextra)
add_executable(p1c03_08 namespace_syntax_6.cpp)
set_target_properties(p1c03_08 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_08 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_08 PRIVATE -Wall -Wextra)
add_executable(p1c03_09 namespace_syntax_7.cpp)
set_target_properties(p1c03_09 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_09 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_09 PRIVATE -Wall -Wextra)
add_executable(p1c03_10 namespace_syntax_8.cpp)
set_target_properties(p1c03_10 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_10 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_10 PRIVATE -Wall -Wextra)
add_executable(p1c03_11 using_declaration.cpp)
set_target_properties(p1c03_11 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_11 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_11 PRIVATE -Wall -Wextra)
add_executable(p1c03_12 using_declaration_2.cpp)
set_target_properties(p1c03_12 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_12 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_12 PRIVATE -Wall -Wextra)
add_executable(p1c03_13 using_directive.cpp)
set_target_properties(p1c03_13 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_13 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_13 PRIVATE -Wall -Wextra)
add_executable(p1c03_14 global_variable_detection.c)
set_target_properties(p1c03_14 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_compile_options(p1c03_14 PRIVATE -Wall -Wextra)
add_executable(p1c03_15 strict_function_signatures.c)
set_target_properties(p1c03_15 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_compile_options(p1c03_15 PRIVATE -Wall -Wextra)
add_executable(p1c03_16 stricter_type_casting.c)
set_target_properties(p1c03_16 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_compile_options(p1c03_16 PRIVATE -Wall -Wextra)
add_executable(p1c03_17 struct_enhancement.cpp)
set_target_properties(p1c03_17 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_17 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_17 PRIVATE -Wall -Wextra)
add_executable(p1c03_18 bool_keyword.cpp)
set_target_properties(p1c03_18 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_18 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_18 PRIVATE -Wall -Wextra)
add_executable(p1c03_19 ternary_operator.c)
set_target_properties(p1c03_19 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_compile_options(p1c03_19 PRIVATE -Wall -Wextra)
add_executable(p1c03_20 ternary_operator.cpp)
set_target_properties(p1c03_20 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_20 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_20 PRIVATE -Wall -Wextra)
add_executable(p1c03_21 const_diff_summary.cpp)
set_target_properties(p1c03_21 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_21 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_21 PRIVATE -Wall -Wextra)
add_executable(p1c03_22 const_diff_summary.c)
set_target_properties(p1c03_22 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_compile_options(p1c03_22 PRIVATE -Wall -Wextra)
add_executable(p1c03_23 const_diff_summary_2.cpp)
set_target_properties(p1c03_23 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_23 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_23 PRIVATE -Wall -Wextra)
add_executable(p1c03_24 const_diff_summary_3.cpp)
set_target_properties(p1c03_24 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_24 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_24 PRIVATE -Wall -Wextra)
add_executable(p1c03_25 const_diff_summary_4.cpp)
set_target_properties(p1c03_25 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_25 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_25 PRIVATE -Wall -Wextra)
add_executable(p1c03_26 const_vs_define.cpp)
set_target_properties(p1c03_26 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_26 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_26 PRIVATE -Wall -Wextra)
add_executable(p1c03_27 const_vs_define_2.cpp)
set_target_properties(p1c03_27 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_27 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_27 PRIVATE -Wall -Wextra)
add_executable(p1c03_28 const_vs_define_3.cpp)
set_target_properties(p1c03_28 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_28 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_28 PRIVATE -Wall -Wextra)
add_executable(p1c03_29 reference_basics.cpp)
set_target_properties(p1c03_29 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_29 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_29 PRIVATE -Wall -Wextra)
add_executable(p1c03_30 reference_basics_2.cpp)
set_target_properties(p1c03_30 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_30 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_30 PRIVATE -Wall -Wextra)
add_executable(p1c03_31 reference_in_function.cpp)
set_target_properties(p1c03_31 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_31 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_31 PRIVATE -Wall -Wextra)
add_executable(p1c03_32 reference_in_function_2.cpp)
set_target_properties(p1c03_32 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_32 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_32 PRIVATE -Wall -Wextra)
add_executable(p1c03_33 reference_essence.cpp)
set_target_properties(p1c03_33 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_33 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_33 PRIVATE -Wall -Wextra)
add_executable(p1c03_34 pointer_reference.cpp)
set_target_properties(p1c03_34 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_34 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_34 PRIVATE -Wall -Wextra)
add_executable(p1c03_35 const_reference.cpp)
set_target_properties(p1c03_35 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_35 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_35 PRIVATE -Wall -Wextra)
add_executable(p1c03_36 const_reference_2.cpp)
set_target_properties(p1c03_36 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_36 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_36 PRIVATE -Wall -Wextra)
add_executable(p1c03_37 macro_pitfalls.cpp)
set_target_properties(p1c03_37 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_37 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_37 PRIVATE -Wall -Wextra)
add_executable(p1c03_38 macro_pitfalls_2.cpp)
set_target_properties(p1c03_38 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_38 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_38 PRIVATE -Wall -Wextra)
add_executable(p1c03_39 inline_in_class.cpp)
set_target_properties(p1c03_39 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_39 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_39 PRIVATE -Wall -Wextra)
add_executable(p1c03_40 default_arguments.cpp)
set_target_properties(p1c03_40 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_40 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_40 PRIVATE -Wall -Wextra)
add_executable(p1c03_41 placeholder_parameters.cpp)
set_target_properties(p1c03_41 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_41 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_41 PRIVATE -Wall -Wextra)
add_executable(p1c03_42 function_overload_basics.cpp)
set_target_properties(p1c03_42 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_42 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_42 PRIVATE -Wall -Wextra)
add_executable(p1c03_43 function_overload_basics_2.cpp)
set_target_properties(p1c03_43 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_43 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_43 PRIVATE -Wall -Wextra)
add_executable(p1c03_44 function_overload_mechanism.cpp)
set_target_properties(p1c03_44 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_44 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_44 PRIVATE -Wall -Wextra)
add_executable(p1c03_45 extern_c.cpp)
set_target_properties(p1c03_45 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c03_45 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c03_45 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch03 DEPENDS p1c03_mymodule p1c03_01 p1c03_02 p1c03_03 p1c03_04 p1c03_05 p1c03_06 p1c03_07 p1c03_08 p1c03_09 p1c03_10 p1c03_11 p1c03_12 p1c03_13 p1c03_14 p1c03_15 p1c03_16 p1c03_17 p1c03_18 p1c03_19 p1c03_20 p1c03_21 p1c03_22 p1c03_23 p1c03_24 p1c03_25 p1c03_26 p1c03_27 p1c03_28 p1c03_29 p1c03_30 p1c03_31 p1c03_32 p1c03_33 p1c03_34 p1c03_35 p1c03_36 p1c03_37 p1c03_38 p1c03_39 p1c03_40 p1c03_41 p1c03_42 p1c03_43 p1c03_44 p1c03_45)
+23
View File
@@ -0,0 +1,23 @@
// ============================================================================
// p1c03_18 — 3.7 “新增”bool类型关键字
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:17] 3.7 “新增”bool类型关键字 ---
void test()
{ cout << sizeof(false) << endl; //为1,//bool类型占一个字节大小
bool flag = true; // c语言中没有这种类型
flag = 100; //给bool类型赋值时,非0值会自动转换为true(1),0值会自动转换false(0)
}
// --- main ---
int main() {
test();
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
// ============================================================================
// p1c03_22 — 3.8.2.3 C/C++中const异同总结
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// --- 原文片段 [58954439:21] 3.8.2.3 C/C++中const异同总结 ---
static void block_0() {
const int constA = 10;
int* p = (int*)&constA;
*p = 300;
printf("constA:%d\n",constA);
printf("*p:%d\n", *p);
}
// --- main ---
int main() {
block_0();
return 0;
}
+19
View File
@@ -0,0 +1,19 @@
// ============================================================================
// p1c03_21 — 3.8.2.3 C/C++中const异同总结
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:20] 3.8.2.3 C/C++中const异同总结 ---
const int constA = 10;
int main(){
int* p = (int*)&constA;
*p = 200;
}
// --- main ---
+25
View File
@@ -0,0 +1,25 @@
// ============================================================================
// p1c03_23 — 3.8.2.3 C/C++中const异同总结
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:22] 3.8.2.3 C/C++中const异同总结 ---
static void block_0() {
const int constA = 10;
int* p = (int*)&constA;
*p = 300;
cout << "constA:" << constA << endl;
cout << "*p:" << *p << endl;
}
// --- main ---
int main() {
block_0();
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
// ============================================================================
// p1c03_24 — 3.8.2.3 C/C++中const异同总结
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:23] 3.8.2.3 C/C++中const异同总结 ---
static void block_0() {
int b = 10;
const int constA = b;
int* p = (int*)&constA;
*p = 300;
cout << "constA:" << constA << endl;
cout << "*p:" << *p << endl;
}
// --- main ---
int main() {
block_0();
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
// ============================================================================
// p1c03_25 — 3.8.2.3 C/C++中const异同总结
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:24] 3.8.2.3 C/C++中const异同总结 ---
static void block_0() {
class Person { public: int age = 0; };
const Person person; //未初始化age
//person.age = 50; //不可修改
Person* pPerson = (Person*)&person;
//指针间接修改
pPerson->age = 100;
cout << "pPerson->age:" << pPerson->age << endl;
pPerson->age = 200;
cout << "pPerson->age:" << pPerson->age << endl;
}
// --- main ---
int main() {
block_0();
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// ============================================================================
// p1c03_35 — 3.10.5 常量引用
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:34] 3.10.5 常量引用 ---
void test01(){
int a = 100;
const int& aRef = a; //此时aRef就是a
//aRef = 200; 不能通过aRef的值
a = 100; //OK
cout << "a:" << a << endl;
cout << "aRef:" << aRef << endl;
}
void test02(){
//不能把一个字面量赋给引用
//int& ref = 100;
//但是可以把一个字面量赋给常引用
const int& ref = 100; //int temp = 200; const int& ret = temp;
}
// --- main ---
int main() {
test01();
test02();
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
// ============================================================================
// p1c03_36 — 3.10.5 常量引用
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:35] 3.10.5 常量引用 ---
//const int& param防止函数中意外修改数据
void ShowVal(const int& param){
cout << "param:" << param << endl;
}
// --- main ---
int main() {
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
// ============================================================================
// p1c03_26 — 3.9.3 尽量以const替换#define
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:25] 3.9.3 尽量以const替换#define ---
#define PARAM 128
const short param = 128;
void func_1(short a){
cout << "short!" << endl;
}
void func_1(int a){
cout << "int" << endl;
}
// --- main ---
int main() {
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
// ============================================================================
// p1c03_27 — 3.9.3 尽量以const替换#define
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:26] 3.9.3 尽量以const替换#define ---
void func1(){
const int a = 10;
#define A 20
//#undef A //卸载宏常量A
}
void func2(){
//cout << "a:" << a << endl; //不可访问,超出了const int a作用域
cout << "A:" << A << endl; //#define作用域从定义到文件结束或者到#undef,可访问
}
int main(){
func2();
return EXIT_SUCCESS;
}
// --- main ---
+27
View File
@@ -0,0 +1,27 @@
// ============================================================================
// p1c03_28 — 3.9.3 尽量以const替换#define
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:27] 3.9.3 尽量以const替换#define ---
namespace MySpace{
#define num 1024
}
void test(){
//cout << MySpace::NUM << endl; //错误
//int num = 100; //命名冲突
cout << num << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+34
View File
@@ -0,0 +1,34 @@
// ============================================================================
// p1c03_40 — 3.12 函数的默认参数
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:39] 3.12 函数的默认参数 ---
void TestFunc01(int a = 10, int b = 20){
cout << "a + b = " << a + b << endl;
}
//注意点:
//1. 形参b设置默认参数值,那么后面位置的形参c也需要设置默认参数
void TestFunc02(int a,int b = 10,int c = 10){}
//2. 如果函数声明和函数定义分开,函数声明设置了默认参数,函数定义不能再设置默认参数
void TestFunc03(int a = 0,int b = 0);
void TestFunc03(int a, int b){}
int main(){
//1.如果没有传参数,那么使用默认参数
TestFunc01();
//2. 如果传一个参数,那么第二个参数使用默认参数
TestFunc01(100);
//3. 如果传入两个参数,那么两个参数都使用我们传入的参数
TestFunc01(100, 200);
return EXIT_SUCCESS;
}
// --- main ---
+48
View File
@@ -0,0 +1,48 @@
// ============================================================================
// p1c03_45 — 3.14.3 extern “C”浅析
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:46] 3.14.3 extern “C”浅析 ---
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#if 0
#ifdef __cplusplus
extern "C" {
#if 0
void func1();
int func2(int a, int b);
#else
#include"MyModule.h"
#endif
}
#endif
#else
extern "C" void func1();
extern "C" int func2(int a, int b);
#endif
int main(){
func1();
cout << func2(10, 20) << endl;
return EXIT_SUCCESS;
}
// --- 为使本示例作为独立目标可链接,就地提供 func1/func2 的 extern "C" 定义 ---
// (原文仅声明;定义在配对的 MyModule.c。此处补最小实现以自洽编译运行。)
extern "C" void func1(){ printf("hello world!"); }
extern "C" int func2(int a, int b){ return a + b; }
// --- main ---
+13
View File
@@ -0,0 +1,13 @@
// 58954439:45 — MyModule 实现文件(与 my_module.h 配对)
// 由 gen_part1.py 成对输出。
#include"my_module.h"
#include <stdlib.h>
void func1(){
printf("hello world!");
}
int func2(int a, int b){
return a + b;
}
int main(){ func1(); printf("func2=%d\n", func2(3,4)); return EXIT_SUCCESS; }
+37
View File
@@ -0,0 +1,37 @@
// ============================================================================
// p1c03_42 — 3.14.2.1 函数重载基本语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:41] 3.14.2.1 函数重载基本语法 ---
namespace A{
void MyFunc(){ cout << "无参数!" << endl; }
void MyFunc(int a){ cout << "a: " << a << endl; }
void MyFunc(string b){ cout << "b: " << b << endl; }
void MyFunc(int a, string b){ cout << "a: " << a << " b:" << b << endl;}
void MyFunc(string b, int a){ cout << "a: " << a << " b:" << b << endl;}
}
namespace B{
void MyFunc(string b, int a){}
//int MyFunc(string b, int a){} //无法重载仅按返回值区分的函数
}
void test(){
A::MyFunc();
A::MyFunc(10);
A::MyFunc("hello");
A::MyFunc(10, "world");
A::MyFunc("world", 10);
B::MyFunc("ok", 1);
}
// --- main ---
int main() {
test();
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
// ============================================================================
// p1c03_43 — 3.14.2.1 函数重载基本语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:42] 3.14.2.1 函数重载基本语法 ---
void MyFunc(string b){
cout << "b: " << b << endl;
}
//函数重载碰上默认参数
void MyFunc(string b, int a = 10){
cout << "a: " << a << " b:" << b << endl;
}
int main(){
// MyFunc("hello"); // 二义:MyFunc(string) 与 MyFunc(string,int=10) 同等匹配
// MyFunc(string("hello")); // 仍二义:强转 string 也无法区分两候选
MyFunc("hello", 20); // 显式给第二参 → 明确选双参重载
return 0;
}
// --- main ---
+20
View File
@@ -0,0 +1,20 @@
// ============================================================================
// p1c03_44 — 3.14.2.2 函数重载实现原理
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:43] 3.14.2.2 函数重载实现原理 ---
void func(){}
void func(int x){}
void func(int x,char y){}
// --- main ---
int main() {
return 0;
}
+22
View File
@@ -0,0 +1,22 @@
// ============================================================================
// p1c03_14 — 3.3 全局变量检测增强
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// --- 原文片段 [58954439:13] 3.3 全局变量检测增强 ---
int a = 10; //赋值,当做定义
int a; //没有赋值,当做声明
int main(){
printf("a:%d\n",a);
return EXIT_SUCCESS;
}
// --- main ---
+22
View File
@@ -0,0 +1,22 @@
// ============================================================================
// p1c03_39 — 3.11.3.2 类内部的内联函数
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:38] 3.11.3.2 类内部的内联函数 ---
class Person{
public:
Person(){ cout << "构造函数!" << endl; }
void PrintPerson(){ cout << "输出Person!" << endl; }
};
// --- main ---
int main() {
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
// ============================================================================
// p1c03_37 — 3.11.2 预处理宏的缺陷
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:36] 3.11.2 预处理宏的缺陷 ---
#define ADD(x,y) x+y
inline int Add(int x,int y){
return x + y;
}
void test(){
int ret1 = ADD(10, 20) * 10; //希望的结果是300
int ret2 = Add(10, 20) * 10; //希望结果也是300
cout << "ret1:" << ret1 << endl; //210
cout << "ret2:" << ret2 << endl; //300
}
// --- main ---
int main() {
test();
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
// ============================================================================
// p1c03_38 — 3.11.2 预处理宏的缺陷
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:37] 3.11.2 预处理宏的缺陷 ---
#define COMPARE(x,y) ((x) < (y) ? (x) : (y))
int Compare(int x,int y){
return x < y ? x : y;
}
void test02(){
int a = 1;
int b = 3;
//cout << "COMPARE(++a, b):" << COMPARE(++a, b) << endl; // 3
cout << "Compare(int x,int y):" << Compare(++a, b) << endl; //2
}
// --- main ---
int main() {
test02();
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef MYMODULE_H
#define MYMODULE_H
#include<stdio.h>
#if __cplusplus
extern "C"{
#endif
void func1();
int func2(int a,int b);
#if __cplusplus
}
#endif
#endif
+28
View File
@@ -0,0 +1,28 @@
// ============================================================================
// p1c03_03 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:2] 3.2.2命名空间使用语法 ---
namespace A{
int a = 10;
}
namespace B{
int a = 20;
}
void test(){
cout << "A::a : " << A::a << endl;
cout << "B::a : " << B::a << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
// ============================================================================
// p1c03_04 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:3] 3.2.2命名空间使用语法 ---
namespace A{
int a = 10;
}
namespace B{
int a = 20;
}
void test(){
cout << "A::a : " << A::a << endl;
cout << "B::a : " << B::a << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
// ============================================================================
// p1c03_05 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:4] 3.2.2命名空间使用语法 ---
namespace A{
int a = 10;
namespace B{
int a = 20;
}
}
void test(){
cout << "A::a : " << A::a << endl;
cout << "A::B::a : " << A::B::a << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
// ============================================================================
// p1c03_06 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:5] 3.2.2命名空间使用语法 ---
namespace A{
int a = 10;
}
namespace A{
void func(){
cout << "hello namespace!" << endl;
}
}
void test(){
cout << "A::a : " << A::a << endl;
A::func();
}
// --- main ---
int main() {
test();
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
// ============================================================================
// p1c03_07 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:6] 3.2.2命名空间使用语法 ---
namespace MySpace{
void func1();
void func2(int param);
}
// --- main ---
int main() {
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
// ============================================================================
// p1c03_08 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:7] 3.2.2命名空间使用语法 ---
namespace MySpace{
void func1();
void func2(int param);
}
void MySpace::func1(){
cout << "MySpace::func1" << endl;
}
void MySpace::func2(int param){
cout << "MySpace::func2 : " << param << endl;
}
void test(){
MySpace::func1();
MySpace::func2(42);
}
// --- main ---
int main() {
test();
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
// ============================================================================
// p1c03_09 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:8] 3.2.2命名空间使用语法 ---
namespace{
int a = 10;
void func(){ cout << "hello namespace" << endl; }
}
void test(){
cout << "a : " << a << endl;
func();
}
// --- main ---
int main() {
test();
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
// ============================================================================
// p1c03_10 — 3.2.2命名空间使用语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:9] 3.2.2命名空间使用语法 ---
namespace veryLongName{
int a = 10;
void func(){ cout << "hello namespace" << endl; }
}
void test(){
namespace shortName = veryLongName;
cout << "veryLongName::a : " << shortName::a << endl;
veryLongName::func();
shortName::func();
}
// --- main ---
int main() {
test();
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
// ============================================================================
// p1c03_41 — 3.13 函数的占位参数
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:40] 3.13 函数的占位参数 ---
void TestFunc01(int a,int b,int){
//函数内部无法使用占位参数
cout << "a + b = " << a + b << endl;
}
//占位参数也可以设置默认值
void TestFunc02(int a, int b, int = 20){
//函数内部依旧无法使用占位参数
cout << "a + b = " << a + b << endl;
}
int main(){
//错误调用,占位参数也是参数,必须传参数
//TestFunc01(10,20);
//正确调用
TestFunc01(10,20,30);
//正确调用
TestFunc02(10,20);
//正确调用
TestFunc02(10, 20, 30);
return EXIT_SUCCESS;
}
// --- main ---
+41
View File
@@ -0,0 +1,41 @@
// ============================================================================
// p1c03_34 — 3.10.4 指针引用
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:33] 3.10.4 指针引用 ---
struct Teacher{
int mAge;
};
//指针间接修改teacher的年龄
void AllocateAndInitByPointer(Teacher** teacher){
*teacher = (Teacher*)malloc(sizeof(Teacher));
(*teacher)->mAge = 200;
}
//引用修改teacher年龄
void AllocateAndInitByReference(Teacher*& teacher){
teacher->mAge = 300;
}
void test(){
//创建Teacher
Teacher* teacher = NULL;
//指针间接赋值
AllocateAndInitByPointer(&teacher);
cout << "AllocateAndInitByPointer:" << teacher->mAge << endl;
//引用赋值,将teacher本身传到ChangeAgeByReference函数中
AllocateAndInitByReference(teacher);
cout << "AllocateAndInitByReference:" << teacher->mAge << endl;
free(teacher);
}
// --- main ---
int main() {
test();
return 0;
}
+58
View File
@@ -0,0 +1,58 @@
// ============================================================================
// p1c03_29 — 3.10.1 引用基本用法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:28] 3.10.1 引用基本用法 ---
//1. 认识引用
void test01(){
int a = 10;
//给变量a取一个别名b
int& b = a;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "------------" << endl;
//操作b就相当于操作a本身
b = 100;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "------------" << endl;
//一个变量可以有n个别名
int& c = a;
c = 200;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "c:" << c << endl;
cout << "------------" << endl;
//a,b,c的地址都是相同的
cout << "a:" << &a << endl;
cout << "b:" << &b << endl;
cout << "c:" << &c << endl;
}
//2. 使用引用注意事项
void test02(){
//1) 引用必须初始化
//int& ref; //报错:必须初始化引用
//2) 引用一旦初始化,不能改变引用
int a = 10;
int b = 20;
int& ref = a;
ref = b; //不能改变引用
//3) 不能对数组建立引用
int arr[10];
//int& ref3[10] = arr;
}
// --- main ---
int main() {
test01();
test02();
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
// ============================================================================
// p1c03_30 — 3.10.1 引用基本用法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:29] 3.10.1 引用基本用法 ---
static void block_0() {
//1. 建立数组引用方法一
typedef int ArrRef[10];
int arr[10];
ArrRef& aRef = arr;
for (int i = 0; i < 10;i ++){
aRef[i] = i+1;
}
for (int i = 0; i < 10;i++){
cout << arr[i] << " ";
}
cout << endl;
//2. 建立数组引用方法二
int(&f)[10] = arr;
for (int i = 0; i < 10; i++){
f[i] = i+10;
}
for (int i = 0; i < 10; i++){
cout << arr[i] << " ";
}
cout << endl;
}
// --- main ---
int main() {
block_0();
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
// ============================================================================
// p1c03_33 — 3.10.3 引用的本质
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:32] 3.10.3 引用的本质 ---
//发现是引用,转换为 int* const ref = &a;
void testFunc(int& ref){
ref = 100; // ref是引用,转换为*ref = 100
}
int main(){
int a = 10;
int& aRef = a; //自动转换为 int* const aRef = &a;这也能说明引用为什么必须初始化
aRef = 20; //内部发现aRef是引用,自动帮我们转换为: *aRef = 20;
cout << "a:" << a << endl;
cout << "aRef:" << aRef << endl;
testFunc(a);
return EXIT_SUCCESS;
}
// --- main ---
+49
View File
@@ -0,0 +1,49 @@
// ============================================================================
// p1c03_31 — 3.10.2 函数中的引用
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:30] 3.10.2 函数中的引用 ---
//值传递
void ValueSwap(int m,int n){
int temp = m;
m = n;
n = temp;
}
//地址传递
void PointerSwap(int* m,int* n){
int temp = *m;
*m = *n;
*n = temp;
}
//引用传递
void ReferenceSwap(int& m,int& n){
int temp = m;
m = n;
n = temp;
}
void test(){
int a = 10;
int b = 20;
//值传递
ValueSwap(a, b);
cout << "a:" << a << " b:" << b << endl;
//地址传递
PointerSwap(&a, &b);
cout << "a:" << a << " b:" << b << endl;
//引用传递
ReferenceSwap(a, b);
cout << "a:" << a << " b:" << b << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
// ============================================================================
// p1c03_32 — 3.10.2 函数中的引用
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:31] 3.10.2 函数中的引用 ---
//返回局部变量引用
int& TestFun01(){
int a = 10; //局部变量
return a;
}
//返回静态变量引用
int& TestFunc02(){
static int a = 20;
cout << "static int a : " << a << endl;
return a;
}
int main(){
//不能返回局部变量的引用
int& ret01 = TestFun01();
//如果函数做左值,那么必须返回引用
TestFunc02();
TestFunc02() = 100;
TestFunc02();
return EXIT_SUCCESS;
}
// --- main ---
+26
View File
@@ -0,0 +1,26 @@
// ============================================================================
// p1c03_01 — 3.1 ::作用域运算符
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:0] 3.1 ::作用域运算符 ---
//全局变量
int a = 10;
void test(){
//局部变量
int a = 20;
//全局a被隐藏
cout << "a:" << a << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
// ============================================================================
// p1c03_02 — 3.1 ::作用域运算符
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:1] 3.1 ::作用域运算符 ---
//全局变量
int a = 10;
//1. 局部变量和全局变量同名
void test(){
int a = 20;
//打印局部变量a
cout << "局部变量a:" << a << endl;
//打印全局变量a
cout << "全局变量a:" << ::a << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+50
View File
@@ -0,0 +1,50 @@
// ============================================================================
// p1c03_15 — 3.4 C++中所有的变量和函数都必须有类型
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// --- 原文片段 [58954439:14] 3.4 C++中所有的变量和函数都必须有类型 ---
//i没有写类型,可以是任意类型
int fun1(int i){
printf("%d\n", i);
return 0;
}
//i没有写类型,可以是任意类型
int fun2(char* i){
printf("%s\n", i);
return 0;
}
//没有写参数,代表可以传任何类型的实参
int fun3(void){
printf("fun33333333333333333\n");
return 0;
}
//C语言,如果函数没有参数,建议写void,代表没有参数
int fun4(void){
printf("fun4444444444444\n");
return 0;
}
int g(void){
return 10;
}
int main(){
fun1(10);
fun2("abc");
fun3(); // 注意:C89 中 fun3() 可接受任意参数;C17 下 fun3(void) 不接受参数
printf("g = %d\n", g());
return 0;
}
// --- main ---
+23
View File
@@ -0,0 +1,23 @@
// ============================================================================
// p1c03_16 — 3.5 更严格的类型转换
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// --- 原文片段 [58954439:15] 3.5 更严格的类型转换 ---
typedef enum COLOR{ GREEN, RED, YELLOW } color;
int main(){
color mycolor = GREEN;
mycolor = 10;
printf("mycolor:%d\n", mycolor);
char* p = malloc(10);
return EXIT_SUCCESS;
}
// --- main ---
+36
View File
@@ -0,0 +1,36 @@
// ============================================================================
// p1c03_17 — 3.6 struct类型加强
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:16] 3.6 struct类型加强 ---
//1. 结构体中即可以定义成员变量,也可以定义成员函数
struct Student{
string mName;
int mAge;
void setName(string name){ mName = name; }
void setAge(int age){ mAge = age; }
void showStudent(){
cout << "Name:" << mName << " Age:" << mAge << endl;
}
};
//2. c++中定义结构体变量不需要加struct关键字
void test01(){
Student student;
student.setName("John");
student.setAge(20);
student.showStudent();
}
// --- main ---
int main() {
test01();
return 0;
}
+28
View File
@@ -0,0 +1,28 @@
// ============================================================================
// p1c03_19 — 3.8 三目运算符功能增强
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// --- 原文片段 [58954439:18] 3.8 三目运算符功能增强 ---
static void block_0() {
int a = 10;
int b = 20;
printf("ret:%d\n", a > b ? a : b);
//思考一个问题,(a > b ? a : b) 三目运算表达式返回的是什么?
//(a > b ? a : b) = 100;
//返回的是右值
}
// --- main ---
int main() {
block_0();
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
// ============================================================================
// p1c03_20 — 3.8 三目运算符功能增强
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:19] 3.8 三目运算符功能增强 ---
static void block_0() {
int a = 10;
int b = 20;
printf("ret:%d\n", a > b ? a : b);
//思考一个问题,(a > b ? a : b) 三目运算表达式返回的是什么?
cout << "b:" << b << endl;
//返回的是左值,变量的引用
(a > b ? a : b) = 100;//返回的是左值,变量的引用
cout << "b:" << b << endl;
}
// --- main ---
int main() {
block_0();
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
// ============================================================================
// p1c03_11 — 3.2.3 using声明
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:10] 3.2.3 using声明 ---
namespace A{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcA" << endl; }
}
void test(){
//1. 通过命名空间域运算符
cout << A::paramA << endl;
A::funcA();
//2. using声明
using A::paramA;
using A::funcA;
cout << paramA << endl;
//cout << paramB << endl; //不可直接访问
funcA();
//3. 同名冲突
//int paramA = 20; //相同作用域注意同名冲突
}
// --- main ---
int main() {
test();
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
// ============================================================================
// p1c03_12 — 3.2.3 using声明
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:11] 3.2.3 using声明 ---
namespace A{
void func(){}
void func(int x){}
int func(int x,int y){}
}
void test(){
using A::func;
func();
func(10);
func(10, 20);
}
// --- main ---
int main() {
test();
return 0;
}
+51
View File
@@ -0,0 +1,51 @@
// ============================================================================
// p1c03_13 — 3.2.4 using编译指令
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954439:12] 3.2.4 using编译指令 ---
namespace A{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcB" << endl; }
}
void test01(){
using namespace A;
cout << paramA << endl;
cout << paramB << endl;
funcA();
funcB();
//不会产生二义性
int paramA = 30;
cout << paramA << endl;
}
namespace B{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcB" << endl; }
}
void test02(){
using namespace A;
using namespace B;
//二义性产生,不知道调用A还是B的paramA
//cout << paramA << endl;
}
// --- main ---
int main() {
test01();
test02();
return 0;
}
+18
View File
@@ -0,0 +1,18 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch04/s01
add_executable(p1c04_01_01 class_encapsulation.c)
set_target_properties(p1c04_01_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
target_compile_options(p1c04_01_01 PRIVATE -Wall -Wextra)
add_executable(p1c04_01_02 class_encapsulation.cpp)
set_target_properties(p1c04_01_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_01_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_01_02 PRIVATE -Wall -Wextra)
add_executable(p1c04_01_03 class_encapsulation_2.cpp)
set_target_properties(p1c04_01_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_01_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_01_03 PRIVATE -Wall -Wextra)
add_executable(p1c04_01_04 private_members.cpp)
set_target_properties(p1c04_01_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_01_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_01_04 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch04_s01 DEPENDS p1c04_01_01 p1c04_01_02 p1c04_01_03 p1c04_01_04)
+41
View File
@@ -0,0 +1,41 @@
// ============================================================================
// p1c04_01_01 — 4.1.2 类的封装
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// --- 原文片段 [58954448:0] 4.1.2 类的封装 ---
typedef struct _Person{
char name[64];
int age;
}Person;
typedef struct _Aninal{
char name[64];
int age;
int type; //动物种类
}Ainmal;
void PersonEat(Person* person){
printf("%s在吃人吃的饭!\n",person->name);
}
void AnimalEat(Ainmal* animal){
printf("%s在吃动物吃的饭!\n", animal->name);
}
int main(){
Person person;
strcpy(person.name, "小明");
person.age = 30;
AnimalEat(&person);
return EXIT_SUCCESS;
}
// --- main ---
+40
View File
@@ -0,0 +1,40 @@
// ============================================================================
// p1c04_01_02 — 4.1.2 类的封装
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954448:1] 4.1.2 类的封装 ---
//封装两层含义
//1. 属性和行为合成一个整体
//2. 访问控制,现实事物本身有些属性和行为是不对外开放
class Person{
//人具有的行为(函数)
public:
void Dese(){ cout << "我有钱,年轻,个子又高,就爱嘚瑟!" << endl;}
//人的属性(变量)
public:
int mTall; //多高,可以让外人知道
protected:
int mMoney; // 有多少钱,只能儿子孙子知道
private:
int mAge; //年龄,不想让外人知道
};
int main(){
Person p;
p.mTall = 220;
//p.mMoney 保护成员外部无法访问
//p.mAge 私有成员外部无法访问
p.Dese();
return EXIT_SUCCESS;
}
// --- main ---
+31
View File
@@ -0,0 +1,31 @@
// ============================================================================
// p1c04_01_03 — 4.1.2 类的封装
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954448:2] 4.1.2 类的封装 ---
class A{
int mAge;
};
struct B{
int mAge;
};
void test(){
A a;
B b;
//a.mAge; //无法访问私有成员
b.mAge; //可正常外部访问
}
// --- main ---
int main() {
test();
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
// ============================================================================
// p1c04_01_04 — 4.1.3 将成员变量设置为private
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954448:3] 4.1.3 将成员变量设置为private ---
static void block_0() {
class AccessLevels{
public:
//对只读属性进行只读访问
int getReadOnly(){ return readOnly; }
//对读写属性进行读写访问
void setReadWrite(int val){ readWrite = val; }
int getReadWrite(){ return readWrite; }
//对只写属性进行只写访问
void setWriteOnly(int val){ writeOnly = val; }
private:
int readOnly; //对外只读访问
int noAccess; //外部不可访问
int readWrite; //读写访问
int writeOnly; //只写访问
};
}
// --- main ---
int main() {
block_0();
return 0;
}
+11
View File
@@ -0,0 +1,11 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch04/s02
add_executable(p1c04_02_01 cube_class_design.cpp)
set_target_properties(p1c04_02_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_02_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_02_01 PRIVATE -Wall -Wextra)
add_executable(p1c04_02_02 point_and_circle.cpp)
set_target_properties(p1c04_02_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_02_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_02_02 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch04_s02 DEPENDS p1c04_02_01 p1c04_02_02)
+80
View File
@@ -0,0 +1,80 @@
// ============================================================================
// p1c04_02_01 — 4.2.1 设计立方体类
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954449:0] 4.2.1 设计立方体类 ---
//立方体类
class Cub{
public:
void setL(int l){ mL = l; }
void setW(int w){ mW = w; }
void setH(int h){ mH = h; }
int getL(){ return mL; }
int getW(){ return mW; }
int getH(){ return mH; }
//立方体面积
int caculateS(){ return (mL*mW + mL*mH + mW*mH) * 2; }
//立方体体积
int caculateV(){ return mL * mW * mH; }
//成员方法
bool CubCompare(Cub& c){
if (getL() == c.getL() && getW() == c.getW() && getH() == c.getH()){
return true;
}
return false;
}
private:
int mL; //长
int mW; //宽
int mH; //高
};
//比较两个立方体是否相等
bool CubCompare(Cub& c1, Cub& c2){
if (c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH()){
return true;
}
return false;
}
void test(){
Cub c1, c2;
c1.setL(10);
c1.setW(20);
c1.setH(30);
c2.setL(20);
c2.setW(20);
c2.setH(30);
cout << "c1面积:" << c1.caculateS() << " 体积:" << c1.caculateV() << endl;
cout << "c2面积:" << c2.caculateS() << " 体积:" << c2.caculateV() << endl;
//比较两个立方体是否相等
if (CubCompare(c1, c2)){
cout << "c1和c2相等!" << endl;
}
else{
cout << "c1和c2不相等!" << endl;
}
if (c1.CubCompare(c2)){
cout << "c1和c2相等!" << endl;
}
else{
cout << "c1和c2不相等!" << endl;
}
}
// --- main ---
int main() {
test();
return 0;
}
+71
View File
@@ -0,0 +1,71 @@
// ============================================================================
// p1c04_02_02 — 4.2.2 点和圆的关系
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954449:1] 4.2.2 点和圆的关系 ---
//点类
class Point{
public:
void setX(int x){ mX = x; }
void setY(int y){ mY = y; }
int getX(){ return mX; }
int getY(){ return mY; }
private:
int mX;
int mY;
};
//圆类
class Circle{
public:
void setP(int x,int y){
mP.setX(x);
mP.setY(y);
}
void setR(int r){ mR = r; }
Point& getP(){ return mP; }
int getR(){ return mR; }
//判断点和圆的关系
void IsPointInCircle(Point& point){
int distance = (point.getX() - mP.getX()) * (point.getX() - mP.getX()) + (point.getY() - mP.getY()) * (point.getY() - mP.getY());
int radius = mR * mR;
if (distance < radius){
cout << "Point(" << point.getX() << "," << point.getY() << ")在圆内!" << endl;
}
else if (distance > radius){
cout << "Point(" << point.getX() << "," << point.getY() << ")在圆外!" << endl;
}
else{
cout << "Point(" << point.getX() << "," << point.getY() << ")在圆上!" << endl;
}
}
private:
Point mP; //圆心
int mR; //半径
};
void test(){
//实例化圆对象
Circle circle;
circle.setP(20, 20);
circle.setR(5);
//实例化点对象
Point point;
point.setX(25);
point.setY(20);
circle.IsPointInCircle(point);
}
// --- main ---
int main() {
test();
return 0;
}
+71
View File
@@ -0,0 +1,71 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch04/s03
add_executable(p1c04_03_01 ctor_and_dtor.cpp)
set_target_properties(p1c04_03_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_01 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_02 ctor_classification.cpp)
set_target_properties(p1c04_03_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_02 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_03 copy_ctor_timing.cpp)
set_target_properties(p1c04_03_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_03 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_04 deep_vs_shallow_copy.cpp)
set_target_properties(p1c04_03_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_04 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_05 initializer_list.cpp)
set_target_properties(p1c04_03_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_05 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_06 class_object_member.cpp)
set_target_properties(p1c04_03_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_06 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_07 explicit_keyword.cpp)
set_target_properties(p1c04_03_07 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_07 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_07 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_08 c_dynamic_alloc.cpp)
set_target_properties(p1c04_03_08 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_08 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_08 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_09 new_operator.cpp)
set_target_properties(p1c04_03_09 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_09 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_09 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_10 delete_operator.cpp)
set_target_properties(p1c04_03_10 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_10 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_10 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_11 new_delete_array.cpp)
set_target_properties(p1c04_03_11 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_11 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_11 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_12 new_delete_array_2.cpp)
set_target_properties(p1c04_03_12 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_12 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_12 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_13 delete_void_ptr.cpp)
set_target_properties(p1c04_03_13 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_13 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_13 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_14 static_member_variable.cpp)
set_target_properties(p1c04_03_14 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_14 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_14 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_15 static_member_function.cpp)
set_target_properties(p1c04_03_15 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_15 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_15 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_16 const_static_member.cpp)
set_target_properties(p1c04_03_16 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_16 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_16 PRIVATE -Wall -Wextra)
add_executable(p1c04_03_17 static_singleton.cpp)
set_target_properties(p1c04_03_17 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_03_17 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_03_17 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch04_s03 DEPENDS p1c04_03_01 p1c04_03_02 p1c04_03_03 p1c04_03_04 p1c04_03_05 p1c04_03_06 p1c04_03_07 p1c04_03_08 p1c04_03_09 p1c04_03_10 p1c04_03_11 p1c04_03_12 p1c04_03_13 p1c04_03_14 p1c04_03_15 p1c04_03_16 p1c04_03_17)
+52
View File
@@ -0,0 +1,52 @@
// ============================================================================
// p1c04_03_08 — 4.3.9.2 C动态分配内存方法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954451:7] 4.3.9.2 C动态分配内存方法 ---
class Person{
public:
Person(){
mAge = 20;
pName = (char*)malloc(strlen("john")+1);
strcpy(pName, "john");
}
void Init(){
mAge = 20;
pName = (char*)malloc(strlen("john")+1);
strcpy(pName, "john");
}
void Clean(){
if (pName != NULL){
free(pName);
}
}
public:
int mAge;
char* pName;
};
int main(){
//分配内存
Person* person = (Person*)malloc(sizeof(Person));
if(person == NULL){
return 0;
}
//调用初始化函数
person->Init();
//清理对象
person->Clean();
//释放person对象
free(person);
return EXIT_SUCCESS;
}
// --- main ---
+90
View File
@@ -0,0 +1,90 @@
// ============================================================================
// p1c04_03_06 — 4.3.7.2 类对象作为成员
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:5] 4.3.7.2 类对象作为成员 ---
//汽车类
class Car{
public:
Car(){
cout << "Car 默认构造函数!" << endl;
mName = "大众汽车";
}
Car(string name){
cout << "Car 带参数构造函数!" << endl;
mName = name;
}
~Car(){
cout << "Car 析构函数!" << endl;
}
public:
string mName;
};
//拖拉机
class Tractor{
public:
Tractor(){
cout << "Tractor 默认构造函数!" << endl;
mName = "爬土坡专用拖拉机";
}
Tractor(string name){
cout << "Tractor 带参数构造函数!" << endl;
mName = name;
}
~Tractor(){
cout << "Tractor 析构函数!" << endl;
}
public:
string mName;
};
//人类
class Person{
public:
#if 1
//类mCar不存在合适的构造函数
Person(string name){
mName = name;
}
#else
//初始化列表可以指定调用构造函数
Person(string carName, string tracName, string name) : mTractor(tracName), mCar(carName), mName(name){
cout << "Person 构造函数!" << endl;
}
#endif
void GoWorkByCar(){
cout << mName << "开着" << mCar.mName << "去上班!" << endl;
}
void GoWorkByTractor(){
cout << mName << "开着" << mTractor.mName << "去上班!" << endl;
}
~Person(){
cout << "Person 析构函数!" << endl;
}
private:
string mName;
Car mCar;
Tractor mTractor;
};
void test(){
//Person person("宝马", "东风拖拉机", "赵四");
Person person("刘能");
person.GoWorkByCar();
person.GoWorkByTractor();
}
// --- main ---
int main() {
test();
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
// ============================================================================
// p1c04_03_16 — 4.3.10.3 const静态成员属性
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:15] 4.3.10.3 const静态成员属性 ---
class Person{
public:
//static const int mShare = 10;
const static int mShare = 10; //只读区,不可修改
};
int main(){
cout << Person::mShare << endl;
//Person::mShare = 20;
return EXIT_SUCCESS;
}
// --- main ---
+69
View File
@@ -0,0 +1,69 @@
// ============================================================================
// p1c04_03_03 — 4.3.4 拷贝构造函数的调用时机
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
// --- 原文片段 [58954451:2] 4.3.4 拷贝构造函数的调用时机 ---
class Person{
public:
Person(){
cout << "no param contructor!" << endl;
mAge = 10;
}
Person(int age){
cout << "param constructor!" << endl;
mAge = age;
}
Person(const Person& person){
cout << "copy constructor!" << endl;
mAge = person.mAge;
}
~Person(){
cout << "destructor!" << endl;
}
public:
int mAge;
};
//1. 旧对象初始化新对象
void test01(){
Person p(10);
Person p1(p);
Person p2 = Person(p);
Person p3 = p; // 相当于Person p2 = Person(p);
}
//2. 传递的参数是普通对象,函数参数也是普通对象,传递将会调用拷贝构造
void doBussiness(Person p){}
void test02(){
Person p(10);
doBussiness(p);
}
//3. 函数返回局部对象
Person MyBusiness(){
Person p(10);
cout << "局部p:" << (int*)&p << endl;
return p;
}
void test03(){
//vs release、qt下没有调用拷贝构造函数
//vs debug下调用一次拷贝构造函数
Person p = MyBusiness();
cout << "局部p:" << (int*)&p << endl;
}
// --- main ---
int main() {
test01();
test03();
return 0;
}
+45
View File
@@ -0,0 +1,45 @@
// ============================================================================
// p1c04_03_01 — 4.3.2 构造函数和析构函数
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954451:0] 4.3.2 构造函数和析构函数 ---
class Person{
public:
Person(){
cout << "构造函数调用!" << endl;
pName = (char*)malloc(sizeof("John"));
strcpy(pName, "John");
mTall = 150;
mMoney = 100;
}
~Person(){
cout << "析构函数调用!" << endl;
if (pName != NULL){
free(pName);
pName = NULL;
}
}
public:
char* pName;
int mTall;
int mMoney;
};
void test(){
Person person;
cout << person.pName << person.mTall << person.mMoney << endl;
}
// --- main ---
int main() {
test();
return 0;
}
+83
View File
@@ -0,0 +1,83 @@
// ============================================================================
// p1c04_03_02 — 4.3.3 构造函数的分类及调用
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
// --- 原文片段 [58954451:1] 4.3.3 构造函数的分类及调用 ---
class Person{
public:
Person(){
cout << "no param constructor!" << endl;
mAge = 0;
}
//有参构造函数
Person(int age){
cout << "1 param constructor!" << endl;
mAge = age;
}
//拷贝构造函数(复制构造函数) 使用另一个对象初始化本对象
Person(const Person& person){
cout << "copy constructor!" << endl;
mAge = person.mAge;
}
//打印年龄
void PrintPerson(){
cout << "Age:" << mAge << endl;
}
private:
int mAge;
};
//1. 无参构造调用方式
void test01(){
//调用无参构造函数
Person person1;
person1.PrintPerson();
//无参构造函数错误调用方式
//Person person2();
//person2.PrintPerson();
}
//2. 调用有参构造函数
void test02(){
//第一种 括号法,最常用
Person person01(100);
person01.PrintPerson();
//调用拷贝构造函数
Person person02(person01);
person02.PrintPerson();
//第二种 匿名对象(显示调用构造函数)
Person(200); //匿名对象,没有名字的对象
Person person03 = Person(300);
person03.PrintPerson();
//注意: 使用匿名对象初始化判断调用哪一个构造函数,要看匿名对象的参数类型
Person person06(Person(400)); //等价于 Person person06 = Person(400);
person06.PrintPerson();
//第三种 =号法 隐式转换
Person person04 = 100; //Person person04 = Person(100)
person04.PrintPerson();
//调用拷贝构造
Person person05 = person04; //Person person05 = Person(person04)
person05.PrintPerson();
}
// --- main ---
int main() {
test01();
test02();
return 0;
}
+47
View File
@@ -0,0 +1,47 @@
// ============================================================================
// p1c04_03_04 — 4.3.6 深拷贝和浅拷贝
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954451:3] 4.3.6 深拷贝和浅拷贝 ---
class Person{
public:
Person(char* name,int age){
pName = (char*)malloc(strlen(name) + 1);
strcpy(pName,name);
mAge = age;
}
//增加拷贝构造函数
Person(const Person& person){
pName = (char*)malloc(strlen(person.pName) + 1);
strcpy(pName, person.pName);
mAge = person.mAge;
}
~Person(){
if (pName != NULL){
free(pName);
}
}
private:
char* pName;
int mAge;
};
void test(){
Person p1("Edward",30);
//用对象p1初始化对象p2,调用c++提供的默认拷贝构造函数
Person p2 = p1;
}
// --- main ---
int main() {
test();
return 0;
}
+58
View File
@@ -0,0 +1,58 @@
// ============================================================================
// p1c04_03_10 — 4.3.9.4 delete operator
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954451:9] 4.3.9.4 delete operator ---
class Person{
public:
Person(){
cout << "无参构造函数!" << endl;
pName = (char*)malloc(strlen("undefined") + 1);
strcpy(pName, "undefined");
mAge = 0;
}
Person(char* name, int age){
cout << "有参构造函数!" << endl;
pName = (char*)malloc(strlen(name) + 1);
strcpy(pName, name);
mAge = age;
}
void ShowPerson(){
cout << "Name:" << pName << " Age:" << mAge << endl;
}
~Person(){
cout << "析构函数!" << endl;
if (pName != NULL){
delete pName;
pName = NULL;
}
}
public:
char* pName;
int mAge;
};
void test(){
Person* person1 = new Person;
Person* person2 = new Person("John",33);
person1->ShowPerson();
person2->ShowPerson();
delete person1;
delete person2;
}
// --- main ---
int main() {
test();
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
// ============================================================================
// p1c04_03_13 — 4.3.9.6 delete void*可能会出错
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954451:12] 4.3.9.6 delete void*可能会出错 ---
class Person{
public:
Person(char* name, int age){
pName = (char*)malloc(sizeof(name));
strcpy(pName,name);
mAge = age;
}
~Person(){
if (pName != NULL){
delete pName;
}
}
public:
char* pName;
int mAge;
};
void test(){
void* person = new Person("john",20);
delete person;
}
// --- main ---
int main() {
test();
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
// ============================================================================
// p1c04_03_07 — 4.3.8 explicit关键字
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:6] 4.3.8 explicit关键字 ---
class MyString{
public:
explicit MyString(int n){
cout << "MyString(int n)!" << endl;
}
MyString(const char* str){
cout << "MyString(const char* str)" << endl;
}
};
int main(){
//给字符串赋值?还是初始化?
//MyString str1 = 1;
MyString str2(10);
//寓意非常明确,给字符串赋值
MyString str3 = "abcd";
MyString str4("abcd");
return EXIT_SUCCESS;
}
// --- main ---
+42
View File
@@ -0,0 +1,42 @@
// ============================================================================
// p1c04_03_05 — 4.3.7.1 初始化列表
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:4] 4.3.7.1 初始化列表 ---
static void block_0() {
class Person{
public:
#if 0
//传统方式初始化
Person(int a,int b,int c){
mA = a;
mB = b;
mC = c;
}
#endif
//初始化列表方式初始化
Person(int a, int b, int c):mA(a),mB(b),mC(c){}
void PrintPerson(){
cout << "mA:" << mA << endl;
cout << "mB:" << mB << endl;
cout << "mC:" << mC << endl;
}
private:
int mA;
int mB;
int mC;
};
}
// --- main ---
int main() {
block_0();
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
// ============================================================================
// p1c04_03_11 — 4.3.9.5 用于数组的new和delete
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:10] 4.3.9.5 用于数组的new和delete ---
static void block_0() {
//创建字符数组
char* pStr = new char[100];
//创建整型数组
int* pArr1 = new int[100];
//创建整型数组并初始化
int* pArr2 = new int[10]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//释放数组内存
delete[] pStr;
delete[] pArr1;
delete[] pArr2;
}
// --- main ---
int main() {
block_0();
return 0;
}
+48
View File
@@ -0,0 +1,48 @@
// ============================================================================
// p1c04_03_12 — 4.3.9.5 用于数组的new和delete
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954451:11] 4.3.9.5 用于数组的new和delete ---
class Person{
public:
Person(){
pName = (char*)malloc(strlen("undefined") + 1);
strcpy(pName, "undefined");
mAge = 0;
}
Person(char* name, int age){
pName = (char*)malloc(sizeof(name));
strcpy(pName, name);
mAge = age;
}
~Person(){
if (pName != NULL){
delete pName;
}
}
public:
char* pName;
int mAge;
};
void test(){
//栈聚合初始化
Person person[] = { Person("john", 20), Person("Smith", 22) };
cout << person[1].pName << endl;
//创建堆上对象数组必须提供构造函数
Person* workers = new Person[20];
}
// --- main ---
int main() {
test();
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
// ============================================================================
// p1c04_03_09 — 4.3.9.3 new operator
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:8] 4.3.9.3 new operator ---
// 原文为伪代码对照,演示 new 与 malloc 的等价关系:
// Person* person = new Person;
// 相当于:
// Person* person = (Person*)malloc(sizeof(Person));
// if(person == NULL){ return 0; }
// person->Init(); // 构造函数
// 下面给出可编译的对照程序:
#include <iostream>
#include <cstdlib>
#include <cstring>
class Person {
public:
Person() { std::cout << "构造函数!" << std::endl; }
void Init() { std::cout << "Init" << std::endl; }
};
int main() {
Person* person = new Person; // C++: new 自动调用构造函数
Person* p2 = (Person*)malloc(sizeof(Person));
if (p2) { new (p2) Person(); } // 对齐 new 的行为(placement new
delete person;
if (p2) { p2->~Person(); free(p2); }
return 0;
}
// --- main ---
+54
View File
@@ -0,0 +1,54 @@
// ============================================================================
// p1c04_03_15 — 4.3.10.2 静态成员函数
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:14] 4.3.10.2 静态成员函数 ---
class Person{
public:
//普通成员函数可以访问static和non-static成员属性
void changeParam1(int param){
mParam = param;
sNum = param;
}
//静态成员函数只能访问static成员属性
static void changeParam2(int param){
//mParam = param; //无法访问
sNum = param;
}
private:
static void changeParam3(int param){
//mParam = param; //无法访问
sNum = param;
}
public:
int mParam;
static int sNum;
};
//静态成员属性类外初始化
int Person::sNum = 0;
int main(){
//1. 类名直接调用
Person::changeParam2(100);
//2. 通过对象调用
Person p;
p.changeParam2(200);
//3. 静态成员函数也有访问权限
//Person::changeParam3(100); //类外无法访问私有静态成员函数
//Person p1;
//p1.changeParam3(200);
return EXIT_SUCCESS;
}
// --- main ---
+50
View File
@@ -0,0 +1,50 @@
// ============================================================================
// p1c04_03_14 — 4.3.10.1 静态成员变量
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954451:13] 4.3.10.1 静态成员变量 ---
class Person{
public:
//类的静态成员属性
static int sNum;
private:
static int sOther;
};
//类外初始化,初始化时不加static
int Person::sNum = 0;
int Person::sOther = 0;
int main(){
//1. 通过类名直接访问
Person::sNum = 100;
cout << "Person::sNum:" << Person::sNum << endl;
//2. 通过对象访问
Person p1, p2;
p1.sNum = 200;
cout << "p1.sNum:" << p1.sNum << endl;
cout << "p2.sNum:" << p2.sNum << endl;
//3. 静态成员也有访问权限,类外不能访问私有成员
//cout << "Person::sOther:" << Person::sOther << endl;
Person p3;
//cout << "p3.sOther:" << p3.sOther << endl;
pause();
return EXIT_SUCCESS;
}
// --- main ---
+43
View File
@@ -0,0 +1,43 @@
// ============================================================================
// p1c04_03_17 — 4.3.10.4 静态成员实现单例模式
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954451:16] 4.3.10.4 静态成员实现单例模式 ---
class Printer{
public:
static Printer* getInstance(){ return pPrinter;}
void PrintText(string text){
cout << "打印内容:" << text << endl;
cout << "已打印次数:" << mTimes << endl;
cout << "--------------" << endl;
mTimes++;
}
private:
Printer(){ mTimes = 0; }
Printer(const Printer&){}
private:
static Printer* pPrinter;
int mTimes;
};
Printer* Printer::pPrinter = new Printer;
void test(){
Printer* printer = Printer::getInstance();
printer->PrintText("离职报告!");
printer->PrintText("入职合同!");
printer->PrintText("提交代码!");
}
// --- main ---
int main() {
test();
return 0;
}
+19
View File
@@ -0,0 +1,19 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch04/s04
add_executable(p1c04_04_01 member_storage.cpp)
set_target_properties(p1c04_04_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_04_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_04_01 PRIVATE -Wall -Wextra)
add_executable(p1c04_04_02 this_pointer.cpp)
set_target_properties(p1c04_04_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_04_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_04_02 PRIVATE -Wall -Wextra)
add_executable(p1c04_04_03 const_member_function.cpp)
set_target_properties(p1c04_04_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_04_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_04_03 PRIVATE -Wall -Wextra)
add_executable(p1c04_04_04 const_object.cpp)
set_target_properties(p1c04_04_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_04_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_04_04 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch04_s04 DEPENDS p1c04_04_01 p1c04_04_02 p1c04_04_03 p1c04_04_04)
+46
View File
@@ -0,0 +1,46 @@
// ============================================================================
// p1c04_04_03 — 4.4.2.3 const修饰成员函数
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954456:2] 4.4.2.3 const修饰成员函数 ---
//const修饰成员函数
class Person{
public:
Person(){
this->mAge = 0;
this->mID = 0;
}
//在函数括号后面加上const,修饰成员变量不可修改,除了mutable变量
void sonmeOperate() const{
//this->mAge = 200; //mAge不可修改
this->mID = 10;
}
void ShowPerson(){
cout << "ID:" << mID << " mAge:" << mAge << endl;
}
private:
int mAge;
mutable int mID;
};
int main(){
Person person;
person.sonmeOperate();
person.ShowPerson();
pause();
return EXIT_SUCCESS;
}
// --- main ---
+48
View File
@@ -0,0 +1,48 @@
// ============================================================================
// p1c04_04_04 — 4.4.2.4 const修饰对象(常对象)
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954456:3] 4.4.2.4 const修饰对象(常对象) ---
class Person{
public:
Person(){
this->mAge = 0;
this->mID = 0;
}
void ChangePerson() const{
//mAge = 100; // 非法:const 成员函数内不能修改非 mutable 成员
mID = 100; // 合法:mID 被 mutable 修饰,const 函数内可改
}
void ShowPerson(){
this->mAge = 1000;
cout << "ID:" << this->mID << " Age:" << this->mAge << endl;
}
public:
int mAge;
mutable int mID;
};
void test(){
const Person person;
//1. 可访问数据成员
cout << "Age:" << person.mAge << endl;
//person.mAge = 300; //不可修改
person.mID = 1001; //但是可以修改mutable修饰的成员变量
//2. 只能访问const修饰的函数
//person.ShowPerson();
person.ChangePerson();
}
// --- main ---
int main() {
test();
return 0;
}
+65
View File
@@ -0,0 +1,65 @@
// ============================================================================
// p1c04_04_01 — 4.4.1 成员变量和函数的存储
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954456:0] 4.4.1 成员变量和函数的存储 ---
class MyClass01{
public:
int mA;
};
class MyClass02{
public:
int mA;
static int sB;
};
class MyClass03{
public:
void printMyClass_1(){
cout << "hello world!" << endl;
}
public:
int mA;
static int sB;
};
class MyClass04{
public:
void printMyClass_1(){
cout << "hello world!" << endl;
}
static void ShowMyClass(){
cout << "hello world" << endl;
}
public:
int mA;
static int sB;
};
int main(){
MyClass01 mclass01;
MyClass02 mclass02;
MyClass03 mclass03;
MyClass04 mclass04;
cout << "MyClass01:" << sizeof(mclass01) << endl; //4
//静态数据成员并不保存在类对象中
cout << "MyClass02:" << sizeof(mclass02) << endl; //4
//非静态成员函数不保存在类对象中
cout << "MyClass03:" << sizeof(mclass03) << endl; //4
//静态成员函数也不保存在类对象中
cout << "MyClass04:" << sizeof(mclass04) << endl; //4
return EXIT_SUCCESS;
}
// --- main ---
+71
View File
@@ -0,0 +1,71 @@
// ============================================================================
// p1c04_04_02 — 4.4.2.2 this指针的使用
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954456:1] 4.4.2.2 this指针的使用 ---
class Person{
public:
//1. 当形参名和成员变量名一样时,this指针可用来区分
Person(string name,int age){
//name = name;
//age = age; //输出错误
this->name = name;
this->age = age;
}
//2. 返回对象本身的引用
//重载赋值操作符
//其实也是两个参数,其中隐藏了一个this指针
Person PersonPlusPerson(Person& person){
string newname = this->name + person.name;
int newage = this->age + person.age;
Person newperson(newname, newage);
return newperson;
}
void ShowPerson(){
cout << "Name:" << name << " Age:" << age << endl;
}
public:
string name;
int age;
};
//3. 成员函数和全局函数(Perosn对象相加)
Person PersonPlusPerson(Person& p1,Person& p2){
string newname = p1.name + p2.name;
int newage = p1.age + p2.age;
Person newperson(newname,newage);
return newperson;
}
int main(){
Person person("John",100);
person.ShowPerson();
cout << "---------" << endl;
Person person1("John",20);
Person person2("001", 10);
//1.全局函数实现两个对象相加
Person person3 = PersonPlusPerson(person1, person2);
person1.ShowPerson();
person2.ShowPerson();
person3.ShowPerson();
//2. 成员函数实现两个对象相加
Person person4 = person1.PersonPlusPerson(person2);
person4.ShowPerson();
pause();
return EXIT_SUCCESS;
}
// --- main ---
+7
View File
@@ -0,0 +1,7 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch04/s05
add_executable(p1c04_05_01 friend_syntax.cpp)
set_target_properties(p1c04_05_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_05_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_05_01 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch04_s05 DEPENDS p1c04_05_01)
+73
View File
@@ -0,0 +1,73 @@
// ============================================================================
// p1c04_05_01 — 4.5.1 友元语法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954457:0] 4.5.1 友元语法 ---
class Building;
//友元类
class MyFriend{
public:
//友元成员函数
void LookAtBedRoom(Building& building);
void PlayInBedRoom(Building& building);
};
class Building{
//全局函数做友元函数
friend void CleanBedRoom(Building& building);
#if 0
//成员函数做友元函数
friend void MyFriend::LookAtBedRoom(Building& building);
friend void MyFriend::PlayInBedRoom(Building& building);
#else
//友元类
friend class MyFriend;
#endif
public:
Building();
public:
string mSittingRoom;
private:
string mBedroom;
};
void MyFriend::LookAtBedRoom(Building& building){
cout << "我的朋友参观" << building.mBedroom << endl;
}
void MyFriend::PlayInBedRoom(Building& building){
cout << "我的朋友玩耍在" << building.mBedroom << endl;
}
//友元全局函数
void CleanBedRoom(Building& building){
cout << "友元全局函数访问" << building.mBedroom << endl;
}
Building::Building(){
this->mSittingRoom = "客厅";
this->mBedroom = "卧室";
}
int main(){
Building building;
MyFriend myfriend;
CleanBedRoom(building);
myfriend.LookAtBedRoom(building);
myfriend.PlayInBedRoom(building);
pause();
return EXIT_SUCCESS;
}
// --- main ---
+27
View File
@@ -0,0 +1,27 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch04/s06
add_executable(p1c04_06_01 overload_with_friend.cpp)
set_target_properties(p1c04_06_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_06_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_06_01 PRIVATE -Wall -Wextra)
add_executable(p1c04_06_02 overload_increment_decrement.cpp)
set_target_properties(p1c04_06_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_06_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_06_02 PRIVATE -Wall -Wextra)
add_executable(p1c04_06_03 overload_pointer_operator.cpp)
set_target_properties(p1c04_06_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_06_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_06_03 PRIVATE -Wall -Wextra)
add_executable(p1c04_06_04 overload_assignment.cpp)
set_target_properties(p1c04_06_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_06_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_06_04 PRIVATE -Wall -Wextra)
add_executable(p1c04_06_05 overload_equality.cpp)
set_target_properties(p1c04_06_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_06_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_06_05 PRIVATE -Wall -Wextra)
add_executable(p1c04_06_06 overload_function_call.cpp)
set_target_properties(p1c04_06_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_06_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_06_06 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch04_s06 DEPENDS p1c04_06_01 p1c04_06_02 p1c04_06_03 p1c04_06_04 p1c04_06_05 p1c04_06_06)
+114
View File
@@ -0,0 +1,114 @@
// ============================================================================
// p1c04_06_04 — 4.6.6 赋值(=)运算符重载
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954458:3] 4.6.6 赋值(=)运算符重载 ---
class Person{
friend ostream& operator<<(ostream& os,const Person& person){
os << "ID:" << person.mID << " Age:" << person.mAge << endl;
return os;
}
public:
Person(int id,int age){
this->mID = id;
this->mAge = age;
}
//重载赋值运算符
Person& operator=(const Person& person){
this->mID = person.mID;
this->mAge = person.mAge;
return *this;
}
private:
int mID;
int mAge;
};
//1. =号混淆的地方
void test01(){
Person person1(10, 20);
Person person2 = person1; //调用拷贝构造
//如果一个对象还没有被创建,则必须初始化,也就是调用构造函数
//上述例子由于person2还没有初始化,所以会调用构造函数
//由于person2是从已有的person1来创建的,所以只有一个选择
//就是调用拷贝构造函数
person2 = person1; //调用operator=函数
//由于person2已经创建,不需要再调用构造函数,这时候调用的是重载的赋值运算符
}
//2. 赋值重载案例
void test02(){
Person person1(20, 20);
Person person2(30, 30);
cout << "person1:" << person1;
cout << "person2:" << person2;
person2 = person1;
cout << "person2:" << person2;
}
//常见错误,当准备给两个相同对象赋值时,应该首先检查一下这个对象是否对自身赋值了
//对于本例来讲,无论如何执行这些赋值运算都是无害的,但如果对类的实现进行修改,那么将会出现差异;
//3. 类中指针
class Person2{
friend ostream& operator<<(ostream& os, const Person2& person){
os << "Name:" << person.pName << " ID:" << person.mID << " Age:" << person.mAge << endl;
return os;
}
public:
Person2(char* name,int id, int age){
this->pName = new char[strlen(name) + 1];
strcpy(this->pName, name);
this->mID = id;
this->mAge = age;
}
#if 1
//重载赋值运算符
Person2& operator=(const Person2& person){
//注意:由于当前对象已经创建完毕,那么就有可能pName指向堆内存
//这个时候如果直接赋值,会导致内存没有及时释放
if (this->pName != NULL){
delete[] this->pName;
}
this->pName = new char[strlen(person.pName) + 1];
strcpy(this->pName,person.pName);
this->mID = person.mID;
this->mAge = person.mAge;
return *this;
}
#endif
//析构函数
~Person2(){
if (this->pName != NULL){
delete[] this->pName;
}
}
private:
char* pName;
int mID;
int mAge;
};
void test03(){
Person2 person1("John",20, 20);
Person2 person2("Edward",30, 30);
cout << "person1:" << person1;
cout << "person2:" << person2;
person2 = person1;
cout << "person2:" << person2;
}
// --- main ---
int main() {
test01();
test02();
test03();
return 0;
}
+61
View File
@@ -0,0 +1,61 @@
// ============================================================================
// p1c04_06_05 — 4.6.7 等于和不等于(==、!=)运算符重载
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <cstring>
// --- 原文片段 [58954458:4] 4.6.7 等于和不等于(==、!=)运算符重载 ---
class Complex{
public:
Complex(char* name,int id,int age){
this->pName = new char[strlen(name) + 1];
strcpy(this->pName, name);
this->mID = id;
this->mAge = age;
}
//重载==号操作符
bool operator==(const Complex& complex){
if (strcmp(this->pName,complex.pName) == 0 &&
this->mID == complex.mID &&
this->mAge == complex.mAge){
return true;
}
return false;
}
//重载!=操作符
bool operator!=(const Complex& complex){
if (strcmp(this->pName, complex.pName) != 0 ||
this->mID != complex.mID ||
this->mAge != complex.mAge){
return true;
}
return false;
}
~Complex(){
if (this->pName != NULL){
delete[] this->pName;
}
}
private:
char* pName;
int mID;
int mAge;
};
void test(){
Complex complex1("aaa", 10, 20);
Complex complex2("bbb", 10, 20);
if (complex1 == complex2){ cout << "相等!" << endl; }
if (complex1 != complex2){ cout << "不相等!" << endl; }
}
// --- main ---
int main() {
test();
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
// ============================================================================
// p1c04_06_06 — 4.6.8 函数调用符号()重载
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954458:5] 4.6.8 函数调用符号()重载 ---
class Complex{
public:
int Add(int x,int y){
return x + y;
}
int operator()(int x,int y){
return x + y;
}
};
void test01(){
Complex complex;
cout << complex.Add(10,20) << endl;
//对象当做函数来调用
cout << complex(10, 20) << endl;
}
// --- main ---
int main() {
test01();
return 0;
}
@@ -0,0 +1,85 @@
// ============================================================================
// p1c04_06_02 — 4.6.4 自增自减(++/--)运算符重载
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954458:1] 4.6.4 自增自减(++/--)运算符重载 ---
class Complex{
friend ostream& operator<<(ostream& os,Complex& complex){
os << "A:" << complex.mA << " B:" << complex.mB << endl;
return os;
}
public:
Complex(){
mA = 0;
mB = 0;
}
//重载前置++
Complex& operator++(){
mA++;
mB++;
return *this;
}
//重载后置++
Complex operator++(int){
Complex temp;
temp.mA = this->mA;
temp.mB = this->mB;
mA++;
mB++;
return temp;
}
//前置--
Complex& operator--(){
mA--;
mB--;
return *this;
}
//后置--
Complex operator--(int){
Complex temp;
temp.mA = mA;
temp.mB = mB;
mA--;
mB--;
return temp;
}
void ShowComplex(){
cout << "A:" << mA << " B:" << mB << endl;
}
private:
int mA;
int mB;
};
void test(){
Complex complex;
complex++;
cout << complex;
++complex;
cout << complex;
Complex ret = complex++;
cout << ret;
cout << complex;
cout << "------" << endl;
ret--;
--ret;
cout << "ret:" << ret;
complex--;
--complex;
cout << "complex:" << complex;
}
// --- main ---
int main() {
test();
return 0;
}
@@ -0,0 +1,59 @@
// ============================================================================
// p1c04_06_03 — 4.6.5 指针运算符(*、->)重载
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954458:2] 4.6.5 指针运算符(*、->)重载 ---
class Person{
public:
Person(int param){
this->mParam = param;
}
void PrintPerson(){
cout << "Param:" << mParam << endl;
}
private:
int mParam;
};
class SmartPointer{
public:
SmartPointer(Person* person){
this->pPerson = person;
}
//重载指针的->、*操作符
Person* operator->(){
return pPerson;
}
Person& operator*(){
return *pPerson;
}
~SmartPointer(){
if (pPerson != NULL){
delete pPerson;
}
}
public:
Person* pPerson;
};
void test01(){
//Person* person = new Person(100);
//如果忘记释放,那么就会造成内存泄漏
SmartPointer pointer(new Person(100));
pointer->PrintPerson();
}
// --- main ---
int main() {
test01();
return 0;
}
+39
View File
@@ -0,0 +1,39 @@
// ============================================================================
// p1c04_06_01 — 4.6.2 运算符重载碰上友元函数
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954458:0] 4.6.2 运算符重载碰上友元函数 ---
class Person{
friend ostream& operator<<(ostream& os, Person& person);
public:
Person(int id,int age){
mID = id;
mAge = age;
}
private:
int mID;
int mAge;
};
ostream& operator<<(ostream& os, Person& person){
os << "ID:" << person.mID << " Age:" << person.mAge;
return os;
}
int main(){
Person person(1001, 30);
//cout << person; //cout.operator+(person)
cout << person << " | " << endl;
return EXIT_SUCCESS;
}
// --- main ---
+55
View File
@@ -0,0 +1,55 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch04/s07
add_executable(p1c04_07_01 why_inheritance.cpp)
set_target_properties(p1c04_07_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_01 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_02 derived_access_control.cpp)
set_target_properties(p1c04_07_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_02 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_03 inheritance_object_model.cpp)
set_target_properties(p1c04_07_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_03 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_04 inheritance_ctor_dtor_order.cpp)
set_target_properties(p1c04_07_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_04 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_05 inheritance_ctor_dtor_order_2.cpp)
set_target_properties(p1c04_07_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_05 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_06 same_name_members.cpp)
set_target_properties(p1c04_07_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_06 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_07 same_name_members_2.cpp)
set_target_properties(p1c04_07_07 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_07 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_07 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_08 static_in_inheritance.cpp)
set_target_properties(p1c04_07_08 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_08 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_08 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_09 multiple_inheritance_concept.cpp)
set_target_properties(p1c04_07_09 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_09 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_09 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_10 diamond_virtual_inheritance.cpp)
set_target_properties(p1c04_07_10 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_10 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_10 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_11 diamond_virtual_inheritance_2.cpp)
set_target_properties(p1c04_07_11 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_11 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_11 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_12 virtual_inheritance_mechanism.cpp)
set_target_properties(p1c04_07_12 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_12 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_12 PRIVATE -Wall -Wextra)
add_executable(p1c04_07_13 virtual_inheritance_mechanism_2.cpp)
set_target_properties(p1c04_07_13 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c04_07_13 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c04_07_13 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch04_s07 DEPENDS p1c04_07_01 p1c04_07_02 p1c04_07_03 p1c04_07_04 p1c04_07_05 p1c04_07_06 p1c04_07_07 p1c04_07_08 p1c04_07_09 p1c04_07_10 p1c04_07_11 p1c04_07_12 p1c04_07_13)
+98
View File
@@ -0,0 +1,98 @@
// ============================================================================
// p1c04_07_02 — 4.7.2 派生类访问控制
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:1] 4.7.2 派生类访问控制 ---
//基类
class A{
public:
int mA;
protected:
int mB;
private:
int mC;
};
//1. 公有(public)继承
class B : public A{
public:
void PrintB(){
cout << mA << endl; //可访问基类public属性
cout << mB << endl; //可访问基类protected属性
//cout << mC << endl; //不可访问基类private属性
}
};
class SubB : public B{
void PrintSubB(){
cout << mA << endl; //可访问基类public属性
cout << mB << endl; //可访问基类protected属性
//cout << mC << endl; //不可访问基类private属性
}
};
void test01(){
B b;
cout << b.mA << endl; //可访问基类public属性
//cout << b.mB << endl; //不可访问基类protected属性
//cout << b.mC << endl; //不可访问基类private属性
}
//2. 私有(private)继承
class C : private A{
public:
void PrintC(){
cout << mA << endl; //可访问基类public属性
cout << mB << endl; //可访问基类protected属性
//cout << mC << endl; //不可访问基类private属性
}
};
class SubC : public C{
void PrintSubC(){
//cout << mA << endl; //不可访问基类public属性
//cout << mB << endl; //不可访问基类protected属性
//cout << mC << endl; //不可访问基类private属性
}
};
void test02(){
C c;
//cout << c.mA << endl; //不可访问基类public属性
//cout << c.mB << endl; //不可访问基类protected属性
//cout << c.mC << endl; //不可访问基类private属性
}
//3. 保护(protected)继承
class D : protected A{
public:
void PrintD(){
cout << mA << endl; //可访问基类public属性
cout << mB << endl; //可访问基类protected属性
//cout << mC << endl; //不可访问基类private属性
}
};
class SubD : public D{
void PrintD(){
cout << mA << endl; //可访问基类public属性
cout << mB << endl; //可访问基类protected属性
//cout << mC << endl; //不可访问基类private属性
}
};
void test03(){
D d;
//cout << d.mA << endl; //不可访问基类public属性
//cout << d.mB << endl; //不可访问基类protected属性
//cout << d.mC << endl; //不可访问基类private属性
}
// --- main ---
int main() {
test01();
test02();
test03();
return 0;
}
@@ -0,0 +1,40 @@
// ============================================================================
// p1c04_07_10 — 4.7.6.2 菱形继承和虚继承
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:9] 4.7.6.2 菱形继承和虚继承 ---
class BigBase{
public:
BigBase(){ mParam = 0; }
void func(){ cout << "BigBase::func" << endl; }
public:
int mParam;
};
class Base1 : public BigBase{};
class Base2 : public BigBase{};
class Derived : public Base1, public Base2{};
int main(){
Derived derived;
//1. 对"func"的访问不明确
//derived.func();
//cout << derived.mParam << endl;
cout << "derived.Base1::mParam:" << derived.Base1::mParam << endl;
cout << "derived.Base2::mParam:" << derived.Base2::mParam << endl;
//2. 重复继承
cout << "Derived size:" << sizeof(Derived) << endl; //8
return EXIT_SUCCESS;
}
// --- main ---
@@ -0,0 +1,37 @@
// ============================================================================
// p1c04_07_11 — 4.7.6.2 菱形继承和虚继承
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:10] 4.7.6.2 菱形继承和虚继承 ---
class BigBase{
public:
BigBase(){ mParam = 0; }
void func(){ cout << "BigBase::func" << endl; }
public:
int mParam;
};
class Base1 : virtual public BigBase{};
class Base2 : virtual public BigBase{};
class Derived : public Base1, public Base2{};
int main(){
Derived derived;
//二义性问题解决
derived.func();
cout << derived.mParam << endl;
//输出结果:12
cout << "Derived size:" << sizeof(Derived) << endl;
return EXIT_SUCCESS;
}
// --- main ---
@@ -0,0 +1,51 @@
// ============================================================================
// p1c04_07_04 — 4.7.3.2 对象构造和析构的调用原则
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:3] 4.7.3.2 对象构造和析构的调用原则 ---
class A{
public:
A(){
cout << "A类构造函数!" << endl;
}
~A(){
cout << "A类析构函数!" << endl;
}
};
class B : public A{
public:
B(){
cout << "B类构造函数!" << endl;
}
~B(){
cout << "B类析构函数!" << endl;
}
};
class C : public B{
public:
C(){
cout << "C类构造函数!" << endl;
}
~C(){
cout << "C类析构函数!" << endl;
}
};
void test(){
C c;
}
// --- main ---
int main() {
test();
return 0;
}
@@ -0,0 +1,59 @@
// ============================================================================
// p1c04_07_05 — 4.7.3.2 对象构造和析构的调用原则
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:4] 4.7.3.2 对象构造和析构的调用原则 ---
class D{
public:
D(){
cout << "D类构造函数!" << endl;
}
~D(){
cout << "D类析构函数!" << endl;
}
};
class A{
public:
A(){
cout << "A类构造函数!" << endl;
}
~A(){
cout << "A类析构函数!" << endl;
}
};
class B : public A{
public:
B(){
cout << "B类构造函数!" << endl;
}
~B(){
cout << "B类析构函数!" << endl;
}
};
class C : public B{
public:
C(){
cout << "C类构造函数!" << endl;
}
~C(){
cout << "C类析构函数!" << endl;
}
public:
D c;
};
void test(){
C c;
}
// --- main ---
int main() {
test();
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
// ============================================================================
// p1c04_07_03 — 4.7.3.1 继承中的对象模型
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:2] 4.7.3.1 继承中的对象模型 ---
class Aclass{
public:
int mA;
int mB;
};
class Bclass : public Aclass{
public:
int mC;
};
class Cclass : public Bclass{
public:
int mD;
};
void test(){
cout << "A size:" << sizeof(Aclass) << endl;
cout << "B size:" << sizeof(Bclass) << endl;
cout << "C size:" << sizeof(Cclass) << endl;
}
// --- main ---
int main() {
test();
return 0;
}
@@ -0,0 +1,38 @@
// ============================================================================
// p1c04_07_09 — 4.7.6.1 多继承概念
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:8] 4.7.6.1 多继承概念 ---
class Base1{
public:
void func1_1(){ cout << "Base1::func1_1" << endl; }
};
class Base2{
public:
void func1_1(){ cout << "Base2::func1_1" << endl; }
void func2(){ cout << "Base2::func2" << endl; }
};
//派生类继承Base1、Base2
class Derived : public Base1, public Base2{};
int main(){
Derived derived;
//func1是从Base1继承来的还是从Base2继承来的?
//derived.func1_1();
derived.func2();
//解决歧义:显示指定调用那个基类的func1
derived.Base1::func1_1();
derived.Base2::func1_1();
return EXIT_SUCCESS;
}
// --- main ---
+48
View File
@@ -0,0 +1,48 @@
// ============================================================================
// p1c04_07_06 — 4.7.3 继承中同名成员的处理方法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954461:5] 4.7.3 继承中同名成员的处理方法 ---
class Base{
public:
Base():mParam(0){}
void Print(){ cout << mParam << endl; }
public:
int mParam;
};
class Derived : public Base{
public:
Derived():mParam(10){}
void Print(){
//在派生类中使用和基类的同名成员,显示使用类名限定符
cout << Base::mParam << endl;
cout << mParam << endl;
}
//返回基类重名成员
int& getBaseParam(){ return Base::mParam; }
public:
int mParam;
};
int main(){
Derived derived;
//派生类和基类成员属性重名,子类访问成员默认是子类成员
cout << derived.mParam << endl; //10
derived.Print();
//类外如何获得基类重名成员属性
derived.getBaseParam() = 100;
cout << "Base:mParam:" << derived.getBaseParam() << endl;
return EXIT_SUCCESS;
}
// --- main ---

Some files were not shown because too many files have changed in this diff Show More