├── 04、封装
├── 04_01_属性和行为
│ ├── 04_01_属性和行为.sln
│ └── 04_01_属性和行为
│ │ ├── 04_01_属性和行为.vcxproj
│ │ ├── 04_01_属性和行为.vcxproj.filters
│ │ ├── 04_01_属性和行为.vcxproj.user
│ │ └── main.cpp
├── 04_02_访问权限
│ ├── 04_02_访问权限.sln
│ └── 04_02_访问权限
│ │ ├── 04_02_访问权限.vcxproj
│ │ ├── 04_02_访问权限.vcxproj.filters
│ │ ├── 04_02_访问权限.vcxproj.user
│ │ └── main.cpp
├── 04_03_class和struct
│ ├── 04_03_class和struct.sln
│ └── 04_03_class和struct
│ │ ├── 04_03_class和struct.vcxproj
│ │ ├── 04_03_class和struct.vcxproj.filters
│ │ ├── 04_03_class和struct.vcxproj.user
│ │ └── main.cpp
└── 04_04_属性私有化
│ ├── 04_04_属性私有化.sln
│ └── 04_04_属性私有化
│ ├── 04_04_属性私有化.vcxproj
│ ├── 04_04_属性私有化.vcxproj.filters
│ ├── 04_04_属性私有化.vcxproj.user
│ └── main.cpp
├── 05、对象特性
├── 05_01_构造函数
│ ├── 05_01_构造函数.sln
│ └── 05_01_构造函数
│ │ ├── 05_01_构造函数.vcxproj
│ │ ├── 05_01_构造函数.vcxproj.filters
│ │ ├── 05_01_构造函数.vcxproj.user
│ │ └── main.cpp
├── 05_02_析构函数
│ ├── 05_02_析构函数.sln
│ └── 05_02_析构函数
│ │ ├── 05_02_析构函数.vcxproj
│ │ ├── 05_02_析构函数.vcxproj.filters
│ │ ├── 05_02_析构函数.vcxproj.user
│ │ └── main.cpp
├── 05_03_拷贝构造函数
│ ├── 05_03_拷贝构造函数.sln
│ └── 05_03_拷贝构造函数
│ │ ├── 05_03_拷贝构造函数.vcxproj
│ │ ├── 05_03_拷贝构造函数.vcxproj.filters
│ │ ├── 05_03_拷贝构造函数.vcxproj.user
│ │ └── main.cpp
├── 05_04_初始化列表
│ ├── 05_04_初始化列表.sln
│ └── 05_04_初始化列表
│ │ ├── 05_04_初始化列表.vcxproj
│ │ ├── 05_04_初始化列表.vcxproj.filters
│ │ ├── 05_04_初始化列表.vcxproj.user
│ │ └── main.cpp
├── 05_05_静态成员变量
│ ├── 05_05_静态成员变量.sln
│ └── 05_05_静态成员变量
│ │ ├── 05_05_静态成员变量.vcxproj
│ │ ├── 05_05_静态成员变量.vcxproj.filters
│ │ ├── 05_05_静态成员变量.vcxproj.user
│ │ └── main.cpp
├── 05_06_静态成员函数
│ ├── 05_06_静态成员函数.sln
│ └── 05_06_静态成员函数
│ │ ├── 05_06_静态成员函数.vcxproj
│ │ ├── 05_06_静态成员函数.vcxproj.filters
│ │ ├── 05_06_静态成员函数.vcxproj.user
│ │ └── main.cpp
├── 05_07_this指针
│ ├── 05_07_this指针.sln
│ └── 05_07_this指针
│ │ ├── 05_07_this指针.vcxproj
│ │ ├── 05_07_this指针.vcxproj.filters
│ │ ├── 05_07_this指针.vcxproj.user
│ │ └── main.cpp
├── 05_08_const修饰成员函数
│ ├── 05_08_const修饰成员函数.sln
│ └── 05_08_const修饰成员函数
│ │ ├── 05_08_const修饰成员函数.vcxproj
│ │ ├── 05_08_const修饰成员函数.vcxproj.filters
│ │ ├── 05_08_const修饰成员函数.vcxproj.user
│ │ └── main.cpp
└── 05_09_mutable关键字
│ ├── 05_09_mutable关键字.sln
│ └── 05_09_mutable关键字
│ ├── 05_09_mutable关键字.vcxproj
│ ├── 05_09_mutable关键字.vcxproj.filters
│ ├── 05_09_mutable关键字.vcxproj.user
│ └── main.cpp
├── 06、友元
├── 06_01_全局函数作为友元
│ ├── 06_01_全局函数作为友元.sln
│ └── 06_01_全局函数作为友元
│ │ ├── 06_01_全局函数作为友元.vcxproj
│ │ ├── 06_01_全局函数作为友元.vcxproj.filters
│ │ ├── 06_01_全局函数作为友元.vcxproj.user
│ │ └── main.cpp
├── 06_02_类作为友元
│ ├── 06_02_类作为友元.sln
│ └── 06_02_类作为友元
│ │ ├── 06_02_类作为友元.vcxproj
│ │ ├── 06_02_类作为友元.vcxproj.filters
│ │ ├── 06_02_类作为友元.vcxproj.user
│ │ └── main.cpp
└── 06_03_成员函数作为友元
│ ├── 06_03_成员函数作为友元.sln
│ └── 06_03_成员函数作为友元
│ ├── 06_03_成员函数作为友元.vcxproj
│ ├── 06_03_成员函数作为友元.vcxproj.filters
│ ├── 06_03_成员函数作为友元.vcxproj.user
│ └── main.cpp
├── 07、运算符重载
├── 07_01_运算符重载概念
│ ├── 07_01_运算符重载概念.sln
│ └── 07_01_运算符重载概念
│ │ ├── 07_01_运算符重载概念.vcxproj
│ │ ├── 07_01_运算符重载概念.vcxproj.filters
│ │ ├── 07_01_运算符重载概念.vcxproj.user
│ │ └── main.cpp
├── 07_02_加号重载
│ ├── 07_02_加号重载.sln
│ └── 07_02_加号重载
│ │ ├── 07_02_加号重载.vcxproj
│ │ ├── 07_02_加号重载.vcxproj.filters
│ │ ├── 07_02_加号重载.vcxproj.user
│ │ └── main.cpp
├── 07_03_左移重载
│ ├── 07_03_左移重载.sln
│ └── 07_03_左移重载
│ │ ├── 07_03_左移重载.vcxproj
│ │ ├── 07_03_左移重载.vcxproj.filters
│ │ ├── 07_03_左移重载.vcxproj.user
│ │ └── main.cpp
├── 07_04_递增运算符
│ ├── 07_04_递增运算符.sln
│ └── 07_04_递增运算符
│ │ ├── 07_04_递增运算符.vcxproj
│ │ ├── 07_04_递增运算符.vcxproj.filters
│ │ ├── 07_04_递增运算符.vcxproj.user
│ │ └── main.cpp
├── 07_05_赋值重载
│ ├── 07_05_赋值重载.sln
│ └── 07_05_赋值重载
│ │ ├── 07_05_赋值重载.vcxproj
│ │ ├── 07_05_赋值重载.vcxproj.filters
│ │ ├── 07_05_赋值重载.vcxproj.user
│ │ └── main.cpp
├── 07_06_关系运算重载
│ ├── 07_06_关系运算重载.sln
│ └── 07_06_关系运算重载
│ │ ├── 07_06_关系运算重载.vcxproj
│ │ ├── 07_06_关系运算重载.vcxproj.filters
│ │ ├── 07_06_关系运算重载.vcxproj.user
│ │ └── main.cpp
└── 07_07_函数调用运算符重载
│ ├── 07_07_函数调用运算符重载.sln
│ └── 07_07_函数调用运算符重载
│ ├── 07_07_函数调用运算符重载.vcxproj
│ ├── 07_07_函数调用运算符重载.vcxproj.filters
│ ├── 07_07_函数调用运算符重载.vcxproj.user
│ └── main.cpp
├── 08、继承
├── 08_01_继承的语法
│ ├── 08_01_继承的语法.sln
│ └── 08_01_继承的语法
│ │ ├── 08_01_继承的语法.vcxproj
│ │ ├── 08_01_继承的语法.vcxproj.filters
│ │ ├── 08_01_继承的语法.vcxproj.user
│ │ └── main.cpp
├── 08_02_继承方式
│ ├── 08_02_继承方式.sln
│ └── 08_02_继承方式
│ │ ├── 08_02_继承方式.vcxproj
│ │ ├── 08_02_继承方式.vcxproj.filters
│ │ ├── 08_02_继承方式.vcxproj.user
│ │ └── main.cpp
├── 08_03_构造和析构顺序
│ ├── 08_03_构造和析构顺序.sln
│ └── 08_03_构造和析构顺序
│ │ ├── 08_03_构造和析构顺序.vcxproj
│ │ ├── 08_03_构造和析构顺序.vcxproj.filters
│ │ ├── 08_03_构造和析构顺序.vcxproj.user
│ │ └── main.cpp
├── 08_04_同名属性访问
│ ├── 08_04_同名属性访问.sln
│ └── 08_04_同名属性访问
│ │ ├── 08_04_同名属性访问.vcxproj
│ │ ├── 08_04_同名属性访问.vcxproj.filters
│ │ ├── 08_04_同名属性访问.vcxproj.user
│ │ └── main.cpp
├── 08_05_同名函数访问
│ ├── 08_05_同名函数访问.sln
│ └── 08_05_同名函数访问
│ │ ├── 08_05_同名函数访问.vcxproj
│ │ ├── 08_05_同名函数访问.vcxproj.filters
│ │ ├── 08_05_同名函数访问.vcxproj.user
│ │ └── main.cpp
└── 08_06_多继承
│ ├── 08_06_多继承.sln
│ └── 08_06_多继承
│ ├── 08_06_多继承.vcxproj
│ ├── 08_06_多继承.vcxproj.filters
│ ├── 08_06_多继承.vcxproj.user
│ └── main.cpp
├── 09、多态
├── 09_01_多态的语法
│ ├── 09_01_多态的语法.sln
│ └── 09_01_多态的语法
│ │ ├── 09_01_多态的语法.vcxproj
│ │ ├── 09_01_多态的语法.vcxproj.filters
│ │ ├── 09_01_多态的语法.vcxproj.user
│ │ └── main.cpp
├── 09_02_虚函数
│ ├── 09_02_虚函数.sln
│ └── 09_02_虚函数
│ │ ├── 09_02_虚函数.vcxproj
│ │ ├── 09_02_虚函数.vcxproj.filters
│ │ ├── 09_02_虚函数.vcxproj.user
│ │ └── main.cpp
├── 09_03_纯虚函数和抽象类
│ ├── 09_03_纯虚函数和抽象类.sln
│ └── 09_03_纯虚函数和抽象类
│ │ ├── 09_03_纯虚函数和抽象类.vcxproj
│ │ ├── 09_03_纯虚函数和抽象类.vcxproj.filters
│ │ ├── 09_03_纯虚函数和抽象类.vcxproj.user
│ │ └── main.cpp
└── 09_04_虚析构和纯虚析构
│ ├── 09_04_虚析构和纯虚析构.sln
│ └── 09_04_虚析构和纯虚析构
│ ├── 09_04_虚析构和纯虚析构.vcxproj
│ ├── 09_04_虚析构和纯虚析构.vcxproj.filters
│ ├── 09_04_虚析构和纯虚析构.vcxproj.user
│ └── main.cpp
├── 99、项目实战
└── minesweeper
│ ├── minesweeper.sln
│ └── minesweeper
│ ├── Grid.cpp
│ ├── Grid.h
│ ├── Map.cpp
│ ├── Map.h
│ ├── const.h
│ ├── main.cpp
│ ├── mine.png
│ ├── minesweeper.vcxproj
│ ├── minesweeper.vcxproj.filters
│ ├── minesweeper.vcxproj.user
│ ├── openal32.dll
│ ├── sfml-audio-2.dll
│ ├── sfml-audio-d-2.dll
│ ├── sfml-graphics-2.dll
│ ├── sfml-graphics-d-2.dll
│ ├── sfml-network-2.dll
│ ├── sfml-network-d-2.dll
│ ├── sfml-system-2.dll
│ ├── sfml-system-d-2.dll
│ ├── sfml-window-2.dll
│ └── sfml-window-d-2.dll
└── README.md
/04、封装/04_01_属性和行为/04_01_属性和行为.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "04_01_属性和行为", "04_01_属性和行为\04_01_属性和行为.vcxproj", "{F4438AE1-34E5-4ACD-9356-B7ACF619446A}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Debug|x64.ActiveCfg = Debug|x64
17 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Debug|x64.Build.0 = Debug|x64
18 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Debug|x86.ActiveCfg = Debug|Win32
19 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Debug|x86.Build.0 = Debug|Win32
20 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Release|x64.ActiveCfg = Release|x64
21 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Release|x64.Build.0 = Release|x64
22 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Release|x86.ActiveCfg = Release|Win32
23 | {F4438AE1-34E5-4ACD-9356-B7ACF619446A}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {3F2E3D81-BD52-401C-9F90-235A9EB01E14}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/04、封装/04_01_属性和行为/04_01_属性和行为/04_01_属性和行为.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {f4438ae1-34e5-4acd-9356-b7acf619446a}
25 | My0401属性和行为
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/04、封装/04_01_属性和行为/04_01_属性和行为/04_01_属性和行为.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/04、封装/04_01_属性和行为/04_01_属性和行为/04_01_属性和行为.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/04、封装/04_01_属性和行为/04_01_属性和行为/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // 面向对象三大特性:封装、继承、多态
5 |
6 | /*
7 | 封装的语法:
8 |
9 | class 类名 {
10 | 访问权限:
11 | 属性(成员变量)
12 | 行为(成员函数)
13 | };
14 | */
15 |
16 | class Hero {
17 | // 访问权限 public private protected
18 | public:
19 | // 属性
20 | int m_Id; // m -> member
21 | int m_Hp;
22 |
23 | // 行为
24 | void addHp(int hp) {
25 | m_Hp += hp;
26 | }
27 |
28 | void subHp(int hp) {
29 | m_Hp -= hp;
30 | }
31 | };
32 |
33 | int main() {
34 | // 通过类来生成对象的过程,叫 实例化
35 | Hero h;
36 | // 访问对象的属性
37 | h.m_Id = 5;
38 | h.m_Hp = 100;
39 | h.addHp(100);
40 | cout << "Id 为" << h.m_Id << "的英雄,血量是" << h.m_Hp << endl;
41 | h.subHp(100);
42 | cout << "Id 为" << h.m_Id << "的英雄,血量是" << h.m_Hp << endl;
43 |
44 | return 0;
45 | }
--------------------------------------------------------------------------------
/04、封装/04_02_访问权限/04_02_访问权限.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "04_02_访问权限", "04_02_访问权限\04_02_访问权限.vcxproj", "{D6CC2C46-3D19-4461-AF36-C819D782A717}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Debug|x64.ActiveCfg = Debug|x64
17 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Debug|x64.Build.0 = Debug|x64
18 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Debug|x86.ActiveCfg = Debug|Win32
19 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Debug|x86.Build.0 = Debug|Win32
20 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Release|x64.ActiveCfg = Release|x64
21 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Release|x64.Build.0 = Release|x64
22 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Release|x86.ActiveCfg = Release|Win32
23 | {D6CC2C46-3D19-4461-AF36-C819D782A717}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {32B0E387-6169-46B2-B7A7-FC1BAD7154CD}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/04、封装/04_02_访问权限/04_02_访问权限/04_02_访问权限.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {d6cc2c46-3d19-4461-af36-c819d782a717}
25 | My0402访问权限
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/04、封装/04_02_访问权限/04_02_访问权限/04_02_访问权限.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/04、封装/04_02_访问权限/04_02_访问权限/04_02_访问权限.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/04、封装/04_02_访问权限/04_02_访问权限/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | 访问权限
6 | 公共权限 public 类内可以访问,类外也可以访问
7 | 保护权限 protected 类内可以访问,类外不可以访问 子类可以访问
8 | 私有权限 private 类内可以访问,类外不可以访问 子类不可以访问
9 |
10 | B -> A
11 |
12 | A 父类、基类 名字、房子、支付密码
13 | B 子类、派生类 公有、保护、私有
14 |
15 | */
16 |
17 |
18 | class People {
19 | // 公有权限
20 | public:
21 | int m_Id;
22 |
23 | // 保护权限
24 | protected:
25 | int m_HouseId;
26 |
27 | // 私有权限
28 | private:
29 | int m_PayPass;
30 |
31 | public:
32 | void work() {
33 | // 所有成员变量,类内均可以访问
34 | m_Id = 1;
35 | m_HouseId = 2;
36 | m_PayPass = 1314;
37 | }
38 | private:
39 | void work1() {
40 | // 所有成员变量,类内均可以访问
41 | m_Id = 1;
42 | m_HouseId = 2;
43 | m_PayPass = 1314;
44 | }
45 |
46 | };
47 |
48 |
49 | class Son : public People {
50 | void func() {
51 | m_Id = 1;
52 | m_HouseId = 4; // 保护成员,子类可以访问
53 | // m_PayPass = 123; // 私有成员,子类无法访问
54 | }
55 | };
56 |
57 | int main() {
58 | // 实例化
59 | People p;
60 | p.m_Id = 1; // 公有成员,类外可以访问
61 | //p.m_HouseId = 5; // 保护成员,类外不可以访问
62 | //p.m_PayPass = 10; // 私有成员,类外不可以访问
63 | p.work();
64 | // p.work1(); // 私有成员函数,类外不可访问
65 | return 0;
66 | }
--------------------------------------------------------------------------------
/04、封装/04_03_class和struct/04_03_class和struct.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "04_03_class和struct", "04_03_class和struct\04_03_class和struct.vcxproj", "{CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Debug|x64.ActiveCfg = Debug|x64
17 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Debug|x64.Build.0 = Debug|x64
18 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Debug|x86.ActiveCfg = Debug|Win32
19 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Debug|x86.Build.0 = Debug|Win32
20 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Release|x64.ActiveCfg = Release|x64
21 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Release|x64.Build.0 = Release|x64
22 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Release|x86.ActiveCfg = Release|Win32
23 | {CE84F28D-1EBA-4BB7-830D-7157F52A9FD5}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {2B5AE43C-9541-4C22-8CFC-59E51C5806AE}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/04、封装/04_03_class和struct/04_03_class和struct/04_03_class和struct.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/04、封装/04_03_class和struct/04_03_class和struct/04_03_class和struct.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/04、封装/04_03_class和struct/04_03_class和struct/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | struct && class
6 |
7 |
8 | struct 默认是公共的
9 | class 默认是私有的
10 | */
11 |
12 | class C {
13 | int m_a;
14 | };
15 |
16 | struct S {
17 | int m_a;
18 |
19 | void func() {
20 | m_a = 666;
21 | }
22 | };
23 |
24 | int main() {
25 | C c;
26 | S s;
27 | // c.m_a; // 私有的
28 | s.m_a = 4; // 公有的
29 | s.func();
30 | cout << s.m_a << endl;
31 |
32 | return 0;
33 | }
--------------------------------------------------------------------------------
/04、封装/04_04_属性私有化/04_04_属性私有化.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "04_04_属性私有化", "04_04_属性私有化\04_04_属性私有化.vcxproj", "{6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Debug|x64.ActiveCfg = Debug|x64
17 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Debug|x64.Build.0 = Debug|x64
18 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Debug|x86.ActiveCfg = Debug|Win32
19 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Debug|x86.Build.0 = Debug|Win32
20 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Release|x64.ActiveCfg = Release|x64
21 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Release|x64.Build.0 = Release|x64
22 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Release|x86.ActiveCfg = Release|Win32
23 | {6B7CEAD9-2580-4CC2-A9AE-681B267FDA10}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {2563A8DA-2BE1-4983-B78C-B02D16445B54}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/04、封装/04_04_属性私有化/04_04_属性私有化/04_04_属性私有化.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {6b7cead9-2580-4cc2-a9ae-681b267fda10}
25 | My0404属性私有化
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/04、封装/04_04_属性私有化/04_04_属性私有化/04_04_属性私有化.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/04、封装/04_04_属性私有化/04_04_属性私有化/04_04_属性私有化.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/04、封装/04_04_属性私有化/04_04_属性私有化/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | // 接口、方法、函数 是同一个概念
6 | // 1、可以控制读写权限
7 | // 2、可以检测数据的有效性
8 |
9 | class Hero {
10 | public:
11 | void SetName(string name) {
12 | m_Name = name;
13 | }
14 | string GetName() {
15 | return m_Name;
16 | }
17 |
18 | int GetSkillCount() {
19 | return m_SkillCount;
20 | }
21 |
22 | void SetSpeed(int speed) {
23 | if (speed < 100 || speed > 500) {
24 | cout << "速度设置不合法" << endl;
25 | return;
26 | }
27 | m_Speed = speed;
28 | }
29 |
30 | private:
31 | string m_Name; // 可读,可写
32 | int m_SkillCount = 4; // 只读
33 | int m_Speed; // 只写
34 | };
35 |
36 | int main() {
37 | Hero h;
38 | /*
39 | h.m_Name = "123";
40 | h.m_SkillCount = 4;
41 | h.m_Speed = 10;
42 | */
43 | h.SetName("剑圣");
44 | cout << "英雄的名字叫:" << h.GetName() << endl;
45 | cout << "英雄的技能数是:" << h.GetSkillCount() << endl;
46 | h.SetSpeed(666);
47 |
48 |
49 | return 0;
50 | }
--------------------------------------------------------------------------------
/05、对象特性/05_01_构造函数/05_01_构造函数.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_01_构造函数", "05_01_构造函数\05_01_构造函数.vcxproj", "{E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Debug|x64.ActiveCfg = Debug|x64
17 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Debug|x64.Build.0 = Debug|x64
18 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Debug|x86.ActiveCfg = Debug|Win32
19 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Debug|x86.Build.0 = Debug|Win32
20 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Release|x64.ActiveCfg = Release|x64
21 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Release|x64.Build.0 = Release|x64
22 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Release|x86.ActiveCfg = Release|Win32
23 | {E6F8AC27-F46E-4D83-9EAA-4C0E34105D02}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {965CA64C-AD99-405F-8B53-A2A2DAD03EC4}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_01_构造函数/05_01_构造函数/05_01_构造函数.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {e6f8ac27-f46e-4d83-9eaa-4c0e34105d02}
25 | My0501构造函数
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/05、对象特性/05_01_构造函数/05_01_构造函数/05_01_构造函数.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_01_构造函数/05_01_构造函数/05_01_构造函数.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_01_构造函数/05_01_构造函数/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 |
6 | /*
7 | 构造函数需要注意的点
8 |
9 | 1、函数名称和类名保持一致
10 | 2、返回值类型 不需要写
11 | 3、构造函数可以有参数
12 | */
13 | class Hero {
14 | public:
15 | // 默认构造函数
16 | Hero() {
17 | m_Name = "";
18 | m_SkillCount = 4;
19 | m_Speed = 100;
20 | cout << "默认构造函数:Hero 构造完毕!" << endl;
21 | }
22 | // 有参构造函数1
23 | Hero(string name) {
24 | m_Name = name;
25 | m_SkillCount = 4;
26 | m_Speed = 100;
27 | cout << "有参构造函数1:Hero 构造完毕!" << endl;
28 | }
29 | // 有参构造函数2
30 | Hero(string name, int skillCount) {
31 | m_Name = name;
32 | m_SkillCount = skillCount;
33 | m_Speed = 100;
34 | cout << "有参构造函数2:Hero 构造完毕!" << endl;
35 | }
36 |
37 | private:
38 | string m_Name;
39 | int m_SkillCount;
40 | int m_Speed;
41 | };
42 |
43 | int main() {
44 | Hero h1;
45 | Hero h2("剑圣");
46 | Hero h3(); // 函数声明 int main(); 、 int work();
47 | Hero h4{};
48 | Hero h5 = Hero("剑圣");
49 | Hero h6{ "猴子", 4 };
50 |
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/05、对象特性/05_02_析构函数/05_02_析构函数.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_02_析构函数", "05_02_析构函数\05_02_析构函数.vcxproj", "{81635CE0-28CE-4D7B-9793-FB93EE6CF48C}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Debug|x64.ActiveCfg = Debug|x64
17 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Debug|x64.Build.0 = Debug|x64
18 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Debug|x86.ActiveCfg = Debug|Win32
19 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Debug|x86.Build.0 = Debug|Win32
20 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Release|x64.ActiveCfg = Release|x64
21 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Release|x64.Build.0 = Release|x64
22 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Release|x86.ActiveCfg = Release|Win32
23 | {81635CE0-28CE-4D7B-9793-FB93EE6CF48C}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {45CB377D-C60F-421B-BB51-7860E527B475}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_02_析构函数/05_02_析构函数/05_02_析构函数.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {81635ce0-28ce-4d7b-9793-fb93ee6cf48c}
25 | My0502析构函数
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/05、对象特性/05_02_析构函数/05_02_析构函数/05_02_析构函数.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_02_析构函数/05_02_析构函数/05_02_析构函数.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_02_析构函数/05_02_析构函数/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 |
5 |
6 | /*
7 | 析构函数注意点
8 |
9 | 1、函数名称和类名一致,并且在最前面加上一个 ~ 波浪号
10 | 2、函数返回值不需要写
11 | 3、不能有参数
12 |
13 | */
14 | class Hero {
15 | public:
16 | // 构造函数
17 | Hero() {
18 | cout << "Hero 默认构造函数调用完毕!" << endl;
19 | }
20 | // 析构函数
21 | ~Hero() {
22 | cout << "Hero 析构函数调用完毕!" << endl;
23 | }
24 | };
25 |
26 | void test() {
27 | Hero h;
28 | }
29 |
30 | int main() {
31 | test();
32 | Hero h;
33 | int a;
34 | cin >> a;
35 | return 0;
36 | }
--------------------------------------------------------------------------------
/05、对象特性/05_03_拷贝构造函数/05_03_拷贝构造函数.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_03_拷贝构造函数", "05_03_拷贝构造函数\05_03_拷贝构造函数.vcxproj", "{FC8423A0-6EE3-4278-B622-22A7B02D7DCB}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Debug|x64.ActiveCfg = Debug|x64
17 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Debug|x64.Build.0 = Debug|x64
18 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Debug|x86.ActiveCfg = Debug|Win32
19 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Debug|x86.Build.0 = Debug|Win32
20 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Release|x64.ActiveCfg = Release|x64
21 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Release|x64.Build.0 = Release|x64
22 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Release|x86.ActiveCfg = Release|Win32
23 | {FC8423A0-6EE3-4278-B622-22A7B02D7DCB}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {88BBB74E-23E9-44A0-A516-E86DA617563E}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_03_拷贝构造函数/05_03_拷贝构造函数/05_03_拷贝构造函数.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_03_拷贝构造函数/05_03_拷贝构造函数/05_03_拷贝构造函数.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_03_拷贝构造函数/05_03_拷贝构造函数/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 |
5 | /*
6 | 拷贝构造函数的定义
7 | 类名(const 类型& 变量名) {}
8 |
9 | */
10 | class Hero {
11 | public:
12 | // 默认构造函数
13 | Hero() {
14 | m_Hp = 100;
15 | cout << "Hero 默认构造函数调用完毕!" << endl;
16 | }
17 |
18 | // 有参构造函数
19 | Hero(int hp) {
20 | m_Hp = hp;
21 | cout << "Hero 有参构造函数调用完毕!" << endl;
22 | }
23 |
24 | Hero(const Hero& h) {
25 | m_Hp = h.m_Hp;
26 | cout << "Hero 拷贝构造函数调用完毕!" << endl;
27 | }
28 |
29 | // 析构函数
30 | ~Hero() {
31 | cout << "Hero 析构函数调用完毕!" << endl;
32 | }
33 |
34 | private:
35 | int m_Hp;
36 | };
37 |
38 |
39 | /*
40 | 拷贝构造函数的调用时机
41 |
42 | 1、用已经创建的对象来初始化对象
43 | 2、函数的传参
44 | 3、函数的返回值
45 |
46 | */
47 |
48 | // 1、用已经创建的对象来初始化对象
49 | void func1() {
50 | cout << "--------------func1--------------" << endl;
51 | Hero h1(20);
52 | Hero h2(h1);
53 | }
54 |
55 | void test1(Hero h) {
56 |
57 | }
58 |
59 | void test2(Hero* h) {
60 |
61 | }
62 |
63 | // 2、函数的传参
64 | void func2() {
65 | cout << "--------------func2--------------" << endl;
66 | Hero h1;
67 | // test1(h1);
68 | test2(&h1);
69 | }
70 |
71 | // 3、函数的返回值
72 | Hero test3() {
73 | Hero h(40);
74 | return h;
75 | }
76 |
77 | void func3() {
78 | cout << "--------------func3--------------" << endl;
79 | Hero h = test3();
80 | }
81 |
82 | int main() {
83 | func1();
84 | func2();
85 | func3();
86 | return 0;
87 | }
88 |
--------------------------------------------------------------------------------
/05、对象特性/05_04_初始化列表/05_04_初始化列表.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_04_初始化列表", "05_04_初始化列表\05_04_初始化列表.vcxproj", "{7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Debug|x64.ActiveCfg = Debug|x64
17 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Debug|x64.Build.0 = Debug|x64
18 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Debug|x86.ActiveCfg = Debug|Win32
19 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Debug|x86.Build.0 = Debug|Win32
20 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Release|x64.ActiveCfg = Release|x64
21 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Release|x64.Build.0 = Release|x64
22 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Release|x86.ActiveCfg = Release|Win32
23 | {7F9B6356-C09F-4C37-848C-06FCEDCE9E1D}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {F775A2AF-D0E8-4531-AF75-C2FB83A7A79C}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_04_初始化列表/05_04_初始化列表/05_04_初始化列表.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {7f9b6356-c09f-4c37-848c-06fcedce9e1d}
25 | My0504初始化列表
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/05、对象特性/05_04_初始化列表/05_04_初始化列表/05_04_初始化列表.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_04_初始化列表/05_04_初始化列表/05_04_初始化列表.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_04_初始化列表/05_04_初始化列表/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | /*
6 | 初始化列表的语法
7 |
8 | 构造函数(传参1, 传参2): 成员变量1(传参1), 成员变量2(传参2) {}
9 |
10 | */
11 | class Hero {
12 | public:
13 | /*Hero(string name, int hp) {
14 | m_Name = name;
15 | m_Hp = hp;
16 | }*/
17 | Hero(string name, int hp, int speed) : m_Name(name), m_Hp(hp), m_Speed(speed) {
18 | }
19 |
20 | void Print() {
21 | cout << "英雄:" << m_Name << "的血量是" << m_Hp << ",速度是" << m_Speed << endl;
22 | }
23 |
24 | private:
25 | string m_Name;
26 | int m_Hp;
27 | int m_Speed;
28 | };
29 | int main() {
30 | Hero h("剑圣", 100, 10);
31 | h.Print();
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/05、对象特性/05_05_静态成员变量/05_05_静态成员变量.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_05_静态成员变量", "05_05_静态成员变量\05_05_静态成员变量.vcxproj", "{1185FD30-B961-42DA-B800-E9EDE8FB9897}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Debug|x64.ActiveCfg = Debug|x64
17 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Debug|x64.Build.0 = Debug|x64
18 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Debug|x86.ActiveCfg = Debug|Win32
19 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Debug|x86.Build.0 = Debug|Win32
20 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Release|x64.ActiveCfg = Release|x64
21 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Release|x64.Build.0 = Release|x64
22 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Release|x86.ActiveCfg = Release|Win32
23 | {1185FD30-B961-42DA-B800-E9EDE8FB9897}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {78899794-2AF2-4389-A4F0-00E50B3451D5}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_05_静态成员变量/05_05_静态成员变量/05_05_静态成员变量.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_05_静态成员变量/05_05_静态成员变量/05_05_静态成员变量.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_05_静态成员变量/05_05_静态成员变量/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 |
6 | /*
7 | 静态成员变量的特点:
8 | 1、所有的对象共享同一份数据
9 | 2、编译阶段分配内存
10 | 3、需要在类中进行声明,在类外进行初始化
11 | */
12 | class Hero {
13 | public:
14 | Hero() {
15 | m_Name = "英雄";
16 | m_Hp = 100;
17 | }
18 |
19 | ~Hero() {
20 |
21 | }
22 | // 3.1 声明
23 | static int m_HeroCount;
24 | private:
25 | string m_Name;
26 | int m_Hp;
27 |
28 | };
29 |
30 | // 3.2 初始化
31 | int Hero::m_HeroCount = 100;
32 |
33 | int main() {
34 | Hero h;
35 | cout << h.m_HeroCount << endl;
36 | h.m_HeroCount = 101;
37 | cout << Hero::m_HeroCount << endl;
38 |
39 | cout << &(h.m_HeroCount) << endl;
40 | cout << &(Hero::m_HeroCount) << endl;
41 | return 0;
42 | }
43 |
--------------------------------------------------------------------------------
/05、对象特性/05_06_静态成员函数/05_06_静态成员函数.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_06_静态成员函数", "05_06_静态成员函数\05_06_静态成员函数.vcxproj", "{72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Debug|x64.ActiveCfg = Debug|x64
17 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Debug|x64.Build.0 = Debug|x64
18 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Debug|x86.ActiveCfg = Debug|Win32
19 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Debug|x86.Build.0 = Debug|Win32
20 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Release|x64.ActiveCfg = Release|x64
21 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Release|x64.Build.0 = Release|x64
22 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Release|x86.ActiveCfg = Release|Win32
23 | {72DAA0C0-C46A-4772-8FCC-A21B7C51F39F}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {1DFC2C0A-68A1-4946-9B5F-D91CDBDDD621}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_06_静态成员函数/05_06_静态成员函数/05_06_静态成员函数.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_06_静态成员函数/05_06_静态成员函数/05_06_静态成员函数.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_06_静态成员函数/05_06_静态成员函数/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 |
6 | /*
7 | 静态成员函数
8 | 1、所有对象共享函数
9 | 2、静态成员函数只能使用静态成员变量,无法使用普通成员变量
10 | */
11 |
12 | class Hero {
13 | public:
14 | Hero() {
15 | m_Name = "英雄";
16 | m_Hp = 100;
17 | }
18 | ~Hero() {
19 |
20 | }
21 |
22 | static int m_HeroCount;
23 |
24 | static int GetHeroCount() {
25 | // m_Hp += 1;
26 | return m_HeroCount;
27 | }
28 | private:
29 | string m_Name;
30 | int m_Hp;
31 |
32 |
33 | static int GetHeroCount1() {
34 | // m_Hp += 1;
35 | return m_HeroCount;
36 | }
37 | };
38 |
39 | int Hero::m_HeroCount = 100;
40 |
41 | int main() {
42 | Hero h;
43 | cout << h.GetHeroCount() << endl;
44 | cout << Hero::GetHeroCount() << endl;
45 | // h.GetHeroCount1();
46 | return 0;
47 | }
--------------------------------------------------------------------------------
/05、对象特性/05_07_this指针/05_07_this指针.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_07_this指针", "05_07_this指针\05_07_this指针.vcxproj", "{53C7B07F-782F-4106-91A0-9845416D5B0A}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Debug|x64.ActiveCfg = Debug|x64
17 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Debug|x64.Build.0 = Debug|x64
18 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Debug|x86.ActiveCfg = Debug|Win32
19 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Debug|x86.Build.0 = Debug|Win32
20 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Release|x64.ActiveCfg = Release|x64
21 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Release|x64.Build.0 = Release|x64
22 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Release|x86.ActiveCfg = Release|Win32
23 | {53C7B07F-782F-4106-91A0-9845416D5B0A}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {97200D16-88C2-4DFB-BAA1-F04B6F7C6372}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_07_this指针/05_07_this指针/05_07_this指针.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_07_this指针/05_07_this指针/05_07_this指针.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_07_this指针/05_07_this指针/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | this指针
6 | 1、解决命名冲突
7 | 2、*this 就可以获取到这个对象本身
8 |
9 |
10 | this *this
11 | &h *(&h) == h
12 |
13 | */
14 |
15 | class Hero {
16 | public:
17 | Hero(int hp) {
18 | this->hp = hp;
19 | cout << this << endl;
20 | cout << (*this).hp << endl;
21 | }
22 | int hp;
23 | };
24 |
25 | int main() {
26 | Hero h(100);
27 | cout << h.hp << endl;
28 | cout << &h << endl;
29 | cout << (*(& h)).hp << endl;
30 | return 0;
31 | }
--------------------------------------------------------------------------------
/05、对象特性/05_08_const修饰成员函数/05_08_const修饰成员函数.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_08_const修饰成员函数", "05_08_const修饰成员函数\05_08_const修饰成员函数.vcxproj", "{48226FAA-0830-4891-AF8B-C047F2C66B62}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Debug|x64.ActiveCfg = Debug|x64
17 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Debug|x64.Build.0 = Debug|x64
18 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Debug|x86.ActiveCfg = Debug|Win32
19 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Debug|x86.Build.0 = Debug|Win32
20 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Release|x64.ActiveCfg = Release|x64
21 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Release|x64.Build.0 = Release|x64
22 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Release|x86.ActiveCfg = Release|Win32
23 | {48226FAA-0830-4891-AF8B-C047F2C66B62}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {7986DE9B-C731-43D6-93AC-C1E0DA1AAC64}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_08_const修饰成员函数/05_08_const修饰成员函数/05_08_const修饰成员函数.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_08_const修饰成员函数/05_08_const修饰成员函数/05_08_const修饰成员函数.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_08_const修饰成员函数/05_08_const修饰成员函数/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | // ³£º¯Êý
6 | class Hero {
7 | public:
8 | Hero() : m_Hp(0) {}
9 |
10 | int getHp() const {
11 | // m_Hp = m_Hp + 1;
12 | return m_Hp;
13 | }
14 | int setHp(int hp) {
15 | m_Hp = hp;
16 | }
17 | private:
18 | int m_Hp;
19 | };
20 |
21 |
22 | int main() {
23 | const Hero h;
24 | // h.setHp(100);
25 | h.getHp();
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/05、对象特性/05_09_mutable关键字/05_09_mutable关键字.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "05_09_mutable关键字", "05_09_mutable关键字\05_09_mutable关键字.vcxproj", "{879143F8-F065-4338-9B8A-FF0E1BC5319F}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Debug|x64.ActiveCfg = Debug|x64
17 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Debug|x64.Build.0 = Debug|x64
18 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Debug|x86.ActiveCfg = Debug|Win32
19 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Debug|x86.Build.0 = Debug|Win32
20 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Release|x64.ActiveCfg = Release|x64
21 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Release|x64.Build.0 = Release|x64
22 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Release|x86.ActiveCfg = Release|Win32
23 | {879143F8-F065-4338-9B8A-FF0E1BC5319F}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {ECC56A4B-09E4-4B77-9339-163DF465BC8A}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/05、对象特性/05_09_mutable关键字/05_09_mutable关键字/05_09_mutable关键字.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/05、对象特性/05_09_mutable关键字/05_09_mutable关键字/05_09_mutable关键字.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/05、对象特性/05_09_mutable关键字/05_09_mutable关键字/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // mutable <-> const
5 |
6 | class Hero {
7 | public:
8 | Hero() :m_Hp(0), m_getHpCounter(0){}
9 |
10 | int getHp() const {
11 | m_getHpCounter++;
12 | return m_Hp;
13 | }
14 |
15 | void printCounter() const {
16 | cout << "Counter: " << m_getHpCounter << endl;
17 | }
18 |
19 | private:
20 | int m_Hp;
21 | mutable int m_getHpCounter;
22 | };
23 |
24 | int main() {
25 | Hero h;
26 | h.getHp(), h.getHp(), h.getHp(), h.getHp(), h.getHp(), h.getHp();
27 | h.printCounter();
28 |
29 | return 0;
30 | }
--------------------------------------------------------------------------------
/06、友元/06_01_全局函数作为友元/06_01_全局函数作为友元.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "06_01_全局函数作为友元", "06_01_全局函数作为友元\06_01_全局函数作为友元.vcxproj", "{87E5F471-65A4-4D83-AAAF-34D1DE0D005D}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Debug|x64.ActiveCfg = Debug|x64
17 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Debug|x64.Build.0 = Debug|x64
18 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Debug|x86.ActiveCfg = Debug|Win32
19 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Debug|x86.Build.0 = Debug|Win32
20 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Release|x64.ActiveCfg = Release|x64
21 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Release|x64.Build.0 = Release|x64
22 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Release|x86.ActiveCfg = Release|Win32
23 | {87E5F471-65A4-4D83-AAAF-34D1DE0D005D}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {59A87A54-2173-4930-A284-7DB796729718}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/06、友元/06_01_全局函数作为友元/06_01_全局函数作为友元/06_01_全局函数作为友元.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/06、友元/06_01_全局函数作为友元/06_01_全局函数作为友元/06_01_全局函数作为友元.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/06、友元/06_01_全局函数作为友元/06_01_全局函数作为友元/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | /*
6 | 友元的目的
7 |
8 | 让一个类 或者 函数
9 | 能够访问另一个类的私有成员
10 |
11 | 友元的关键字: friend
12 |
13 | 三种友元
14 | 1、全局函数作为友元
15 | 2、类作为友元
16 | 3、成员函数作为友元
17 | */
18 |
19 | class People{
20 | friend void friendVisit(People* p);
21 | public:
22 | People() {
23 | m_House = "别墅";
24 | m_Car = "跑车";
25 | }
26 | public:
27 | string m_House;
28 |
29 | private:
30 | string m_Car;
31 | };
32 |
33 | void friendVisit(People* p) {
34 | cout << "好朋友来访问你的" << p->m_House << endl;
35 | cout << "好朋友来访问你的" << p->m_Car << endl;
36 | }
37 |
38 | int main() {
39 | People p;
40 | friendVisit(&p);
41 | return 0;
42 | }
--------------------------------------------------------------------------------
/06、友元/06_02_类作为友元/06_02_类作为友元.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "06_02_类作为友元", "06_02_类作为友元\06_02_类作为友元.vcxproj", "{8F1F240A-9593-4FCD-9CFA-F8E7D563197D}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Debug|x64.ActiveCfg = Debug|x64
17 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Debug|x64.Build.0 = Debug|x64
18 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Debug|x86.ActiveCfg = Debug|Win32
19 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Debug|x86.Build.0 = Debug|Win32
20 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Release|x64.ActiveCfg = Release|x64
21 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Release|x64.Build.0 = Release|x64
22 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Release|x86.ActiveCfg = Release|Win32
23 | {8F1F240A-9593-4FCD-9CFA-F8E7D563197D}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {F1096078-69BA-419F-87E8-A18564D5C634}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/06、友元/06_02_类作为友元/06_02_类作为友元/06_02_类作为友元.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {8f1f240a-9593-4fcd-9cfa-f8e7d563197d}
25 | My0602类作为友元
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/06、友元/06_02_类作为友元/06_02_类作为友元/06_02_类作为友元.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/06、友元/06_02_类作为友元/06_02_类作为友元/06_02_类作为友元.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/06、友元/06_02_类作为友元/06_02_类作为友元/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // 类作为友元
5 | // 让一个类去访问另一个类的私有成员
6 |
7 | class People;
8 |
9 | class PeopleFriend {
10 | public:
11 | PeopleFriend() {
12 |
13 | }
14 | void visit(People* p);
15 | };
16 |
17 | class People {
18 | friend class PeopleFriend;
19 | public:
20 | People() {
21 | m_House = "别墅";
22 | m_Car = "跑车";
23 | }
24 | public:
25 | string m_House;
26 |
27 | private:
28 | string m_Car;
29 | };
30 |
31 | void PeopleFriend::visit(People* p) {
32 | cout << "好朋友来访问你的" << p->m_House << endl;
33 | cout << "好朋友来访问你的" << p->m_Car << endl;
34 | }
35 |
36 | int main() {
37 | People p;
38 | PeopleFriend pf;
39 | pf.visit(&p);
40 | return 0;
41 | }
42 |
--------------------------------------------------------------------------------
/06、友元/06_03_成员函数作为友元/06_03_成员函数作为友元.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "06_03_成员函数作为友元", "06_03_成员函数作为友元\06_03_成员函数作为友元.vcxproj", "{29673C0B-AB57-45CA-B0D4-285D444E4FF5}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Debug|x64.ActiveCfg = Debug|x64
17 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Debug|x64.Build.0 = Debug|x64
18 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Debug|x86.ActiveCfg = Debug|Win32
19 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Debug|x86.Build.0 = Debug|Win32
20 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Release|x64.ActiveCfg = Release|x64
21 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Release|x64.Build.0 = Release|x64
22 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Release|x86.ActiveCfg = Release|Win32
23 | {29673C0B-AB57-45CA-B0D4-285D444E4FF5}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {437E088B-5475-4144-B139-FE64DA7F90E4}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/06、友元/06_03_成员函数作为友元/06_03_成员函数作为友元/06_03_成员函数作为友元.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/06、友元/06_03_成员函数作为友元/06_03_成员函数作为友元/06_03_成员函数作为友元.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/06、友元/06_03_成员函数作为友元/06_03_成员函数作为友元/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | // 成员函数作为友元
6 | // PeopleFriend 的某个函数能够访问 People 的私有成员变量
7 |
8 | class People;
9 |
10 | class PeopleFriend {
11 | public:
12 | PeopleFriend() {}
13 |
14 | void visitAll(People* p);
15 | void visitPub(People* p);
16 | };
17 |
18 |
19 | class People {
20 | // friend class PeopleFriend;
21 | friend void PeopleFriend::visitAll(People* p);
22 | public:
23 | People() {
24 | m_House = "别墅";
25 | m_Car = "跑车";
26 | }
27 | public:
28 | string m_House;
29 | private:
30 | string m_Car;
31 | };
32 |
33 | void PeopleFriend::visitAll(People* p) {
34 | cout << "好朋友访问了你的" << p->m_House << endl;
35 | cout << "好朋友访问了你的" << p->m_Car << endl;
36 |
37 | }
38 | void PeopleFriend::visitPub(People* p) {
39 | cout << "好朋友访问了你的" << p->m_House << endl;
40 | // cout << "好朋友访问了你的" << p->m_Car << endl;
41 | }
42 |
43 | int main() {
44 | People p;
45 | PeopleFriend pf;
46 | pf.visitAll(&p);
47 | pf.visitPub(&p);
48 |
49 | return 0;
50 | }
51 |
--------------------------------------------------------------------------------
/07、运算符重载/07_01_运算符重载概念/07_01_运算符重载概念.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "07_01_运算符重载概念", "07_01_运算符重载概念\07_01_运算符重载概念.vcxproj", "{1B12E676-1CE8-406D-B051-AAEB3F949E46}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Debug|x64.ActiveCfg = Debug|x64
17 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Debug|x64.Build.0 = Debug|x64
18 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Debug|x86.ActiveCfg = Debug|Win32
19 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Debug|x86.Build.0 = Debug|Win32
20 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Release|x64.ActiveCfg = Release|x64
21 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Release|x64.Build.0 = Release|x64
22 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Release|x86.ActiveCfg = Release|Win32
23 | {1B12E676-1CE8-406D-B051-AAEB3F949E46}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {0C64057D-FABB-4641-8DE1-157F6C047433}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/07、运算符重载/07_01_运算符重载概念/07_01_运算符重载概念/07_01_运算符重载概念.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/07、运算符重载/07_01_运算符重载概念/07_01_运算符重载概念/07_01_运算符重载概念.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/07、运算符重载/07_01_运算符重载概念/07_01_运算符重载概念/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace std;
4 |
5 | /*
6 | +
7 | 4 + 5 = 9
8 |
9 | class A {
10 | };
11 |
12 | A a;
13 | A b;
14 | a + b;
15 |
16 | */
17 | int main() {
18 | // 1、加法运算符
19 | int a = 520;
20 | int b = 1314;
21 | cout << a + b << endl;
22 |
23 | // 2、字符串拼接
24 | string c = "520";
25 | string d = "1314";
26 | cout << c + d << endl;
27 |
28 | string e = "我";
29 | string f = "爱你";
30 | cout << e + f << endl;
31 | return 0;
32 | }
--------------------------------------------------------------------------------
/07、运算符重载/07_02_加号重载/07_02_加号重载.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "07_02_加号重载", "07_02_加号重载\07_02_加号重载.vcxproj", "{00F7E6FA-E895-4830-8E0D-7E2535EBFB29}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Debug|x64.ActiveCfg = Debug|x64
17 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Debug|x64.Build.0 = Debug|x64
18 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Debug|x86.ActiveCfg = Debug|Win32
19 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Debug|x86.Build.0 = Debug|Win32
20 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Release|x64.ActiveCfg = Release|x64
21 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Release|x64.Build.0 = Release|x64
22 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Release|x86.ActiveCfg = Release|Win32
23 | {00F7E6FA-E895-4830-8E0D-7E2535EBFB29}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {F40FDCE2-88A9-4EDE-8C2C-A36B228A3B6C}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/07、运算符重载/07_02_加号重载/07_02_加号重载/07_02_加号重载.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/07、运算符重载/07_02_加号重载/07_02_加号重载/07_02_加号重载.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/07、运算符重载/07_02_加号重载/07_02_加号重载/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | +
6 | */
7 | // ¸´ÊıÀà
8 | class Complex {
9 | friend Complex operator+(Complex& a, Complex& b);
10 | friend Complex operator-(Complex& a, Complex& b);
11 | public:
12 | Complex() : real(0), image(0) {
13 |
14 | }
15 | Complex(int real, int image) {
16 | this->real = real;
17 | this->image = image;
18 | }
19 | /*
20 | Complex operator+(Complex& other) {
21 | Complex ret;
22 | ret.real = this->real + other.real;
23 | ret.image = this->image + other.image;
24 | return ret;
25 | }*/
26 |
27 | // a + bi
28 | void Print() {
29 | cout << real << '+' << image << 'i' << endl;
30 | }
31 |
32 | private:
33 | int real;
34 | int image;
35 | };
36 |
37 | Complex operator+(Complex& a, Complex& b) {
38 | Complex ret;
39 | ret.real = a.real + b.real;
40 | ret.image = a.image + b.image;
41 | return ret;
42 | }
43 |
44 | Complex operator-(Complex& a, Complex& b) {
45 | Complex ret;
46 | ret.real = a.real - b.real;
47 | ret.image = a.image - b.image;
48 | return ret;
49 | }
50 |
51 | int main() {
52 | Complex a(10, 20);
53 | Complex b(5, 8);
54 | // Complex c = a.operator+(b);
55 | Complex c = a + b;
56 | Complex d = a - b;
57 | c.Print();
58 | d.Print();
59 | return 0;
60 | }
--------------------------------------------------------------------------------
/07、运算符重载/07_03_左移重载/07_03_左移重载.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "07_03_左移重载", "07_03_左移重载\07_03_左移重载.vcxproj", "{AB06BB77-A5E0-494F-83A2-5FB03A8780B3}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Debug|x64.ActiveCfg = Debug|x64
17 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Debug|x64.Build.0 = Debug|x64
18 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Debug|x86.ActiveCfg = Debug|Win32
19 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Debug|x86.Build.0 = Debug|Win32
20 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Release|x64.ActiveCfg = Release|x64
21 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Release|x64.Build.0 = Release|x64
22 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Release|x86.ActiveCfg = Release|Win32
23 | {AB06BB77-A5E0-494F-83A2-5FB03A8780B3}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {B53F01D4-77BF-4D54-BA82-708E8C19EFFB}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/07、运算符重载/07_03_左移重载/07_03_左移重载/07_03_左移重载.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/07、运算符重载/07_03_左移重载/07_03_左移重载/07_03_左移重载.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/07、运算符重载/07_03_左移重载/07_03_左移重载/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | <<
6 |
7 | Complex c;
8 | cout.operator<<(c)
9 |
10 | // 成员函数
11 | c.operator<<(cout)
12 | c << cout
13 | */
14 | // 复数类
15 | class Complex {
16 | friend Complex operator+(Complex& a, Complex& b);
17 | friend Complex operator-(Complex& a, Complex& b);
18 | friend ostream& operator<<(ostream& cout, Complex a);
19 | public:
20 | Complex() : real(0), image(0) {
21 |
22 | }
23 | Complex(int real, int image) {
24 | this->real = real;
25 | this->image = image;
26 | }
27 | /*
28 | Complex operator+(Complex& other) {
29 | Complex ret;
30 | ret.real = this->real + other.real;
31 | ret.image = this->image + other.image;
32 | return ret;
33 | }*/
34 |
35 | // a + bi
36 | void Print() {
37 | cout << real << '+' << image << 'i' << endl;
38 | }
39 |
40 | private:
41 | int real;
42 | int image;
43 | };
44 |
45 | Complex operator+(Complex& a, Complex& b) {
46 | Complex ret;
47 | ret.real = a.real + b.real;
48 | ret.image = a.image + b.image;
49 | return ret;
50 | }
51 |
52 | Complex operator-(Complex& a, Complex& b) {
53 | Complex ret;
54 | ret.real = a.real - b.real;
55 | ret.image = a.image - b.image;
56 | return ret;
57 | }
58 |
59 | ostream& operator<<(ostream& cout, Complex a) {
60 | cout << a.real << '+' << a.image << 'i';
61 | return cout;
62 | }
63 |
64 | int main() {
65 | Complex a(10, 20);
66 | Complex b(5, 8);
67 | // Complex c = a.operator+(b);
68 | Complex c = a + b;
69 | Complex d = a - b;
70 | //c.Print();
71 | //d.Print();
72 | // operator<<(cout, c)
73 | cout << c << endl << endl;
74 | return 0;
75 | }
--------------------------------------------------------------------------------
/07、运算符重载/07_04_递增运算符/07_04_递增运算符.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "07_04_递增运算符", "07_04_递增运算符\07_04_递增运算符.vcxproj", "{DEB76529-2B6A-4A6B-88DA-42A89F62C215}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Debug|x64.ActiveCfg = Debug|x64
17 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Debug|x64.Build.0 = Debug|x64
18 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Debug|x86.ActiveCfg = Debug|Win32
19 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Debug|x86.Build.0 = Debug|Win32
20 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Release|x64.ActiveCfg = Release|x64
21 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Release|x64.Build.0 = Release|x64
22 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Release|x86.ActiveCfg = Release|Win32
23 | {DEB76529-2B6A-4A6B-88DA-42A89F62C215}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {587A6CDA-5024-4A88-BE0B-9557631A41E1}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/07、运算符重载/07_04_递增运算符/07_04_递增运算符/07_04_递增运算符.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/07、运算符重载/07_04_递增运算符/07_04_递增运算符/07_04_递增运算符.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/07、运算符重载/07_04_递增运算符/07_04_递增运算符/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | ++
6 |
7 | «∞÷√++
8 | ∫Û÷√++
9 | */
10 |
11 | class Complex {
12 | friend ostream& operator<<(ostream& c, const Complex& a);
13 | public:
14 | Complex() : real(0), image(0) {}
15 | Complex(int real, int image) {
16 | this->real = real;
17 | this->image = image;
18 | }
19 |
20 | Complex& operator++() {
21 | this->real += 1;
22 | return *this;
23 | }
24 |
25 | Complex operator++(int) {
26 | Complex c = *this;
27 | this->real += 1;
28 | return c;
29 | }
30 |
31 | private:
32 | int real;
33 | int image;
34 | };
35 |
36 | ostream& operator<<(ostream& c, const Complex& a) {
37 | c << a.real << '+' << a.image << 'i';
38 | return c;
39 | }
40 |
41 | class A {
42 |
43 | };
44 | A func1() {
45 | return A();
46 | }
47 | void func2(const A& a) {
48 |
49 | }
50 |
51 | int main() {
52 | int x = 1;
53 | cout << ++(++x) << endl;
54 | cout << x << endl;
55 |
56 | Complex a(10, 10);
57 | cout << a << endl;
58 | // ++a;
59 | cout << ++(++a) << endl;
60 | cout << a << endl;
61 |
62 | cout << a++ << endl;
63 | cout << a << endl;
64 |
65 | // func2(func1());
66 |
67 | // cout << ((a++)++)++ << endl;
68 | cout << a << endl;
69 |
70 | int b = 5;
71 | // cout << ((b++)++)++ << endl;
72 | cout << b << endl;
73 |
74 | return 0;
75 | }
--------------------------------------------------------------------------------
/07、运算符重载/07_05_赋值重载/07_05_赋值重载.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "07_05_赋值重载", "07_05_赋值重载\07_05_赋值重载.vcxproj", "{D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Debug|x64.ActiveCfg = Debug|x64
17 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Debug|x64.Build.0 = Debug|x64
18 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Debug|x86.ActiveCfg = Debug|Win32
19 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Debug|x86.Build.0 = Debug|Win32
20 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Release|x64.ActiveCfg = Release|x64
21 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Release|x64.Build.0 = Release|x64
22 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Release|x86.ActiveCfg = Release|Win32
23 | {D1CB9EE2-CAE9-438B-895B-DB5CB171D0F6}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {1665F914-E436-47DE-BADE-C8167AC19B7C}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/07、运算符重载/07_05_赋值重载/07_05_赋值重载/07_05_赋值重载.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/07、运算符重载/07_05_赋值重载/07_05_赋值重载/07_05_赋值重载.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/07、运算符重载/07_05_赋值重载/07_05_赋值重载/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 |
5 | class Hero {
6 | public:
7 | Hero() : m_Data(NULL) {}
8 | Hero(int data) {
9 | m_Data = new int;
10 | *m_Data = data;
11 | }
12 | // double delete
13 | ~Hero() {
14 | if (m_Data) {
15 | delete m_Data;
16 | m_Data = NULL;
17 | }
18 | }
19 |
20 | Hero& operator=(Hero& h) {
21 | // m_Data = h.m_Data;
22 | if (m_Data) {
23 | delete m_Data;
24 | m_Data = NULL;
25 | }
26 | m_Data = new int;
27 | *m_Data = *h.m_Data;
28 | return *this;
29 | }
30 |
31 | int* m_Data;
32 | };
33 |
34 | int main() {
35 | Hero h1(1);
36 | Hero h2(2);
37 | Hero h3(3);
38 |
39 | cout << h1.m_Data << endl;
40 | cout << h2.m_Data << endl;
41 | h1 = h2; // ÄÚ´æÐ¹Â©
42 | cout << h1.m_Data << endl;
43 | cout << h2.m_Data << endl;
44 |
45 | h3 = (h2 = h1);
46 | cout << h1.m_Data << endl;
47 | cout << h2.m_Data << endl;
48 | cout << h3.m_Data << endl;
49 |
50 | return 0;
51 | }
--------------------------------------------------------------------------------
/07、运算符重载/07_06_关系运算重载/07_06_关系运算重载.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "07_06_关系运算重载", "07_06_关系运算重载\07_06_关系运算重载.vcxproj", "{7365F239-41C6-4A1F-9692-B0BB6DBE90AF}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Debug|x64.ActiveCfg = Debug|x64
17 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Debug|x64.Build.0 = Debug|x64
18 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Debug|x86.ActiveCfg = Debug|Win32
19 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Debug|x86.Build.0 = Debug|Win32
20 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Release|x64.ActiveCfg = Release|x64
21 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Release|x64.Build.0 = Release|x64
22 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Release|x86.ActiveCfg = Release|Win32
23 | {7365F239-41C6-4A1F-9692-B0BB6DBE90AF}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {6AE7D20F-B4C1-4BD7-AF7F-6DA5D34F12E3}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/07、运算符重载/07_06_关系运算重载/07_06_关系运算重载/07_06_关系运算重载.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/07、运算符重载/07_06_关系运算重载/07_06_关系运算重载/07_06_关系运算重载.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/07、运算符重载/07_06_关系运算重载/07_06_关系运算重载/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class Point {
5 | public:
6 | Point(int x, int y) : m_x(x), m_y(y) {
7 | }
8 |
9 | bool operator==(const Point& other) const {
10 | return m_x == other.m_x && m_y == other.m_y;
11 | }
12 |
13 | bool operator<(const Point& other) const {
14 | int d = m_x * m_x + m_y * m_y;
15 | int otherd = other.m_x * other.m_x + other.m_y * other.m_y;
16 | return d < otherd;
17 | }
18 | bool operator>(const Point& other) const {
19 | if (*this == other) {
20 | return false;
21 | }
22 | if (*this < other) {
23 | return false;
24 | }
25 | return true;
26 | }
27 | private:
28 | int m_x, m_y;
29 | };
30 |
31 | int main() {
32 | Point a(1, 6);
33 | Point b(2, 5);
34 |
35 | if (a == b) {
36 | cout << "a 和 b 相等" << endl;
37 | }
38 | else if (a < b) {
39 | cout << "a 比 b 更加接近原点" << endl;
40 | }
41 | else if(a > b) {
42 | cout << "b 比 a 更加接近原点" << endl;
43 | }
44 |
45 | return 0;
46 | }
--------------------------------------------------------------------------------
/07、运算符重载/07_07_函数调用运算符重载/07_07_函数调用运算符重载.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "07_07_函数调用运算符重载", "07_07_函数调用运算符重载\07_07_函数调用运算符重载.vcxproj", "{95944C9D-F1A2-43DD-8DD6-30F54B500567}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Debug|x64.ActiveCfg = Debug|x64
17 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Debug|x64.Build.0 = Debug|x64
18 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Debug|x86.ActiveCfg = Debug|Win32
19 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Debug|x86.Build.0 = Debug|Win32
20 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Release|x64.ActiveCfg = Release|x64
21 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Release|x64.Build.0 = Release|x64
22 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Release|x86.ActiveCfg = Release|Win32
23 | {95944C9D-F1A2-43DD-8DD6-30F54B500567}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {A35DC935-3441-43F1-BD5A-F7800D742CEC}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/07、运算符重载/07_07_函数调用运算符重载/07_07_函数调用运算符重载/07_07_函数调用运算符重载.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/07、运算符重载/07_07_函数调用运算符重载/07_07_函数调用运算符重载/07_07_函数调用运算符重载.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/07、运算符重载/07_07_函数调用运算符重载/07_07_函数调用运算符重载/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | // ()
5 | // add()
6 |
7 | class AddFunctor {
8 | public:
9 | AddFunctor() {
10 | m_acc = 0;
11 | }
12 | int operator() (int a, int b) {
13 | m_acc++;
14 | return a + b + m_acc;
15 | }
16 | private:
17 | int m_acc;
18 | };
19 |
20 | int Add(int a, int b) {
21 | return a + b;
22 | }
23 |
24 | int main() {
25 | AddFunctor add;
26 | cout << add(5, 6) <
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/08、继承/08_01_继承的语法/08_01_继承的语法/08_01_继承的语法.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/08、继承/08_01_继承的语法/08_01_继承的语法/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | * 动物
6 | * / \
7 | * 猫 狗
8 | *
9 | * 继承的语法
10 | * class 子类 : 继承方式 父类 {}
11 | * 子类 -> 派生类
12 | * 父类 -> 基类
13 | */
14 |
15 | class Animal {
16 | public:
17 | void eat() {
18 | cout << "吃" << endl;
19 | }
20 | };
21 |
22 | class Cat : public Animal {
23 | public:
24 | void sayHi() {
25 | cout << "喵~" << endl;
26 | }
27 | };
28 |
29 | class Dog : public Animal {
30 | public:
31 | void sayHi() {
32 | cout << "汪汪汪~" << endl;
33 | }
34 | };
35 |
36 | int main() {
37 | Cat c;
38 | Dog d;
39 | c.eat();
40 | d.eat();
41 |
42 | c.sayHi();
43 | d.sayHi();
44 |
45 | return 0;
46 | }
47 |
--------------------------------------------------------------------------------
/08、继承/08_02_继承方式/08_02_继承方式.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "08_02_继承方式", "08_02_继承方式\08_02_继承方式.vcxproj", "{F006A6ED-37EC-4977-BA0C-0AEB2779BA04}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Debug|x64.ActiveCfg = Debug|x64
17 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Debug|x64.Build.0 = Debug|x64
18 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Debug|x86.ActiveCfg = Debug|Win32
19 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Debug|x86.Build.0 = Debug|Win32
20 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Release|x64.ActiveCfg = Release|x64
21 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Release|x64.Build.0 = Release|x64
22 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Release|x86.ActiveCfg = Release|Win32
23 | {F006A6ED-37EC-4977-BA0C-0AEB2779BA04}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {FB55B85C-3F9C-4D7B-AE68-B017E1C8562C}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/08、继承/08_02_继承方式/08_02_继承方式/08_02_继承方式.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {f006a6ed-37ec-4977-ba0c-0aeb2779ba04}
25 | My0802继承方式
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/08、继承/08_02_继承方式/08_02_继承方式/08_02_继承方式.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/08、继承/08_02_继承方式/08_02_继承方式/08_02_继承方式.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/08、继承/08_02_继承方式/08_02_继承方式/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 |
5 | /*
6 | 继承方式
7 |
8 | class 子类名 : 继承方式 父类名 {};
9 |
10 | 公共 public
11 | 保护 protected
12 | 私有 private
13 |
14 | 3 x 3 = 9
15 |
16 | | public | protected | private
17 | public | public | protected | 无法访问
18 | protected | protected | protected | 无法访问
19 | private | private | private | 无法访问
20 |
21 | public: 类内可以访问,类外也可以访问
22 | protected: 类内可以访问,类外不可访问,且子类可以访问
23 | private: 类内可以访问,类外不可访问,且子类不可访问
24 | */
25 |
26 | class Animal {
27 | public:
28 | int m_pub;
29 | protected:
30 | int m_pro;
31 | private:
32 | int m_pri;
33 | };
34 |
35 | class Cat : public Animal {
36 | public:
37 | Cat() {
38 | m_pub = 1;
39 | m_pro = 2;
40 | // m_pri = 3; 父类私有成员,子类公有继承,无法访问
41 | }
42 | };
43 |
44 | class BossCat : public Cat {
45 | public:
46 | BossCat() {
47 | m_pub = 1;
48 | m_pro = 2; // 父类Cat中不是私有
49 | }
50 | };
51 |
52 | void testCat() {
53 | Cat c;
54 | c.m_pub = 1;
55 | // c.m_pro = 2; // 要么是私有,要么是保护
56 | }
57 |
58 | class Dog : protected Animal {
59 | public:
60 | Dog() {
61 | m_pub = 1;
62 | m_pro = 2;
63 | // m_pri = 3; // 父类私有成员,子类保护继承,无法访问
64 | }
65 | };
66 |
67 | class PoliceDog : public Dog {
68 | public:
69 | PoliceDog() {
70 | m_pub = 1; // 这个变量,在父类 Dog 中一定不是私有成员
71 | m_pro = 2; // 这个变量,在父类 Dog 中一定不是私有成员
72 | }
73 | };
74 |
75 | void testDog() {
76 | Dog d;
77 | // d.m_pub = 1; // 要么是保护,要么是私有
78 | // d.m_pro = 2; // 要么是保护,要么是私有
79 | }
80 |
81 | class Pig : private Animal {
82 | public:
83 | Pig() {
84 | m_pub = 1;
85 | m_pro = 2;
86 | // m_pri = 3; // 父类私有成员,子类私有继承,无法访问
87 | }
88 | };
89 |
90 | class WildPig : public Pig {
91 | public:
92 | WildPig() {
93 | // m_pub = 1; // 该变量在父类 Pig 中是私有的
94 | // m_pro = 2; // 该变量在父类 Pig 中是私有的
95 | }
96 | };
97 |
98 | void testPig() {
99 | Pig p;
100 | // p.m_pub = 1; // 要么是保护,要么是私有
101 | // p.m_pro = 2; // 要么是保护,要么是私有
102 | }
103 |
104 | int main() {
105 |
106 |
107 | return 0;
108 | }
--------------------------------------------------------------------------------
/08、继承/08_03_构造和析构顺序/08_03_构造和析构顺序.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "08_03_构造和析构顺序", "08_03_构造和析构顺序\08_03_构造和析构顺序.vcxproj", "{C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Debug|x64.ActiveCfg = Debug|x64
17 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Debug|x64.Build.0 = Debug|x64
18 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Debug|x86.ActiveCfg = Debug|Win32
19 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Debug|x86.Build.0 = Debug|Win32
20 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Release|x64.ActiveCfg = Release|x64
21 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Release|x64.Build.0 = Release|x64
22 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Release|x86.ActiveCfg = Release|Win32
23 | {C3FEC3AD-053D-4C2F-ABD0-3B88958ADB5C}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {74680E8C-3661-4025-B51E-A06EC45CC9A3}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/08、继承/08_03_构造和析构顺序/08_03_构造和析构顺序/08_03_构造和析构顺序.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/08、继承/08_03_构造和析构顺序/08_03_构造和析构顺序/08_03_构造和析构顺序.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/08、继承/08_03_构造和析构顺序/08_03_构造和析构顺序/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | /*
5 | 继承中,构造链里,先构造的后析构
6 |
7 | d -> c -> b -> a
8 |
9 | a b c d d c b a
10 | */
11 |
12 | class Animal {
13 | public:
14 | Animal() {
15 | cout << "Animal 构造" << endl;
16 | }
17 | ~Animal() {
18 | cout << "Animal 析构" << endl;
19 | }
20 | };
21 |
22 | class Cat: public Animal {
23 | public:
24 | Cat() {
25 | cout << "Cat 构造" << endl;
26 | }
27 | ~Cat() {
28 | cout << "Cat 析构" << endl;
29 | }
30 | };
31 |
32 | class BossCat : public Cat {
33 | public:
34 | BossCat() {
35 | cout << "BossCat 构造" << endl;
36 | }
37 | ~BossCat() {
38 | cout << "BossCat 析构" << endl;
39 | }
40 | };
41 |
42 | void Test() {
43 | // Animal a;
44 | // Cat c;
45 | BossCat b;
46 | }
47 |
48 |
49 | int main() {
50 | Test();
51 | return 0;
52 | }
--------------------------------------------------------------------------------
/08、继承/08_04_同名属性访问/08_04_同名属性访问.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "08_04_同名属性访问", "08_04_同名属性访问\08_04_同名属性访问.vcxproj", "{5ED41384-00AA-4B50-B83D-A30408B5BD41}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Debug|x64.ActiveCfg = Debug|x64
17 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Debug|x64.Build.0 = Debug|x64
18 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Debug|x86.ActiveCfg = Debug|Win32
19 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Debug|x86.Build.0 = Debug|Win32
20 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Release|x64.ActiveCfg = Release|x64
21 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Release|x64.Build.0 = Release|x64
22 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Release|x86.ActiveCfg = Release|Win32
23 | {5ED41384-00AA-4B50-B83D-A30408B5BD41}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {56703E71-DE4F-4921-BE65-0C1E29EC1030}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/08、继承/08_04_同名属性访问/08_04_同名属性访问/08_04_同名属性访问.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/08、继承/08_04_同名属性访问/08_04_同名属性访问/08_04_同名属性访问.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/08、继承/08_04_同名属性访问/08_04_同名属性访问/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class Animal {
5 | public:
6 | Animal() {
7 | m_Data = 17891;
8 | }
9 | int m_Data;
10 | };
11 |
12 | class Cat : public Animal {
13 | public:
14 | Cat() {
15 | m_Data = 29812;
16 | }
17 | int m_Data;
18 | };
19 |
20 | void Test() {
21 | Cat c;
22 | cout << c.m_Data << endl;
23 | cout << c.Animal::m_Data << endl;
24 |
25 | cout << &(c.m_Data) << endl;
26 | cout << &(c.Animal::m_Data) << endl;
27 | }
28 |
29 | int main() {
30 |
31 | Test();
32 | return 0;
33 | }
--------------------------------------------------------------------------------
/08、继承/08_05_同名函数访问/08_05_同名函数访问.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "08_05_同名函数访问", "08_05_同名函数访问\08_05_同名函数访问.vcxproj", "{1199ED94-535F-4B12-ABCC-735E556168E9}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Debug|x64.ActiveCfg = Debug|x64
17 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Debug|x64.Build.0 = Debug|x64
18 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Debug|x86.ActiveCfg = Debug|Win32
19 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Debug|x86.Build.0 = Debug|Win32
20 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Release|x64.ActiveCfg = Release|x64
21 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Release|x64.Build.0 = Release|x64
22 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Release|x86.ActiveCfg = Release|Win32
23 | {1199ED94-535F-4B12-ABCC-735E556168E9}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {FFC0E7A0-2772-47F4-B0C4-312645F541EE}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/08、继承/08_05_同名函数访问/08_05_同名函数访问/08_05_同名函数访问.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/08、继承/08_05_同名函数访问/08_05_同名函数访问/08_05_同名函数访问.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/08、继承/08_05_同名函数访问/08_05_同名函数访问/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class Animal {
5 | public:
6 | Animal() {
7 |
8 | }
9 | void eat() {
10 | cout << "动物吃东西" << endl;
11 | }
12 | };
13 |
14 | class Cat: public Animal {
15 | public:
16 | Cat() {
17 |
18 | }
19 | void eat() {
20 | Animal::eat();
21 | cout << "猫吃东西" << endl;
22 | }
23 | };
24 |
25 |
26 | int main() {
27 | Cat c;
28 | c.eat();
29 | // c.Animal::eat();
30 |
31 | return 0;
32 | }
--------------------------------------------------------------------------------
/08、继承/08_06_多继承/08_06_多继承.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "08_06_多继承", "08_06_多继承\08_06_多继承.vcxproj", "{A5CFF79D-886B-49CB-9BB1-40819B038F08}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Debug|x64.ActiveCfg = Debug|x64
17 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Debug|x64.Build.0 = Debug|x64
18 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Debug|x86.ActiveCfg = Debug|Win32
19 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Debug|x86.Build.0 = Debug|Win32
20 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Release|x64.ActiveCfg = Release|x64
21 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Release|x64.Build.0 = Release|x64
22 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Release|x86.ActiveCfg = Release|Win32
23 | {A5CFF79D-886B-49CB-9BB1-40819B038F08}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {03FD0982-C868-4DE6-B887-911A0D75076A}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/08、继承/08_06_多继承/08_06_多继承/08_06_多继承.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {a5cff79d-886b-49cb-9bb1-40819b038f08}
25 | My0806多继承
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/08、继承/08_06_多继承/08_06_多继承/08_06_多继承.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/08、继承/08_06_多继承/08_06_多继承/08_06_多继承.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/08、继承/08_06_多继承/08_06_多继承/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class BaseA {
5 | public:
6 | int m_A;
7 | int m_Base;
8 |
9 | BaseA() : m_A(0), m_Base(520) {}
10 | };
11 |
12 | class BaseB {
13 | public:
14 | int m_B;
15 | int m_Base;
16 |
17 | BaseB() : m_B(0), m_Base(1314) {}
18 | };
19 |
20 | class BaseC {
21 | public:
22 | int m_C;
23 |
24 | BaseC() : m_C(0) {}
25 | };
26 |
27 | class Son : public BaseA, public BaseB, public BaseC {
28 |
29 | };
30 |
31 | int main() {
32 | Son s;
33 | s.m_A = 1;
34 | s.m_B = 2;
35 | s.m_C = 3;
36 | // s.m_Base = 8;
37 | s.BaseA::m_Base = 8;
38 | s.BaseB::m_Base = 9;
39 | cout << &s.BaseA::m_Base << endl;
40 | cout << &s.BaseB::m_Base << endl;
41 | cout << sizeof(s) << endl;
42 |
43 | return 0;
44 | }
--------------------------------------------------------------------------------
/09、多态/09_01_多态的语法/09_01_多态的语法.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "09_01_多态的语法", "09_01_多态的语法\09_01_多态的语法.vcxproj", "{BF5D4DBE-2689-49C0-B955-544467D7ED5F}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Debug|x64.ActiveCfg = Debug|x64
17 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Debug|x64.Build.0 = Debug|x64
18 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Debug|x86.ActiveCfg = Debug|Win32
19 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Debug|x86.Build.0 = Debug|Win32
20 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Release|x64.ActiveCfg = Release|x64
21 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Release|x64.Build.0 = Release|x64
22 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Release|x86.ActiveCfg = Release|Win32
23 | {BF5D4DBE-2689-49C0-B955-544467D7ED5F}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {B2083AF8-71F5-47E4-8837-D0767C131675}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/09、多态/09_01_多态的语法/09_01_多态的语法/09_01_多态的语法.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/09、多态/09_01_多态的语法/09_01_多态的语法/09_01_多态的语法.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/09、多态/09_01_多态的语法/09_01_多态的语法/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class Animal {
5 | public:
6 | // 虚函数
7 | virtual void eat() {
8 | cout << "动物在吃东西" << endl;
9 | }
10 | };
11 |
12 | class Cat : public Animal {
13 | public:
14 | void eat() {
15 | cout << "猫在吃东西" << endl;
16 | }
17 | };
18 |
19 | class Pig : public Animal {
20 | public:
21 | void eat() {
22 | cout << "猪在吃东西" << endl;
23 | }
24 | };
25 |
26 |
27 | // main -> test -> eat -> Animal::eat
28 | // 函数传参是个动物,但是传入不同的动物,会产生不同的行为,这就叫多态
29 | void eat(Animal& a) {
30 | a.eat();
31 | }
32 |
33 | void Test() {
34 | Cat c;
35 | Pig p;
36 | eat(c);
37 | eat(p);
38 | }
39 |
40 | int main() {
41 | Test();
42 | return 0;
43 | }
--------------------------------------------------------------------------------
/09、多态/09_02_虚函数/09_02_虚函数.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "09_02_虚函数", "09_02_虚函数\09_02_虚函数.vcxproj", "{84C82B11-F5FC-411E-81FD-1E83E474ECD3}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Debug|x64.ActiveCfg = Debug|x64
17 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Debug|x64.Build.0 = Debug|x64
18 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Debug|x86.ActiveCfg = Debug|Win32
19 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Debug|x86.Build.0 = Debug|Win32
20 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Release|x64.ActiveCfg = Release|x64
21 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Release|x64.Build.0 = Release|x64
22 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Release|x86.ActiveCfg = Release|Win32
23 | {84C82B11-F5FC-411E-81FD-1E83E474ECD3}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {1A3A8E35-8F9D-4658-9BE2-E02E6142D3C8}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/09、多态/09_02_虚函数/09_02_虚函数/09_02_虚函数.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 17.0
23 | Win32Proj
24 | {84c82b11-f5fc-411e-81fd-1e83e474ecd3}
25 | My0902虚函数
26 | 10.0
27 |
28 |
29 |
30 | Application
31 | true
32 | v143
33 | Unicode
34 |
35 |
36 | Application
37 | false
38 | v143
39 | true
40 | Unicode
41 |
42 |
43 | Application
44 | true
45 | v143
46 | Unicode
47 |
48 |
49 | Application
50 | false
51 | v143
52 | true
53 | Unicode
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | Level3
76 | true
77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
78 | true
79 |
80 |
81 | Console
82 | true
83 |
84 |
85 |
86 |
87 | Level3
88 | true
89 | true
90 | true
91 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
92 | true
93 |
94 |
95 | Console
96 | true
97 | true
98 | true
99 |
100 |
101 |
102 |
103 | Level3
104 | true
105 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
106 | true
107 |
108 |
109 | Console
110 | true
111 |
112 |
113 |
114 |
115 | Level3
116 | true
117 | true
118 | true
119 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
120 | true
121 |
122 |
123 | Console
124 | true
125 | true
126 | true
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/09、多态/09_02_虚函数/09_02_虚函数/09_02_虚函数.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/09、多态/09_02_虚函数/09_02_虚函数/09_02_虚函数.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/09、多态/09_02_虚函数/09_02_虚函数/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class Animal {
5 | public:
6 | virtual void eat() {
7 | cout << "动物在吃东西" << endl;
8 | }
9 | virtual void run() {
10 | cout << "动物在跑" << endl;
11 | }
12 | };
13 |
14 | class Cat: public Animal {
15 | public:
16 | void eat() {
17 | cout << "猫在吃东西" << endl;
18 | }
19 | };
20 |
21 | void eat(Animal& a) {
22 | Animal b;
23 | a.eat();
24 | }
25 |
26 | void Test() {
27 | Cat c;
28 | eat(c);
29 | cout << "Animal's size = " << sizeof(Animal) << endl;
30 | }
31 |
32 |
33 | int main() {
34 | Test();
35 | return 0;
36 | }
--------------------------------------------------------------------------------
/09、多态/09_03_纯虚函数和抽象类/09_03_纯虚函数和抽象类.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "09_03_纯虚函数和抽象类", "09_03_纯虚函数和抽象类\09_03_纯虚函数和抽象类.vcxproj", "{816708AC-E313-4A25-B0B8-3A0C8531B7F1}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Debug|x64.ActiveCfg = Debug|x64
17 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Debug|x64.Build.0 = Debug|x64
18 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Debug|x86.ActiveCfg = Debug|Win32
19 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Debug|x86.Build.0 = Debug|Win32
20 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Release|x64.ActiveCfg = Release|x64
21 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Release|x64.Build.0 = Release|x64
22 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Release|x86.ActiveCfg = Release|Win32
23 | {816708AC-E313-4A25-B0B8-3A0C8531B7F1}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {C6ECC1C6-EBDF-4375-BEF5-B951FBD8BEEA}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/09、多态/09_03_纯虚函数和抽象类/09_03_纯虚函数和抽象类/09_03_纯虚函数和抽象类.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/09、多态/09_03_纯虚函数和抽象类/09_03_纯虚函数和抽象类/09_03_纯虚函数和抽象类.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/09、多态/09_03_纯虚函数和抽象类/09_03_纯虚函数和抽象类/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | class Animal {
6 | public:
7 | virtual void eat() = 0;
8 | };
9 |
10 | class Cat : public Animal {
11 | public:
12 | virtual void eat() {
13 | cout << "èÔÚ³Ô¶«Î÷" << endl;
14 | }
15 | };
16 |
17 | int main() {
18 | Cat c;
19 | c.eat();
20 |
21 | return 0;
22 | }
--------------------------------------------------------------------------------
/09、多态/09_04_虚析构和纯虚析构/09_04_虚析构和纯虚析构.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "09_04_虚析构和纯虚析构", "09_04_虚析构和纯虚析构\09_04_虚析构和纯虚析构.vcxproj", "{4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Debug|x64.ActiveCfg = Debug|x64
17 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Debug|x64.Build.0 = Debug|x64
18 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Debug|x86.ActiveCfg = Debug|Win32
19 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Debug|x86.Build.0 = Debug|Win32
20 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Release|x64.ActiveCfg = Release|x64
21 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Release|x64.Build.0 = Release|x64
22 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Release|x86.ActiveCfg = Release|Win32
23 | {4503C7A5-4ED3-482B-B6F9-D73CBA0A19F7}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {3ABF8807-F711-42A5-A399-EF0B625B7B7B}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/09、多态/09_04_虚析构和纯虚析构/09_04_虚析构和纯虚析构/09_04_虚析构和纯虚析构.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/09、多态/09_04_虚析构和纯虚析构/09_04_虚析构和纯虚析构/09_04_虚析构和纯虚析构.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/09、多态/09_04_虚析构和纯虚析构/09_04_虚析构和纯虚析构/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | class BaseA {
5 | public:
6 | BaseA() {}
7 | ~BaseA() {
8 | cout << "BaseA 销毁了" << endl;
9 | }
10 | };
11 |
12 | class SonA : public BaseA {
13 | public:
14 | SonA() : m_Value(NULL) {
15 | m_Value = new int(10);
16 | }
17 | ~SonA() {
18 | cout << "SonA 销毁了" << endl;
19 | delete m_Value;
20 | }
21 | int* m_Value;
22 | };
23 |
24 |
25 | class BaseB {
26 | public:
27 | BaseB() {}
28 | /*virtual ~BaseB() {
29 | cout << "BaseB 销毁了" << endl;
30 | }*/
31 | virtual ~BaseB() = 0;
32 | };
33 |
34 | BaseB::~BaseB() {
35 | cout << "BaseB 销毁了" << endl;
36 | }
37 |
38 | class SonB : public BaseB {
39 | public:
40 | SonB() : m_Value(NULL) {
41 | m_Value = new int(10);
42 | }
43 | ~SonB() {
44 | cout << "SonB 销毁了" << endl;
45 | delete m_Value;
46 | }
47 | int* m_Value;
48 | };
49 |
50 | int main() {
51 | BaseA* a = new SonA();
52 | delete a;
53 |
54 | BaseB* b = new SonB();
55 | delete b;
56 |
57 | // BaseB x; 抽象类无法进行实例化
58 |
59 | return 0;
60 | }
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.10.34928.147
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "minesweeper", "minesweeper\minesweeper.vcxproj", "{8A10F18D-80E2-44AF-83E4-CFC829A88541}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Debug|x64.ActiveCfg = Debug|x64
17 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Debug|x64.Build.0 = Debug|x64
18 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Debug|x86.ActiveCfg = Debug|Win32
19 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Debug|x86.Build.0 = Debug|Win32
20 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Release|x64.ActiveCfg = Release|x64
21 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Release|x64.Build.0 = Release|x64
22 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Release|x86.ActiveCfg = Release|Win32
23 | {8A10F18D-80E2-44AF-83E4-CFC829A88541}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {CB838530-F025-45D1-9F7F-70123FE30EB7}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/Grid.cpp:
--------------------------------------------------------------------------------
1 | #include "Grid.h"
2 |
3 | Grid::Grid() {
4 | m_realGridType = GridType::GT_EMPTY;
5 | m_showGridType = GridType::GT_EMPTY;
6 | }
7 |
8 | void Grid::SetShowGridType(GridType showGridType) {
9 | m_showGridType = showGridType;
10 | }
11 | void Grid::SetRealGridType(GridType realGridType) {
12 | m_realGridType = realGridType;
13 | }
14 | void Grid::ShowGrid() {
15 | m_showGridType = m_realGridType;
16 | }
17 |
18 | GridType Grid::GetShowGridType() const {
19 | return m_showGridType;
20 | }
21 |
22 | bool Grid::IsEmpty() const {
23 | return m_realGridType == GridType::GT_EMPTY;
24 | }
25 | bool Grid::IsRealBomb() const {
26 | return m_realGridType == GridType::GT_BOMB;
27 | }
28 | bool Grid::IsShowBomb() const {
29 | return m_showGridType == GridType::GT_BOMB;
30 | }
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/Grid.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "const.h"
3 |
4 | class Grid {
5 | public:
6 | Grid();
7 | void SetShowGridType(GridType showGridType);
8 | void SetRealGridType(GridType realGridType);
9 | void ShowGrid();
10 | GridType GetShowGridType() const;
11 | bool IsEmpty() const;
12 | bool IsRealBomb() const;
13 | bool IsShowBomb() const;
14 |
15 | private:
16 | GridType m_realGridType;
17 | GridType m_showGridType;
18 | };
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/Map.cpp:
--------------------------------------------------------------------------------
1 | #include "Map.h"
2 |
3 | void Map::init(RenderWindow* win, Sprite* sprite) {
4 | pwin = win;
5 | psprite = sprite;
6 | initGame();
7 | }
8 |
9 | void Map::initGame() {
10 | isRunning = true;
11 | for (int i = 1; i <= MAP_COL; ++i) {
12 | for (int j = 1; j <= MAP_ROW; ++j) {
13 | grid[i][j].SetShowGridType(GridType::GT_HIDE);
14 | if (rand() % 6 == 0) {
15 | grid[i][j].SetRealGridType(GridType::GT_BOMB);
16 | }
17 | else {
18 | grid[i][j].SetRealGridType(GridType::GT_EMPTY);
19 | }
20 | }
21 | }
22 | for (int i = 1; i <= MAP_COL; ++i) {
23 | for (int j = 1; j <= MAP_ROW; ++j) {
24 | if (grid[i][j].IsEmpty()) {
25 | int cnt = 0;
26 | for (int k = 0; k < 8; ++k) {
27 | int ti = i + DIR[k][0];
28 | int tj = j + DIR[k][1];
29 | if (grid[ti][tj].IsRealBomb()) {
30 | ++cnt;
31 | }
32 | }
33 | grid[i][j].SetRealGridType((GridType)cnt);
34 | }
35 | }
36 | }
37 | }
38 |
39 | void Map::handleMouseEvent(Event& e, int x, int y) {
40 | if (e.type == Event::MouseButtonPressed) {
41 | if (e.key.code == Mouse::Left) {
42 | if (isRunning) {
43 | grid[x][y].ShowGrid();
44 | if (grid[x][y].IsShowBomb()) {
45 | isRunning = false;
46 | }
47 | }
48 | else {
49 | initGame();
50 | }
51 | }
52 | else if (e.key.code == Mouse::Right) {
53 | grid[x][y].SetShowGridType(GridType::GT_FLAG);
54 | }
55 | }
56 | }
57 |
58 | void Map::draw(int x, int y) {
59 | psprite->setScale(Vector2f(GRID_SIZE * 1.0 / ORI_GRID_SIZE, GRID_SIZE * 1.0 / ORI_GRID_SIZE));
60 | for (int i = 1; i <= MAP_COL; ++i) {
61 | for (int j = 1; j <= MAP_ROW; ++j) {
62 | if (!isRunning) {
63 | grid[i][j].ShowGrid();
64 | }
65 | psprite->setTextureRect(IntRect(ORI_GRID_SIZE * grid[i][j].GetShowGridType(), 0, ORI_GRID_SIZE, ORI_GRID_SIZE));
66 | psprite->setPosition(0.0f + i * GRID_SIZE, 0.0f + j * GRID_SIZE);
67 | pwin->draw(*psprite);
68 | }
69 | }
70 | }
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/Map.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include "Grid.h"
4 | using namespace sf;
5 |
6 | class Map {
7 | private:
8 | void initGame();
9 | public:
10 | void init(RenderWindow* win, Sprite* sprite);
11 | void handleMouseEvent(Event& e, int x, int y);
12 | void draw(int x, int y);
13 | private:
14 | Grid grid[MAP_COL + 1][MAP_ROW + 1];
15 | RenderWindow* pwin;
16 | Sprite* psprite;
17 | bool isRunning;
18 | };
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/const.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | const int DIR[8][2] = {
3 | {-1, -1}, {-1, 0}, {-1, 1},
4 | {0, -1}, {0, 1},
5 | {1, -1}, {1, 0}, {1, 1}
6 | };
7 |
8 | const int ORI_GRID_SIZE = 96;
9 | const int GRID_SIZE = 48;
10 | const int MAP_COL = 15;
11 | const int MAP_ROW = 10;
12 | const int WIN_W = GRID_SIZE * (1 + MAP_COL + 1);
13 | const int WIN_H = GRID_SIZE * (1 + MAP_ROW + 1);
14 |
15 | enum GridType {
16 | GT_EMPTY = 0,
17 | GT_COUNT_1 = 1,
18 | GT_COUNT_2 = 2,
19 | GT_COUNT_3 = 3,
20 | GT_COUNT_4 = 4,
21 | GT_COUNT_5 = 5,
22 | GT_COUNT_6 = 6,
23 | GT_COUNT_7 = 7,
24 | GT_COUNT_8 = 8,
25 | GT_BOMB = 9,
26 | GT_HIDE = 10,
27 | GT_FLAG = 11,
28 | };
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Grid.h"
4 | #include "Map.h"
5 | using namespace sf;
6 |
7 | int main() {
8 | RenderWindow win(VideoMode(WIN_W, WIN_H), L"扫雷" );
9 | Texture t;
10 | t.loadFromFile("mine.png");
11 | Sprite s(t);
12 | Map mp;
13 |
14 | mp.init(&win, &s);
15 | while ( win.isOpen() ) {
16 | Event e;
17 | Vector2i pos = Mouse::getPosition(win);
18 | int x = pos.x / GRID_SIZE;
19 | int y = pos.y / GRID_SIZE;
20 |
21 | while (win.pollEvent(e)) {
22 | if (e.type == Event::Closed) {
23 | std::cout << "按下关闭按钮" << std::endl;
24 | win.close();
25 | }
26 | mp.handleMouseEvent(e, x, y);
27 | }
28 | mp.draw(x, y);
29 | win.display();
30 | }
31 | return 0;
32 | }
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/mine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/mine.png
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/minesweeper.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 | 源文件
23 |
24 |
25 | 源文件
26 |
27 |
28 |
29 |
30 | 头文件
31 |
32 |
33 | 头文件
34 |
35 |
36 | 头文件
37 |
38 |
39 |
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/minesweeper.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/openal32.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/openal32.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-audio-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-audio-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-audio-d-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-audio-d-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-graphics-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-graphics-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-graphics-d-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-graphics-d-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-network-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-network-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-network-d-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-network-d-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-system-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-system-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-system-d-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-system-d-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-window-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-window-2.dll
--------------------------------------------------------------------------------
/99、项目实战/minesweeper/minesweeper/sfml-window-d-2.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/WhereIsHeroFrom/cpp_oop/1b2d756f2b5ee5a5195ac307fa432ed683593aab/99、项目实战/minesweeper/minesweeper/sfml-window-d-2.dll
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # cpp_oop
2 | C++面向对象
3 | # 作者:英雄哪里出来
4 | # 公众号:夜深人静写算法
5 | # 知识星球:英雄算法联盟
6 |
--------------------------------------------------------------------------------