├── 10
├── 10.vcxproj
├── 10.vcxproj.filters
├── AutoKeywordExample.cpp
├── AutoKeywordExample.h
├── Ball.cpp
├── Ball.h
├── DefaultDeleteFinalOverrideExample.cpp
├── DefaultDeleteFinalOverrideExample.h
├── EColor.h
├── EnumExample.cpp
├── EnumExample.h
├── Human.cpp
├── Human.h
├── IntVector3D.h
├── IntVector4D.h
├── MyVector.h
├── Pope.cpp
├── Pope.h
├── PopeClone.cpp
├── PopeClone.h
├── StaticAssertExample.cpp
├── StaticAssertExample.h
└── main.cpp
├── 11
├── 11.vcxproj
├── 11.vcxproj.filters
├── MyVector.cpp
├── MyVector.h
├── RangeBasedForLoopExample.cpp
├── RangeBasedForLoopExample.h
├── SpeedTestExample.cpp
├── SpeedTestExample.h
├── UsingUniquePointersExample.cpp
├── UsingUniquePointersExample.h
└── main.cpp
├── 12
├── 12.vcxproj
├── 12.vcxproj.filters
├── CacheExample.cpp
├── CacheExample.h
├── MoveConstructorAssignmentExample.cpp
├── MoveConstructorAssignmentExample.h
├── MyString.cpp
├── MyString.h
├── MyVector2D.cpp
├── MyVector2D.h
├── Node.cpp
├── Node.h
├── SimpleCache.cpp
├── SimpleCache.h
├── SinglyLinkedListExample.cpp
├── SinglyLinkedListExample.h
└── main.cpp
├── 13
├── 13.vcxproj
├── 13.vcxproj.filters
├── Bar.cpp
├── Bar.h
├── Foo.cpp
├── Foo.h
├── LambdaExpressionsExample.cpp
├── LambdaExpressionsExample.h
├── Node.cpp
├── Node.h
├── SimpleData.h
├── SimpleHashMap.cpp
├── SimpleHashMap.h
├── SimpleHashMapExample.cpp
├── SimpleHashMapExample.h
├── VariadicTemplateFunctionExample.cpp
├── VariadicTemplateFunctionExample.h
└── main.cpp
├── 14
├── 14.vcxproj
├── 14.vcxproj.filters
├── AutoResetTimerExample.cpp
├── AutoResetTimerExample.h
├── FileSystemExample.cpp
├── FileSystemExample.h
├── FileSystemExample
│ └── test.txt
├── MultiThreadingExample.cpp
├── MultiThreadingExample.h
└── main.cpp
├── .gitignore
├── 01
├── 01.vcxproj
├── 01.vcxproj.filters
├── AddIntegers.cpp
├── AddIntegers.h
├── HelloPope.cpp
├── HelloPope.h
├── HiPope.cpp
├── HiPope.h
├── Main.cpp
├── PrintEverything.cpp
├── PrintEverything.h
├── PrintMenu.cpp
├── PrintMenu.h
├── ReverseInputString.cpp
├── ReverseInputString.h
├── Swap.cpp
└── Swap.h
├── 02
├── 02.vcxproj
├── 02.vcxproj.filters
├── Main.cpp
├── MirrorString.cpp
├── MirrorString.h
├── PrintRecord.cpp
├── PrintRecord.h
├── PrintRecords.cpp
├── PrintRecords.h
├── Vector1.cpp
├── Vector1.h
├── Vector2.cpp
├── Vector2.h
├── VectorExample1.cpp
├── VectorExample1.h
├── VectorExample2.cpp
├── VectorExample2.h
└── studentRecords.dat
├── 03
├── 03.vcxproj
├── 03.vcxproj.filters
├── Animal.cpp
├── Animal.h
├── Cat.cpp
├── Cat.h
├── CopyConstructorExample.cpp
├── CopyConstructorExample.h
├── Dog.cpp
├── Dog.h
├── FunctionOverloadExample.cpp
├── FunctionOverloadExample.h
├── InheritanceExample.cpp
├── InheritanceExample.h
├── Main.cpp
├── OperatorOverloadExample.cpp
├── OperatorOverloadExample.h
├── String.cpp
├── String.h
├── Vector.cpp
├── Vector.h
├── Vector2.cpp
└── Vector2.h
├── 04
├── 04.vcxproj
├── 04.vcxproj.filters
├── Animal.cpp
├── Animal.h
├── Bat.cpp
├── Bat.h
├── Cat.cpp
├── Cat.h
├── Cat1.cpp
├── Cat1.h
├── Dog.cpp
├── Dog.h
├── IFlyable.h
├── IWalkable.h
├── InterfaceExample.cpp
├── InterfaceExample.h
├── Liger.cpp
├── Liger.h
├── Lion.cpp
├── Lion.h
├── Main.cpp
├── MultiInheritanceExample.cpp
├── MultiInheritanceExample.h
├── PolymorphismExample.cpp
├── PolymorphismExample.h
├── Tiger.cpp
└── Tiger.h
├── 05
├── 05.vcxproj
├── 05.vcxproj.filters
├── Animal.cpp
├── Animal.h
├── Cat.cpp
├── Cat.h
├── Cat2.cpp
├── Cat2.h
├── Dog.cpp
├── Dog.h
├── Main.cpp
├── Math.cpp
├── Math.h
├── ObjectAddressSavingExample.cpp
├── ObjectAddressSavingExample.h
├── ObjectPointerCastingExample.cpp
├── ObjectPointerCastingExample.h
├── StaticMemberFunctionExample.cpp
├── StaticMemberFunctionExample.h
├── StaticMemberVariableExample.cpp
├── StaticMemberVariableExample.h
├── Tiger.cpp
├── Tiger.h
├── ValueCastingExample.cpp
├── ValueCastingExample.h
├── Vector.cpp
├── Vector.h
├── Vector2.cpp
└── Vector2.h
├── 06
├── 06.vcxproj
├── 06.vcxproj.filters
├── Main.cpp
├── ObjectVectorExample.cpp
├── ObjectVectorExample.h
├── PointerVectorExample.cpp
├── PointerVectorExample.h
├── Score.cpp
├── Score.h
├── VectorAddingElementsExample.cpp
├── VectorAddingElementsExample.h
├── VectorCalculatingSumExample.cpp
├── VectorCalculatingSumExample.h
├── VectorSwappingElementsExample.cpp
└── VectorSwappingElementsExample.h
├── 07
├── 07.vcxproj
├── 07.vcxproj.filters
├── Main.cpp
├── MapInsertingExample.cpp
├── MapInsertingExample.h
├── MapInsertingWithOperatorExample.cpp
├── MapInsertingWithOperatorExample.h
├── MapUserDefinedTypesExample1.cpp
├── MapUserDefinedTypesExample1.h
├── MapUserDefinedTypesExample2.cpp
├── MapUserDefinedTypesExample2.h
├── StudentInfo.cpp
├── StudentInfo.h
├── StudentInfo2.cpp
├── StudentInfo2.h
└── StudentInfo2Comparer.h
├── 09
├── 09.vcxproj
├── 09.vcxproj.filters
├── Algorithm.cpp
├── Algorithm.h
├── CopyingVectorExample.cpp
├── CopyingVectorExample.h
├── FindFunctionExample.cpp
├── FindFunctionExample.h
├── FixedVector.h
├── FixedVectorExample.cpp
├── FixedVectorExample.h
├── IntVector.cpp
├── IntVector.h
├── Main.cpp
├── Math.cpp
├── Math.h
├── MathExample.cpp
├── MathExample.h
├── MyArray.h
├── MyArrayExample.cpp
├── MyArrayExample.h
├── Score.cpp
└── Score.h
├── CODEOWNERS
├── Common.props
├── PlatformSettings.props
├── README.md
└── Samples.sln
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # .NET Core
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 | **/Properties/launchSettings.json
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # Visual Studio code coverage results
117 | *.coverage
118 | *.coveragexml
119 |
120 | # NCrunch
121 | _NCrunch_*
122 | .*crunch*.local.xml
123 | nCrunchTemp_*
124 |
125 | # MightyMoose
126 | *.mm.*
127 | AutoTest.Net/
128 |
129 | # Web workbench (sass)
130 | .sass-cache/
131 |
132 | # Installshield output folder
133 | [Ee]xpress/
134 |
135 | # DocProject is a documentation generator add-in
136 | DocProject/buildhelp/
137 | DocProject/Help/*.HxT
138 | DocProject/Help/*.HxC
139 | DocProject/Help/*.hhc
140 | DocProject/Help/*.hhk
141 | DocProject/Help/*.hhp
142 | DocProject/Help/Html2
143 | DocProject/Help/html
144 |
145 | # Click-Once directory
146 | publish/
147 |
148 | # Publish Web Output
149 | *.[Pp]ublish.xml
150 | *.azurePubxml
151 | # TODO: Comment the next line if you want to checkin your web deploy settings
152 | # but database connection strings (with potential passwords) will be unencrypted
153 | *.pubxml
154 | *.publishproj
155 |
156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
157 | # checkin your Azure Web App publish settings, but sensitive information contained
158 | # in these scripts will be unencrypted
159 | PublishScripts/
160 |
161 | # NuGet Packages
162 | *.nupkg
163 | # The packages folder can be ignored because of Package Restore
164 | **/packages/*
165 | # except build/, which is used as an MSBuild target.
166 | !**/packages/build/
167 | # Uncomment if necessary however generally it will be regenerated when needed
168 | #!**/packages/repositories.config
169 | # NuGet v3's project.json files produces more ignorable files
170 | *.nuget.props
171 | *.nuget.targets
172 |
173 | # Microsoft Azure Build Output
174 | csx/
175 | *.build.csdef
176 |
177 | # Microsoft Azure Emulator
178 | ecf/
179 | rcf/
180 |
181 | # Windows Store app package directories and files
182 | AppPackages/
183 | BundleArtifacts/
184 | Package.StoreAssociation.xml
185 | _pkginfo.txt
186 |
187 | # Visual Studio cache files
188 | # files ending in .cache can be ignored
189 | *.[Cc]ache
190 | # but keep track of directories ending in .cache
191 | !*.[Cc]ache/
192 |
193 | # Others
194 | ClientBin/
195 | ~$*
196 | *~
197 | *.dbmdl
198 | *.dbproj.schemaview
199 | *.jfm
200 | *.pfx
201 | *.publishsettings
202 | orleans.codegen.cs
203 |
204 | # Since there are multiple workflows, uncomment next line to ignore bower_components
205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
206 | #bower_components/
207 |
208 | # RIA/Silverlight projects
209 | Generated_Code/
210 |
211 | # Backup & report files from converting an old project file
212 | # to a newer Visual Studio version. Backup files are not needed,
213 | # because we have git ;-)
214 | _UpgradeReport_Files/
215 | Backup*/
216 | UpgradeLog*.XML
217 | UpgradeLog*.htm
218 |
219 | # SQL Server files
220 | *.mdf
221 | *.ldf
222 | *.ndf
223 |
224 | # Business Intelligence projects
225 | *.rdl.data
226 | *.bim.layout
227 | *.bim_*.settings
228 |
229 | # Microsoft Fakes
230 | FakesAssemblies/
231 |
232 | # GhostDoc plugin setting file
233 | *.GhostDoc.xml
234 |
235 | # Node.js Tools for Visual Studio
236 | .ntvs_analysis.dat
237 | node_modules/
238 |
239 | # Typescript v1 declaration files
240 | typings/
241 |
242 | # Visual Studio 6 build log
243 | *.plg
244 |
245 | # Visual Studio 6 workspace options file
246 | *.opt
247 |
248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
249 | *.vbw
250 |
251 | # Visual Studio LightSwitch build output
252 | **/*.HTMLClient/GeneratedArtifacts
253 | **/*.DesktopClient/GeneratedArtifacts
254 | **/*.DesktopClient/ModelManifest.xml
255 | **/*.Server/GeneratedArtifacts
256 | **/*.Server/ModelManifest.xml
257 | _Pvt_Extensions
258 |
259 | # Paket dependency manager
260 | .paket/paket.exe
261 | paket-files/
262 |
263 | # FAKE - F# Make
264 | .fake/
265 |
266 | # JetBrains Rider
267 | .idea/
268 | *.sln.iml
269 |
270 | # CodeRush
271 | .cr/
272 |
273 | # Python Tools for Visual Studio (PTVS)
274 | __pycache__/
275 | *.pyc
276 |
277 | # Cake - Uncomment if you are using it
278 | # tools/**
279 | # !tools/packages.config
280 |
281 | # Telerik's JustMock configuration file
282 | *.jmconfig
283 |
284 | # BizTalk build output
285 | *.btp.cs
286 | *.btm.cs
287 | *.odx.cs
288 | *.xsd.cs
289 |
--------------------------------------------------------------------------------
/01/01.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | 1. Namespace
7 |
8 |
9 | 1. Namespace
10 |
11 |
12 | 4. Summing Integers
13 |
14 |
15 | 3. Manipulator
16 |
17 |
18 | 6. Reference
19 |
20 |
21 | 5. Discarding & Interactive Input
22 |
23 |
24 | 2. Print Everything
25 |
26 |
27 |
28 |
29 | 1. Namespace
30 |
31 |
32 | 1. Namespace
33 |
34 |
35 | 4. Summing Integers
36 |
37 |
38 | 3. Manipulator
39 |
40 |
41 | 6. Reference
42 |
43 |
44 | 5. Discarding & Interactive Input
45 |
46 |
47 | 2. Print Everything
48 |
49 |
50 |
51 |
52 | {c8b29393-0148-42a0-ad25-5a8d8a5b8905}
53 |
54 |
55 | {e987f10b-6191-419c-a86c-3e99808a8a61}
56 |
57 |
58 | {45ea9b02-6975-4b4f-af43-d4d0b1f9fa28}
59 |
60 |
61 | {89618905-e065-4f6a-8e3d-40ce0a201d8e}
62 |
63 |
64 | {0f92a396-aa4b-4c72-9cac-1789dad8a05b}
65 |
66 |
67 | {72941ab2-ffbb-4704-b849-50a0aa636bd8}
68 |
69 |
70 |
--------------------------------------------------------------------------------
/01/AddIntegers.cpp:
--------------------------------------------------------------------------------
1 | #include "AddIntegers.h"
2 | #include
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | void AddIntegersExample()
9 | {
10 | cout << "+------------------------------+" << endl;
11 | cout << "| Add Integers Example |" << endl;
12 | cout << "+------------------------------+" << endl;
13 |
14 | int number;
15 | int sum = 0;
16 |
17 | while (true)
18 | {
19 | cout << "Please enter an integer or EOF: ";
20 | cin >> number;
21 | if (cin.eof())
22 | {
23 | break;
24 | }
25 |
26 | if (cin.fail())
27 | {
28 | cout << "Invalid input" << endl;
29 | cin.clear();
30 | cin.ignore(LLONG_MAX, '\n');
31 | continue;
32 | }
33 | sum += number;
34 | }
35 | cin.clear();
36 |
37 | cout << "The sum is " << sum << endl;
38 | }
39 | }
--------------------------------------------------------------------------------
/01/AddIntegers.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void AddIntegersExample();
6 | }
7 |
--------------------------------------------------------------------------------
/01/HelloPope.cpp:
--------------------------------------------------------------------------------
1 | #include "HelloPope.h"
2 | #include
3 |
4 | using namespace std;
5 |
6 | namespace hello
7 | {
8 | void SayHelloExample()
9 | {
10 | cout << "Hello Pope." << endl;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/01/HelloPope.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace hello
4 | {
5 | void SayHelloExample();
6 | }
--------------------------------------------------------------------------------
/01/HiPope.cpp:
--------------------------------------------------------------------------------
1 | #include "HiPope.h"
2 | #include
3 |
4 | using namespace std;
5 |
6 | namespace hi
7 | {
8 | void SayHelloExample()
9 | {
10 | cout << "Hi Pope." << endl;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/01/HiPope.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace hi
4 | {
5 | void SayHelloExample();
6 | }
--------------------------------------------------------------------------------
/01/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "AddIntegers.h"
2 | #include "HelloPope.h"
3 | #include "HiPope.h"
4 | #include "PrintEverything.h"
5 | #include "PrintMenu.h"
6 | #include "ReverseInputString.h"
7 | #include "Swap.h"
8 |
9 | using namespace samples;
10 |
11 | int main()
12 | {
13 | // 1. Namespace Example
14 | hello::SayHelloExample();
15 | hi::SayHelloExample();
16 |
17 | // 2. Print Everything
18 | PrintEverythingExample();
19 |
20 | // 3. Manipulators
21 | PrintMenuExample();
22 |
23 | // 4. Summing Integers
24 | AddIntegersExample();
25 |
26 | // 5. Discarding & Interactive Input
27 | ReverseInputStringExample();
28 |
29 | // 6. Reference
30 | SwapExample();
31 |
32 | return 0;
33 | }
--------------------------------------------------------------------------------
/01/PrintEverything.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "PrintEverything.h"
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | void PrintEverythingExample()
9 | {
10 | cout << "+------------------------------+" << endl;
11 | cout << "| Print Everything Example |" << endl;
12 | cout << "+------------------------------+" << endl;
13 |
14 | int integer = 10;
15 | float decimal = 1.5f;
16 | char letter = 'A';
17 | char string[] = "Hello, world!";
18 |
19 | cout << integer << endl;
20 | cout << decimal << endl;
21 | cout << letter << endl
22 | << string << endl;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/01/PrintEverything.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void PrintEverythingExample();
6 | }
--------------------------------------------------------------------------------
/01/PrintMenu.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "PrintMenu.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void PrintMenuExample()
10 | {
11 | cout << "+------------------------------+" << endl;
12 | cout << "| Print Menu Example |" << endl;
13 | cout << "+------------------------------+" << endl;
14 |
15 | const float coffeePrice = 1.25f;
16 | const float lattePrice = 4.75f;
17 | const float breakfastComboPrice = 12.104f;
18 |
19 | const size_t nameColumnLength = 20;
20 | const size_t priceColumnLength = 10;
21 |
22 | cout << left << fixed << showpoint << setprecision(2);
23 |
24 | cout << setfill('-') << setw(nameColumnLength + priceColumnLength) << "" << endl << setfill(' ');
25 | cout << setw(nameColumnLength) << "Name"
26 | << setw(priceColumnLength) << "Price" << endl;
27 | cout << setfill('-') << setw(nameColumnLength + priceColumnLength) << "" << endl << setfill(' ');
28 |
29 | cout << setw(nameColumnLength) << "Coffee"
30 | << "$" << coffeePrice << endl;
31 | cout << setw(nameColumnLength) << "Latte"
32 | << "$" << lattePrice << endl;
33 | cout << setw(nameColumnLength) << "Breakfast Combo"
34 | << "$" << breakfastComboPrice << endl;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/01/PrintMenu.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void PrintMenuExample();
6 | }
--------------------------------------------------------------------------------
/01/ReverseInputString.cpp:
--------------------------------------------------------------------------------
1 | #include "ReverseInputString.h"
2 | #include
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | void ReverseInputStringExample()
9 | {
10 | cout << "+------------------------------+" << endl;
11 | cout << "| Reverse String Example |" << endl;
12 | cout << "+------------------------------+" << endl;
13 |
14 | const int LINE_SIZE = 512;
15 | char line[LINE_SIZE];
16 |
17 | cout << "Please enter a string to reverse" << endl
18 | << "or EOF to quit: ";
19 |
20 | cin.getline(line, LINE_SIZE);
21 | if (cin.fail())
22 | {
23 | cin.clear();
24 | return;
25 | }
26 |
27 | char* p = line;
28 | char* q = line + strlen(line) - 1;
29 | while (p < q)
30 | {
31 | char temp = *p;
32 | *p = *q;
33 | *q = temp;
34 |
35 | ++p;
36 | --q;
37 | }
38 |
39 | cout << "Reversed string: " << line << endl;
40 | }
41 | }
--------------------------------------------------------------------------------
/01/ReverseInputString.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void ReverseInputStringExample();
6 | }
--------------------------------------------------------------------------------
/01/Swap.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Swap.h"
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | void SwapExample()
9 | {
10 | cout << "+------------------------------+" << endl;
11 | cout << "| Swap Example |" << endl;
12 | cout << "+------------------------------+" << endl;
13 | int number1 = 1;
14 | int number2 = 2;
15 |
16 | cout << "Before swap: " << number1 << ", " << number2 << endl;
17 | Swap(number1, number2);
18 | cout << "After swap: " << number1 << ", " << number2 << endl;
19 | }
20 |
21 | void Swap(int& number1, int& number2)
22 | {
23 | int temp = number1;
24 | number1 = number2;
25 | number2 = temp;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/01/Swap.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void SwapExample();
6 | void Swap(int& number1, int& number2);
7 | }
--------------------------------------------------------------------------------
/02/02.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {e5a4d6f6-a289-424b-bc24-23effd3db790}
6 |
7 |
8 | {5cf0e715-d007-470d-8666-10829ec42d56}
9 |
10 |
11 | {521554f7-0750-4c1e-81d6-658b2442e6bc}
12 |
13 |
14 | {2f84b65a-a0be-4972-ae5d-f8b03231007a}
15 |
16 |
17 | {c58f1218-17c0-4608-b236-1f8138423742}
18 |
19 |
20 |
21 |
22 | 1. Mirror String
23 |
24 |
25 | 2. StringStream
26 |
27 |
28 | 3. File I/O
29 |
30 |
31 |
32 | 4. Vector%28Header/Cpp%29
33 |
34 |
35 | 5. Vector with Member Functions
36 |
37 |
38 | 4. Vector%28Header/Cpp%29
39 |
40 |
41 | 5. Vector with Member Functions
42 |
43 |
44 |
45 |
46 | 1. Mirror String
47 |
48 |
49 | 2. StringStream
50 |
51 |
52 | 3. File I/O
53 |
54 |
55 | 4. Vector%28Header/Cpp%29
56 |
57 |
58 | 5. Vector with Member Functions
59 |
60 |
61 | 4. Vector%28Header/Cpp%29
62 |
63 |
64 | 5. Vector with Member Functions
65 |
66 |
67 |
--------------------------------------------------------------------------------
/02/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "MirrorString.h"
2 | #include "PrintRecord.h"
3 | #include "PrintRecords.h"
4 | #include "VectorExample1.h"
5 | #include "VectorExample2.h"
6 |
7 | using namespace samples;
8 |
9 | int main()
10 | {
11 | // 1. Mirror String
12 | MirrorStringExample();
13 |
14 | // 2. StringStream
15 | PrintRecordExample();
16 |
17 | // 3. File I/O
18 | ManageRecordsExample();
19 |
20 | // 4. Vector(Header/Cpp)
21 | VectorExample1();
22 |
23 | // 5. Vector with Member Functions
24 | VectorExample2();
25 |
26 | return 0;
27 | }
--------------------------------------------------------------------------------
/02/MirrorString.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "MirrorString.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void MirrorStringExample()
10 | {
11 | cout << "+------------------------------+" << endl;
12 | cout << "| Mirror String Example |" << endl;
13 | cout << "+------------------------------+" << endl;
14 |
15 | string line = "Hello World!";
16 |
17 | cout << "string to mirror: " << line << endl;
18 |
19 | for (int i = (int)line.size() - 1; i >= 0; --i)
20 | {
21 | line += line[i];
22 | }
23 | cout << "mirrored string" << line << endl;
24 | }
25 | }
--------------------------------------------------------------------------------
/02/MirrorString.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void MirrorStringExample();
6 | }
7 |
--------------------------------------------------------------------------------
/02/PrintRecord.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include "PrintRecord.h"
5 |
6 | using namespace std;
7 |
8 | namespace samples
9 | {
10 | void PrintRecordExample()
11 | {
12 | cout << "+------------------------------+" << endl;
13 | cout << "| Print Record Example |" << endl;
14 | cout << "+------------------------------+" << endl;
15 |
16 | string firstName;
17 | string lastName;
18 | string studentID;
19 | int score;
20 |
21 | istringstream inputStream("Pope Kim A12345678 80");
22 | ostringstream outputStream;
23 |
24 | inputStream >> firstName >> lastName >> studentID >> score;
25 | outputStream << firstName << " " << lastName << " " << studentID << " " << score;
26 | cout << outputStream.str() << endl;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/02/PrintRecord.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void PrintRecordExample();
6 | }
7 |
--------------------------------------------------------------------------------
/02/PrintRecords.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "PrintRecords.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | Record ReadRecord(istream& stream, bool bPrompt)
10 | {
11 | Record record;
12 |
13 | if (bPrompt)
14 | {
15 | cout << "First name: ";
16 | }
17 | stream >> record.FirstName;
18 |
19 | if (bPrompt)
20 | {
21 | cout << "Last name: ";
22 | }
23 | stream >> record.LastName;
24 |
25 | if (bPrompt)
26 | {
27 | cout << "Student ID: ";
28 | }
29 | stream >> record.StudentID;
30 |
31 | if (bPrompt)
32 | {
33 | cout << "Score: ";
34 | }
35 | stream >> record.Score;
36 |
37 | return record;
38 | }
39 |
40 | void DisplayRecords(fstream& fileStream)
41 | {
42 | fileStream.seekg(0);
43 |
44 | string line;
45 | while (true)
46 | {
47 | getline(fileStream, line);
48 |
49 | if (fileStream.eof())
50 | {
51 | fileStream.clear();
52 | break;
53 | }
54 | cout << line << endl;
55 | }
56 | }
57 |
58 | void WriteFileRecord(fstream& outputStream, const Record& record)
59 | {
60 | outputStream.seekp(0, ios_base::end);
61 |
62 | outputStream << record.FirstName << " "
63 | << record.LastName << " "
64 | << record.StudentID << " "
65 | << record.Score << endl;
66 |
67 | outputStream.flush();
68 | }
69 |
70 | void ManageRecordsExample()
71 | {
72 | cout << "+------------------------------+" << endl;
73 | cout << "| Manage Records Example |" << endl;
74 | cout << "+------------------------------+" << endl;
75 |
76 | fstream fileStream;
77 | fileStream.open("studentRecords.dat", ios_base::in | ios_base::out);
78 |
79 | bool bExit = false;
80 | while (!bExit)
81 | {
82 | char command = ' ';
83 |
84 | cout << "a: add" << endl
85 | << "d: display" << endl
86 | << "x: exit" << endl;
87 |
88 | cin >> command;
89 | cin.ignore(LLONG_MAX, '\n');
90 |
91 | switch (command)
92 | {
93 | case 'a':
94 | {
95 | Record record = ReadRecord(cin, true);
96 | WriteFileRecord(fileStream, record);
97 | break;
98 | }
99 | case 'd':
100 | {
101 | DisplayRecords(fileStream);
102 | break;
103 | }
104 | case 'x':
105 | {
106 | bExit = true;
107 | break;
108 | }
109 | default:
110 | {
111 | cout << "invalid input" << endl;
112 | break;
113 | }
114 | }
115 | }
116 |
117 | fileStream.close();
118 | }
119 | }
--------------------------------------------------------------------------------
/02/PrintRecords.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | struct Record
7 | {
8 | std::string FirstName;
9 | std::string LastName;
10 | std::string StudentID;
11 | std::string Score;
12 | };
13 |
14 | namespace samples
15 | {
16 | Record ReadRecord(std::istream& stream, bool bPrompt);
17 |
18 | void WriteFileRecord(std::fstream& outputStream, const Record& record);
19 |
20 | void DisplayRecords(std::fstream& fileStream);
21 |
22 | void ManageRecordsExample();
23 | }
24 |
--------------------------------------------------------------------------------
/02/Vector1.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Vector1.h"
3 |
4 | using namespace std;
5 |
6 | Vector1::Vector1()
7 | : mX(0)
8 | , mY(0)
9 | {
10 | cout << "Vector1(): (" << mX << ", " << mY << ")" << endl;
11 | }
12 |
13 | Vector1::Vector1(int x, int y)
14 | : mX(x)
15 | , mY(y)
16 | {
17 | cout << "Vector1(int x, int y): (" << mX << ", " << mY << ")" << endl;
18 | }
--------------------------------------------------------------------------------
/02/Vector1.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | class Vector1
4 | {
5 | public:
6 | Vector1();
7 | Vector1(int x, int y);
8 |
9 | private:
10 | int mX;
11 | int mY;
12 | };
--------------------------------------------------------------------------------
/02/Vector2.cpp:
--------------------------------------------------------------------------------
1 | #include "Vector2.h"
2 |
3 | Vector2::Vector2()
4 | : mX(0)
5 | , mY(0)
6 | {
7 | }
8 |
9 | Vector2::Vector2(int x, int y)
10 | : mX(x)
11 | , mY(y)
12 | {
13 | }
14 |
15 | int Vector2::GetX() const
16 | {
17 | return mX;
18 | }
19 |
20 | int Vector2::GetY() const
21 | {
22 | return mY;
23 | }
24 |
25 | void Vector2::SetX(int x)
26 | {
27 | mX = x;
28 | }
29 |
30 | void Vector2::SetY(int y)
31 | {
32 | mY = y;
33 | }
34 |
35 | void Vector2::Add(const Vector2& vector)
36 | {
37 | mX += vector.mX;
38 | mY += vector.mY;
39 | }
40 |
--------------------------------------------------------------------------------
/02/Vector2.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | class Vector2
4 | {
5 | public:
6 | Vector2();
7 | Vector2(int x, int y);
8 |
9 | int GetX() const;
10 | int GetY() const;
11 | void SetX(int x);
12 | void SetY(int Y);
13 |
14 | void Add(const Vector2& vector);
15 |
16 | private:
17 | int mX;
18 | int mY;
19 | };
--------------------------------------------------------------------------------
/02/VectorExample1.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Vector1.h"
3 | #include "VectorExample1.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void VectorExample1()
10 | {
11 | cout << "+------------------------------+" << endl;
12 | cout << "| Vector Example 1 |" << endl;
13 | cout << "+------------------------------+" << endl;
14 |
15 | Vector1 v1;
16 | Vector1 v2(10, 55);
17 | }
18 | }
--------------------------------------------------------------------------------
/02/VectorExample1.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void VectorExample1();
6 | }
--------------------------------------------------------------------------------
/02/VectorExample2.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Vector2.h"
3 | #include "VectorExample2.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void VectorExample2()
10 | {
11 | cout << "+------------------------------+" << endl;
12 | cout << "| Vector Example 2 |" << endl;
13 | cout << "+------------------------------+" << endl;
14 |
15 | Vector2 v1;
16 | Vector2 v2(13, 4);
17 |
18 | cout << "v1: (" << v1.GetX() << ", " << v1.GetY() << ")" << endl;
19 | cout << "v2: (" << v2.GetX() << ", " << v2.GetY() << ")" << endl;
20 |
21 | v1.SetX(10);
22 | v1.SetY(20);
23 |
24 | v2.SetX(30);
25 | v2.SetY(40);
26 |
27 | cout << "v1: (" << v1.GetX() << ", " << v1.GetY() << ")" << endl;
28 | cout << "v2: (" << v2.GetX() << ", " << v2.GetY() << ")" << endl;
29 |
30 | v1.Add(v2);
31 |
32 | cout << "v1: (" << v1.GetX() << ", " << v1.GetY() << ")" << endl;
33 | }
34 | }
--------------------------------------------------------------------------------
/02/VectorExample2.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void VectorExample2();
6 | }
--------------------------------------------------------------------------------
/02/studentRecords.dat:
--------------------------------------------------------------------------------
1 | p k 1 1
2 | p e 2 d
3 | e d 111 111
4 |
--------------------------------------------------------------------------------
/03/03.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | 1. Copy Constructor with char Array
7 |
8 |
9 | 2. Vector Class w Funciton Overloading
10 |
11 |
12 | 3. Vector Class w Operator Overloading
13 |
14 |
15 | 4. Animal, Cat, Dog Classes
16 |
17 |
18 | 4. Animal, Cat, Dog Classes
19 |
20 |
21 | 4. Animal, Cat, Dog Classes
22 |
23 |
24 | 4. Animal, Cat, Dog Classes
25 |
26 |
27 | 1. Copy Constructor with char Array
28 |
29 |
30 | 2. Vector Class w Funciton Overloading
31 |
32 |
33 | 3. Vector Class w Operator Overloading
34 |
35 |
36 |
37 |
38 | {ada907ec-a4c7-4ed2-a86d-421f9debac8e}
39 |
40 |
41 | {f93cf2ed-2993-4f00-b055-bbb6737c7534}
42 |
43 |
44 | {477e63be-5c91-43a3-94f0-f9200e4e047c}
45 |
46 |
47 | {82695e67-7097-4116-a681-db170fa55d49}
48 |
49 |
50 |
51 |
52 | 1. Copy Constructor with char Array
53 |
54 |
55 | 2. Vector Class w Funciton Overloading
56 |
57 |
58 | 3. Vector Class w Operator Overloading
59 |
60 |
61 | 4. Animal, Cat, Dog Classes
62 |
63 |
64 | 4. Animal, Cat, Dog Classes
65 |
66 |
67 | 4. Animal, Cat, Dog Classes
68 |
69 |
70 | 4. Animal, Cat, Dog Classes
71 |
72 |
73 | 1. Copy Constructor with char Array
74 |
75 |
76 | 2. Vector Class w Funciton Overloading
77 |
78 |
79 | 3. Vector Class w Operator Overloading
80 |
81 |
82 |
--------------------------------------------------------------------------------
/03/Animal.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Animal.h"
3 |
4 | namespace samples
5 | {
6 | Animal::Animal()
7 | : Animal(0)
8 | {
9 | }
10 |
11 | Animal::Animal(int age)
12 | : mAge(age)
13 | {
14 | }
15 |
16 | int Animal::GetAge() const
17 | {
18 | return mAge;
19 | }
20 | }
--------------------------------------------------------------------------------
/03/Animal.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Animal
6 | {
7 | public:
8 | Animal();
9 | Animal(int age);
10 |
11 | int GetAge() const;
12 |
13 | private:
14 | int mAge;
15 | };
16 | }
--------------------------------------------------------------------------------
/03/Cat.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Cat.h"
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | Cat::Cat(int age)
9 | : Animal(age)
10 | {
11 | }
12 |
13 | string Cat::Purr() const
14 | {
15 | return string("Purr");
16 | }
17 | }
--------------------------------------------------------------------------------
/03/Cat.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include "Animal.h"
5 |
6 | namespace samples
7 | {
8 | class Cat : public Animal
9 | {
10 | public:
11 | Cat(int age);
12 |
13 | std::string Purr() const;
14 | };
15 | }
--------------------------------------------------------------------------------
/03/CopyConstructorExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "CopyConstructorExample.h"
3 | #include "String.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void CopyConstructorExample()
10 | {
11 | cout << "+------------------------------+" << endl;
12 | cout << "| Copy Constructor example |" << endl;
13 | cout << "+------------------------------+" << endl;
14 |
15 | String myName(" John Doe");
16 | String myNameCopy(myName);
17 |
18 | myName.Print();
19 | myNameCopy.Print();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/03/CopyConstructorExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void CopyConstructorExample();
6 | }
--------------------------------------------------------------------------------
/03/Dog.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Dog.h"
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | Dog::Dog(int age)
9 | : Animal(age)
10 | {
11 | }
12 |
13 | string Dog::Bark() const
14 | {
15 | return string("BowWow");
16 | }
17 | }
--------------------------------------------------------------------------------
/03/Dog.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include "Animal.h"
5 |
6 | namespace samples
7 | {
8 | class Dog : public Animal
9 | {
10 | public:
11 | Dog(int age);
12 |
13 | std::string Bark() const;
14 | };
15 | }
--------------------------------------------------------------------------------
/03/FunctionOverloadExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "OperatorOverloadExample.h"
3 | #include "Vector.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void FunctionOverloadExample()
10 | {
11 | cout << "+------------------------------+" << endl;
12 | cout << "| Function Overloading Example |" << endl;
13 | cout << "+------------------------------+" << endl;
14 |
15 | Vector vector1(3, 5);
16 | Vector vector2(7, 9);
17 | const int multiplier = 3;
18 |
19 | cout << "vector1:" << vector1.GetX() << ", " << vector1.GetY() << endl;
20 | cout << "vector2:" << vector2.GetX() << ", " << vector2.GetY() << endl;
21 |
22 | Vector result = vector1.Multiply(vector2);
23 | cout << "vector1.Multiply(vector2): " << result.GetX() << ", " << result.GetY() << endl;
24 |
25 | result = vector1.Multiply(multiplier);
26 | cout << "vector1.Multiply(multiplier): " << result.GetX() << ", " << result.GetY() << endl;
27 | cout << "vector1: " << vector1.GetX() << ", " << vector1.GetY() << endl;
28 |
29 | vector1.Scale(vector2);
30 | cout << "vector1.Scale(vector2);" << endl;
31 | cout << "vector1: " << vector1.GetX() << ", " << vector1.GetY() << endl;
32 |
33 | vector1.Scale(multiplier);
34 | cout << "vector1.Scale(multiplier)" << endl;
35 | cout << "vector1: " << vector1.GetX() << ", " << vector1.GetY() << endl;
36 | }
37 | }
--------------------------------------------------------------------------------
/03/FunctionOverloadExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void FunctionOverloadExample();
6 | }
--------------------------------------------------------------------------------
/03/InheritanceExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Animal.h"
3 | #include "Cat.h"
4 | #include "Dog.h"
5 | #include "InheritanceExample.h"
6 |
7 | using namespace std;
8 |
9 | namespace samples
10 | {
11 | void InheritanceExample()
12 | {
13 | cout << "+------------------------------+" << endl;
14 | cout << "| Inheritance Example |" << endl;
15 | cout << "+------------------------------+" << endl;
16 |
17 | Cat cat(3);
18 | Dog dog(5);
19 |
20 | cout << "Cat's Age: " << cat.GetAge() << endl;
21 | cout << "Cat's Purring: " << cat.Purr() << endl;
22 |
23 | cout << "Dog's Age: " << dog.GetAge() << endl;
24 | cout << "Dog's Barking: " << dog.Bark() << endl;
25 | }
26 | }
--------------------------------------------------------------------------------
/03/InheritanceExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void InheritanceExample();
6 | }
--------------------------------------------------------------------------------
/03/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "CopyConstructorExample.h"
2 | #include "FunctionOverloadExample.h"
3 | #include "InheritanceExample.h"
4 | #include "OperatorOverloadExample.h"
5 |
6 | using namespace samples;
7 |
8 | int main()
9 | {
10 | // 1. Copy Constructor with char Array
11 | CopyConstructorExample();
12 |
13 | // 2. Multiplying with a Vector or Scalar in Vector Class
14 | FunctionOverloadExample();
15 |
16 | // 3. Multiplying with a Vector or Scalar in Vector Class
17 | OperatorOverloadExample();
18 |
19 | // 4. Animal, Cat, Dog Classes
20 | InheritanceExample();
21 |
22 | return 0;
23 | }
--------------------------------------------------------------------------------
/03/OperatorOverloadExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "OperatorOverloadExample.h"
3 | #include "Vector2.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void OperatorOverloadExample()
10 | {
11 | cout << "+------------------------------+" << endl;
12 | cout << "| Operator Overloading Example |" << endl;
13 | cout << "+------------------------------+" << endl;
14 |
15 | Vector2 vector1(3, 5);
16 | Vector2 vector2(7, 9);
17 |
18 | const int multiplier = 3;
19 |
20 | cout << "vector1:" << vector1 << endl;
21 | cout << "vector2:" << vector2 << endl;
22 |
23 | Vector2 result = vector1 * vector2;
24 | cout << "vector1 * vector2: " << result << endl;
25 |
26 | result = vector1 * multiplier;
27 | cout << "vector1 * multiplier: " << result << endl;
28 | cout << "vector1: " << vector1 << endl;
29 |
30 | result = multiplier * vector1;
31 | cout << "multiplier * vector1: " << result << endl;
32 | cout << "vector1: " << vector1 << endl;
33 |
34 | vector1 *= vector2;
35 | cout << "vector1 *= vector2" << endl;
36 | cout << "vector1: " << vector1 << endl;
37 |
38 | vector1 *= multiplier;
39 | cout << "vector1 *= multiplier" << endl;
40 | cout << "vector1: " << vector1 << endl;
41 | }
42 | }
--------------------------------------------------------------------------------
/03/OperatorOverloadExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void OperatorOverloadExample();
6 | }
--------------------------------------------------------------------------------
/03/String.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "String.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | String::String(const char* str)
10 | : mSize(strlen(str) + 1)
11 | {
12 | mString = new char[mSize];
13 | memcpy(mString, str, mSize);
14 | }
15 |
16 | String::String(const String& str)
17 | : mSize(str.mSize)
18 | {
19 | mString = new char[mSize];
20 | memcpy(mString, str.mString, mSize);
21 | }
22 |
23 | String::~String()
24 | {
25 | delete[] mString;
26 | }
27 |
28 | void String::Print()
29 | {
30 | cout << "Member string : " << mString << endl;
31 | }
32 | }
--------------------------------------------------------------------------------
/03/String.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class String
6 | {
7 | public:
8 | String(const char* str);
9 | String(const String& str);
10 | ~String();
11 |
12 | void Print();
13 |
14 | private:
15 | char* mString;
16 | size_t mSize;
17 | };
18 | }
19 |
--------------------------------------------------------------------------------
/03/Vector.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Vector.h"
3 |
4 | namespace samples
5 | {
6 | Vector::Vector()
7 | : Vector(0, 0)
8 | {
9 | }
10 |
11 | Vector::Vector(int x, int y)
12 | : mX(x)
13 | , mY(y)
14 | {
15 | }
16 |
17 | int Vector::GetX() const
18 | {
19 | return mX;
20 | }
21 |
22 | void Vector::SetX(int x)
23 | {
24 | mX = x;
25 | }
26 |
27 | void Vector::SetY(int y)
28 | {
29 | mY = y;
30 | }
31 |
32 | int Vector::GetY() const
33 | {
34 | return mY;
35 | }
36 |
37 | bool Vector::IsEqual(const Vector& v) const
38 | {
39 | return (mX == v.mX && mY == v.mY);
40 | }
41 |
42 | Vector Vector::Multiply(const Vector& v) const
43 | {
44 | Vector result(mX * v.GetX(), mY * v.GetY());
45 |
46 | return result;
47 | }
48 |
49 | Vector Vector::Multiply(int multiplier) const
50 | {
51 | Vector result(mX * multiplier, mY * multiplier);
52 |
53 | return result;
54 | }
55 |
56 | void Vector::Scale(const Vector& v)
57 | {
58 | mX *= v.mX;
59 | mY *= v.mY;
60 | }
61 |
62 | void Vector::Scale(int multiplier)
63 | {
64 | mX *= multiplier;
65 | mY *= multiplier;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/03/Vector.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Vector
6 | {
7 | public:
8 | Vector();
9 | Vector(int x, int y);
10 |
11 | int GetX() const;
12 | void SetX(int x);
13 |
14 | int GetY() const;
15 | void SetY(int y);
16 |
17 | bool IsEqual(const Vector& v) const;
18 | Vector Multiply(const Vector& v) const;
19 | Vector Multiply(int multiplier) const;
20 |
21 | void Scale(const Vector& v);
22 | void Scale(int multiplier);
23 |
24 | private:
25 | int mX;
26 | int mY;
27 | };
28 | }
29 |
--------------------------------------------------------------------------------
/03/Vector2.cpp:
--------------------------------------------------------------------------------
1 | #include "Vector2.h"
2 |
3 | namespace samples
4 | {
5 | Vector2::Vector2()
6 | : Vector2(0, 0)
7 | {
8 | }
9 |
10 | Vector2::Vector2(int x, int y)
11 | : mX(x)
12 | , mY(y)
13 | {
14 | }
15 |
16 | int Vector2::GetX() const
17 | {
18 | return mX;
19 | }
20 |
21 | void Vector2::SetX(int x)
22 | {
23 | mX = x;
24 | }
25 |
26 | void Vector2::SetY(int y)
27 | {
28 | mY = y;
29 | }
30 |
31 | int Vector2::GetY() const
32 | {
33 | return mY;
34 | }
35 |
36 | bool Vector2::operator==(const Vector2& rhs) const
37 | {
38 | return (mX == rhs.mX && mY == rhs.mY);
39 | }
40 |
41 | Vector2 Vector2::operator*(const Vector2& rhs) const
42 | {
43 | Vector2 result(mX * rhs.mX, mY * rhs.mY);
44 |
45 | return result;
46 | }
47 |
48 | Vector2 Vector2::operator*(int multiplier) const
49 | {
50 | Vector2 result(mX * multiplier, mY * multiplier);
51 |
52 | return result;
53 | }
54 |
55 | Vector2 operator*(int multiplier, const Vector2& v)
56 | {
57 | Vector2 result(v.mX * multiplier, v.mY * multiplier);
58 |
59 | return result;
60 | }
61 |
62 | Vector2& Vector2::operator*=(const Vector2& rhs)
63 | {
64 | mX *= rhs.mX;
65 | mY *= rhs.mY;
66 |
67 | return *this;
68 | }
69 |
70 | Vector2& Vector2::operator*=(int multiplier)
71 | {
72 | mX *= multiplier;
73 | mY *= multiplier;
74 |
75 | return *this;
76 | }
77 |
78 | std::ostream& operator<<(std::ostream& out, const Vector2& v)
79 | {
80 | out << v.mX << ", " << v.mY << std::endl;
81 |
82 | return out;
83 | }
84 | }
--------------------------------------------------------------------------------
/03/Vector2.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | namespace samples
6 | {
7 | class Vector2
8 | {
9 | public:
10 | Vector2();
11 | Vector2(int x, int y);
12 |
13 | int GetX() const;
14 | void SetX(int x);
15 |
16 | int GetY() const;
17 | void SetY(int y);
18 |
19 | bool operator==(const Vector2& rhs) const;
20 |
21 | Vector2 operator*(const Vector2& rhs) const;
22 | Vector2 operator*(int multiplier) const;
23 | friend Vector2 operator*(int multiplier, const Vector2& v);
24 |
25 | Vector2& operator*=(const Vector2& rhs);
26 | Vector2& operator*=(int multiplier);
27 |
28 | friend std::ostream& operator<<(std::ostream& out, const Vector2& vector);
29 |
30 | private:
31 | int mX;
32 | int mY;
33 | };
34 | }
--------------------------------------------------------------------------------
/04/04.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {61c142c1-c02a-48ed-af0d-3011c3b7086e}
6 |
7 |
8 | {f979aa1a-1ef1-4111-98d2-46bd08c5dad9}
9 |
10 |
11 | {6f4d664b-a32a-4a26-a4a9-f6ec4f0b57b9}
12 |
13 |
14 |
15 |
16 | 01. Polymorphism
17 |
18 |
19 | Bonus. Multiple Inheritance
20 |
21 |
22 | 02. Interface
23 |
24 |
25 |
26 | 01. Polymorphism
27 |
28 |
29 | 01. Polymorphism
30 |
31 |
32 | 01. Polymorphism
33 |
34 |
35 | Bonus. Multiple Inheritance
36 |
37 |
38 | Bonus. Multiple Inheritance
39 |
40 |
41 | Bonus. Multiple Inheritance
42 |
43 |
44 | 02. Interface
45 |
46 |
47 | 02. Interface
48 |
49 |
50 |
51 |
52 | 01. Polymorphism
53 |
54 |
55 | Bonus. Multiple Inheritance
56 |
57 |
58 | 02. Interface
59 |
60 |
61 | 01. Polymorphism
62 |
63 |
64 | 01. Polymorphism
65 |
66 |
67 | 01. Polymorphism
68 |
69 |
70 | Bonus. Multiple Inheritance
71 |
72 |
73 | Bonus. Multiple Inheritance
74 |
75 |
76 | Bonus. Multiple Inheritance
77 |
78 |
79 | 02. Interface
80 |
81 |
82 | 02. Interface
83 |
84 |
85 | 02. Interface
86 |
87 |
88 | 02. Interface
89 |
90 |
91 |
--------------------------------------------------------------------------------
/04/Animal.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Animal.h"
3 |
4 | namespace samples
5 | {
6 | Animal::Animal(int age)
7 | : mAge(age)
8 | {
9 | }
10 |
11 | Animal::~Animal()
12 | {
13 | }
14 |
15 | void Animal::Move() const
16 | {
17 | std::cout << "An animal is moving" << std::endl;
18 | }
19 |
20 | void Animal::Speak() const
21 | {
22 | std::cout << "An animal is speaking" << std::endl;
23 | }
24 |
25 | int Animal::GetAge() const
26 | {
27 | return mAge;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/04/Animal.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Animal
6 | {
7 | public:
8 | Animal(int age);
9 | virtual ~Animal();
10 |
11 | virtual void Move() const;
12 | virtual void Speak() const;
13 |
14 | int GetAge() const;
15 |
16 | private:
17 | int mAge;
18 | };
19 | }
--------------------------------------------------------------------------------
/04/Bat.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Bat.h"
3 |
4 | namespace samples
5 | {
6 | void Bat::Fly() const
7 | {
8 | std::cout << "A bat is flying" << std::endl;
9 | }
10 |
11 | void Bat::Walk() const
12 | {
13 | std::cout << "A bat is walking" << std::endl;
14 | }
15 | }
--------------------------------------------------------------------------------
/04/Bat.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "IFlyable.h"
4 | #include "IWalkable.h"
5 |
6 | namespace samples
7 | {
8 | class Bat : public IFlyable, public IWalkable
9 | {
10 | public:
11 | void Fly() const;
12 | void Walk() const;
13 | };
14 | }
15 |
--------------------------------------------------------------------------------
/04/Cat.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Cat.h"
3 |
4 | namespace samples
5 | {
6 | Cat::Cat(int age)
7 | : Animal(age)
8 | {
9 | }
10 |
11 | void Cat::Move() const
12 | {
13 | std::cout << "A cat is moving" << std::endl;
14 | }
15 |
16 | void Cat::Speak() const
17 | {
18 | std::cout << "Meow " << std::endl;
19 | }
20 | }
--------------------------------------------------------------------------------
/04/Cat.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Animal.h"
4 |
5 | namespace samples
6 | {
7 | class Cat : public Animal
8 | {
9 | public:
10 | Cat(int age);
11 |
12 | virtual void Move() const;
13 | virtual void Speak() const;
14 | };
15 | }
16 |
--------------------------------------------------------------------------------
/04/Cat1.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Cat1.h"
3 |
4 | namespace samples
5 | {
6 | void Cat1::Walk() const
7 | {
8 | std::cout << "A cat is walking" << std::endl;
9 | }
10 | }
--------------------------------------------------------------------------------
/04/Cat1.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "IWalkable.h"
4 |
5 | namespace samples
6 | {
7 | class Cat1 : public IWalkable
8 | {
9 | public:
10 | void Walk() const;
11 | };
12 | }
--------------------------------------------------------------------------------
/04/Dog.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Dog.h"
3 |
4 | namespace samples
5 | {
6 | Dog::Dog(int age)
7 | : Animal(age)
8 | {
9 | }
10 |
11 | void Dog::Move() const
12 | {
13 | std::cout << "A dog is moving" << std::endl;
14 | }
15 |
16 | void Dog::Speak() const
17 | {
18 | std::cout << "Woof " << std::endl;
19 | }
20 | }
--------------------------------------------------------------------------------
/04/Dog.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Animal.h"
4 |
5 | namespace samples
6 | {
7 | class Dog : public Animal
8 | {
9 | public:
10 | Dog(int age);
11 |
12 | virtual void Move() const;
13 | virtual void Speak() const;
14 | };
15 | }
--------------------------------------------------------------------------------
/04/IFlyable.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class IFlyable
6 | {
7 | public:
8 | virtual void Fly() const = 0;
9 | };
10 | }
--------------------------------------------------------------------------------
/04/IWalkable.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class IWalkable
6 | {
7 | public:
8 | virtual void Walk() const = 0;
9 | };
10 | }
11 |
--------------------------------------------------------------------------------
/04/InterfaceExample.cpp:
--------------------------------------------------------------------------------
1 | #include "Bat.h"
2 | #include "Cat1.h"
3 | #include "InterfaceExample.h"
4 |
5 | namespace samples
6 | {
7 | void InterfaceExample()
8 | {
9 | Bat bat;
10 | Cat1 cat;
11 |
12 | bat.Fly();
13 | bat.Walk();
14 |
15 | // cat.Fly();
16 | cat.Walk();
17 | }
18 | }
--------------------------------------------------------------------------------
/04/InterfaceExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void InterfaceExample();
6 | }
7 |
--------------------------------------------------------------------------------
/04/Liger.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Liger.h"
3 |
4 | namespace samples
5 | {
6 | Liger::Liger(int age)
7 | : Lion(age)
8 | , Tiger(2 * age)
9 | {
10 | }
11 | }
--------------------------------------------------------------------------------
/04/Liger.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Lion.h"
4 | #include "Tiger.h"
5 |
6 | namespace samples
7 | {
8 | class Liger : public Lion, public Tiger
9 | {
10 | public:
11 | Liger(int age);
12 | };
13 | }
14 |
--------------------------------------------------------------------------------
/04/Lion.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Lion.h"
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | Lion::Lion(int age)
9 | : Animal(age)
10 | {
11 | }
12 |
13 | void Lion::ShowOffBeautifulHair() const
14 | {
15 | cout << "I'm too sexy" << endl;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/04/Lion.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Animal.h"
4 |
5 | namespace samples
6 | {
7 | class Lion : public Animal
8 | {
9 | public:
10 | Lion(int age);
11 |
12 | void ShowOffBeautifulHair() const;
13 | };
14 | }
--------------------------------------------------------------------------------
/04/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "PolymorphismExample.h"
2 | #include "MultiInheritanceExample.h"
3 | #include "InterfaceExample.h"
4 |
5 | using namespace samples;
6 |
7 | int main()
8 | {
9 | // 1. Polymorphism
10 | PolymorphismExample();
11 |
12 | // 2. Multiple Inheritance... YUCK
13 | MultiInheritanceExample();
14 |
15 | // 3. Interface
16 | InterfaceExample();
17 |
18 | return 0;
19 | }
--------------------------------------------------------------------------------
/04/MultiInheritanceExample.cpp:
--------------------------------------------------------------------------------
1 | #include "Liger.h"
2 | #include "MultiInheritanceExample.h"
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | namespace samples
9 | {
10 | void MultiInheritanceExample()
11 | {
12 | Liger liger(5);
13 |
14 | liger.ShowOffBeautifulHair();
15 | liger.PretendToBeZebra();
16 |
17 | cout << "tiger age: " << liger.Tiger::GetAge() << endl;
18 | cout << "lion age: " << liger.Lion::GetAge() << endl;
19 | }
20 | }
--------------------------------------------------------------------------------
/04/MultiInheritanceExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void MultiInheritanceExample();
6 | }
7 |
--------------------------------------------------------------------------------
/04/PolymorphismExample.cpp:
--------------------------------------------------------------------------------
1 | #include "Animal.h"
2 | #include "Cat.h"
3 | #include "Dog.h"
4 | #include "PolymorphismExample.h"
5 |
6 | namespace samples
7 | {
8 | void PolymorphismExample()
9 | {
10 | Animal* cat = new Cat(3);
11 | Animal* dog = new Dog(5);
12 |
13 | cat->Move();
14 | cat->Speak();
15 |
16 | dog->Move();
17 | dog->Speak();
18 |
19 | delete cat;
20 | delete dog;
21 | }
22 | }
--------------------------------------------------------------------------------
/04/PolymorphismExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void PolymorphismExample();
6 | }
7 |
--------------------------------------------------------------------------------
/04/Tiger.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Tiger.h"
3 |
4 | using namespace std;
5 |
6 | namespace samples
7 | {
8 | Tiger::Tiger(int age)
9 | : Animal(age)
10 | {
11 | }
12 |
13 | void Tiger::PretendToBeZebra() const
14 | {
15 | cout << "I Has Zebra Stripes" << endl;
16 | }
17 | }
--------------------------------------------------------------------------------
/04/Tiger.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Animal.h"
4 |
5 | namespace samples
6 | {
7 | class Tiger : public Animal
8 | {
9 | public:
10 | Tiger::Tiger(int age);
11 |
12 | void PretendToBeZebra() const;
13 | };
14 | }
--------------------------------------------------------------------------------
/05/05.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {657df201-6e8f-4a3b-b82f-2b53dad32383}
6 |
7 |
8 | {b19dd871-97a7-4b17-baac-878f4e09c735}
9 |
10 |
11 | {c85a384d-4a48-48f5-83fc-cb66984cdc66}
12 |
13 |
14 | {d27e9bb7-1a02-4dbf-9e44-9e81008fb958}
15 |
16 |
17 | {bcab35f3-1902-4be1-9192-d05b1c593867}
18 |
19 |
20 | {ad6b2ac9-edcf-48fd-b7da-97ef123a98ec}
21 |
22 |
23 |
24 |
25 |
26 | 1. Static_Cast with values
27 |
28 |
29 | 2. Static_Cast with pointers
30 |
31 |
32 | 3. Saving Address of a Tiger object
33 |
34 |
35 | 5. Static member variable
36 |
37 |
38 | 6. Static member function
39 |
40 |
41 | 2. Static_Cast with pointers
42 |
43 |
44 | 2. Static_Cast with pointers
45 |
46 |
47 | 2. Static_Cast with pointers
48 |
49 |
50 | 3. Saving Address of a Tiger object
51 |
52 |
53 | 4. Inline function
54 |
55 |
56 | 4. Inline function
57 |
58 |
59 | 5. Static member variable
60 |
61 |
62 | 6. Static member function
63 |
64 |
65 |
66 |
67 | 1. Static_Cast with values
68 |
69 |
70 | 2. Static_Cast with pointers
71 |
72 |
73 | 3. Saving Address of a Tiger object
74 |
75 |
76 | 5. Static member variable
77 |
78 |
79 | 6. Static member function
80 |
81 |
82 | 2. Static_Cast with pointers
83 |
84 |
85 | 2. Static_Cast with pointers
86 |
87 |
88 | 2. Static_Cast with pointers
89 |
90 |
91 | 3. Saving Address of a Tiger object
92 |
93 |
94 | 4. Inline function
95 |
96 |
97 | 4. Inline function
98 |
99 |
100 | 5. Static member variable
101 |
102 |
103 | 6. Static member function
104 |
105 |
106 |
--------------------------------------------------------------------------------
/05/Animal.cpp:
--------------------------------------------------------------------------------
1 | #include "Animal.h"
2 |
3 | namespace samples
4 | {
5 | Animal::Animal(int age)
6 | : mAge(age)
7 | {
8 | }
9 | }
--------------------------------------------------------------------------------
/05/Animal.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Animal
6 | {
7 | public:
8 | Animal(int age);
9 | virtual ~Animal() {};
10 |
11 | private:
12 | int mAge;
13 | };
14 | }
--------------------------------------------------------------------------------
/05/Cat.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "Cat.h"
4 |
5 | namespace samples
6 | {
7 | Cat::Cat(int age, const char* name)
8 | : Animal(age)
9 | {
10 | mName = new char[strlen(name) + 1];
11 | memcpy(mName, name, strlen(name) + 1);
12 | }
13 |
14 | Cat::~Cat()
15 | {
16 | delete[] mName;
17 | }
18 |
19 | const char* Cat::GetName() const
20 | {
21 | return mName;
22 | }
23 | }
--------------------------------------------------------------------------------
/05/Cat.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Animal.h"
4 |
5 | namespace samples
6 | {
7 | class Cat : public Animal
8 | {
9 | public:
10 | Cat(int age, const char* name);
11 | virtual ~Cat();
12 |
13 | const char* GetName() const;
14 |
15 | private:
16 | char* mName;
17 | };
18 | }
--------------------------------------------------------------------------------
/05/Cat2.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Cat2.h"
3 |
4 | namespace samples
5 | {
6 | const char* Cat2::mAnimalType = "Cat";
7 |
8 | Cat2::Cat2(int age, const char* name)
9 | : mAge(age)
10 | {
11 | mName = new char[strlen(name) + 1];
12 | memcpy(mName, name, strlen(name) + 1);
13 | }
14 |
15 | Cat2::~Cat2()
16 | {
17 | delete[] mName;
18 | }
19 |
20 | // static function
21 | const char* Cat2::GetType()
22 | {
23 | return mAnimalType;
24 | }
25 | }
--------------------------------------------------------------------------------
/05/Cat2.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Cat2
6 | {
7 | public:
8 | Cat2(int age, const char* name);
9 | virtual ~Cat2();
10 |
11 | static const char* GetType();
12 |
13 | private:
14 | static const char* mAnimalType;
15 |
16 | int mAge;
17 | char* mName;
18 | };
19 | }
--------------------------------------------------------------------------------
/05/Dog.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "Dog.h"
4 |
5 | namespace samples
6 | {
7 | Dog::Dog(int age, const char* address)
8 | : Animal(age)
9 | {
10 | mHomeAddress = new char[strlen(address) + 1];
11 | memcpy(mHomeAddress, address, strlen(address) + 1);
12 | }
13 |
14 | Dog::~Dog()
15 | {
16 | delete[] mHomeAddress;
17 | }
18 |
19 | const char* Dog::GetAddress() const
20 | {
21 | return mHomeAddress;
22 | }
23 | }
--------------------------------------------------------------------------------
/05/Dog.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "Animal.h"
4 |
5 | namespace samples
6 | {
7 | class Dog : public Animal
8 | {
9 | public:
10 | Dog(int age, const char* address);
11 | virtual ~Dog();
12 |
13 | const char* GetAddress() const;
14 |
15 | private:
16 | char* mHomeAddress;
17 | };
18 | }
--------------------------------------------------------------------------------
/05/Main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "ObjectAddressSavingExample.h"
4 | #include "ObjectPointerCastingExample.h"
5 | #include "StaticMemberFunctionExample.h"
6 | #include "StaticMemberVariableExample.h"
7 | #include "ValueCastingExample.h"
8 |
9 | using namespace samples;
10 |
11 | int main(){
12 | // 1. Static_Cast with values
13 | ValueCastingExample();
14 |
15 | std::cout << std::dec;
16 |
17 | // 2. Static_Cast with pointers
18 | ObjectPointerCastingExample();
19 |
20 | // 3. Saving Address of an Cat class
21 | ObjectAddressSavingExample();
22 |
23 | // 5. Static member variable
24 | StaticMemberVariableExample();
25 |
26 | // 6. Static member function
27 | StaticMemberFunctionExample();
28 |
29 | return 0;
30 | }
--------------------------------------------------------------------------------
/05/Math.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Math.h"
3 |
4 | namespace samples
5 | {
6 | int Math::Ceil(float value)
7 | {
8 | int intValue = static_cast(value);
9 | if (value == static_cast(intValue))
10 | {
11 | return intValue;
12 | }
13 |
14 | return intValue + 1;
15 | }
16 |
17 | int Math::Floor(float value)
18 | {
19 | return static_cast(value);
20 | }
21 |
22 | float Math::Power(float value, float power)
23 | {
24 | return pow(value, power);
25 | }
26 |
27 | int Math::Round(float value)
28 | {
29 | return static_cast(value + 0.5f);
30 | }
31 |
32 | float Math::Square(float value)
33 | {
34 | return value * value;
35 | }
36 | }
--------------------------------------------------------------------------------
/05/Math.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Math
6 | {
7 | public:
8 | static int Ceil(float value);
9 | static int Floor(float value);
10 | static float Power(float value, float power);
11 | static int Round(float value);
12 | static float Square(float value);
13 |
14 | private:
15 | Math() {};
16 | };
17 | }
--------------------------------------------------------------------------------
/05/ObjectAddressSavingExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "ObjectAddressSavingExample.h"
3 | #include "Tiger.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void ObjectAddressSavingExample()
10 | {
11 | Tiger* tiger = new Tiger(5);
12 | unsigned int intAddress = reinterpret_cast(tiger);
13 |
14 | cout << "saving address as int: " << intAddress << endl;
15 | cout << "read int address to pointer" << endl;
16 |
17 | tiger = reinterpret_cast(intAddress);
18 | tiger->PretendIAmAZebra();
19 |
20 | delete tiger;
21 | }
22 | }
--------------------------------------------------------------------------------
/05/ObjectAddressSavingExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void ObjectAddressSavingExample();
6 | }
--------------------------------------------------------------------------------
/05/ObjectPointerCastingExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "Animal.h"
4 | #include "Cat.h"
5 | #include "Dog.h"
6 | #include "ObjectPointerCastingExample.h"
7 |
8 | using namespace std;
9 |
10 | namespace samples
11 | {
12 | void ObjectPointerCastingExample()
13 | {
14 | Animal* pet1 = new Cat(2, "Lulu");
15 | Animal* pet2 = new Dog(2, "Burnaby");
16 |
17 | Cat* cat = static_cast(pet1);
18 | Dog* dog1 = static_cast(pet2);
19 | Dog* dog2 = static_cast(pet1);
20 |
21 | cout << "cat's name : " << cat->GetName() << endl;
22 | cout << "dog1's address :" << dog1->GetAddress() << endl;
23 |
24 | // prints cat's name instead
25 | cout << "dog2's address : " << dog2->GetAddress() << endl;
26 |
27 | delete pet1;
28 | delete pet2;
29 | }
30 | }
--------------------------------------------------------------------------------
/05/ObjectPointerCastingExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void ObjectPointerCastingExample();
6 | }
--------------------------------------------------------------------------------
/05/StaticMemberFunctionExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Math.h"
3 | #include "StaticMemberFunctionExample.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void StaticMemberFunctionExample()
10 | {
11 | float number = 5.5f;
12 |
13 | cout << Math::Ceil(number) << endl;
14 | cout << Math::Floor(number) << endl;
15 | cout << Math::Power(number, 3) << endl;
16 | cout << Math::Round(number) << endl;
17 | cout << Math::Square(number) << endl;
18 | }
19 | }
--------------------------------------------------------------------------------
/05/StaticMemberFunctionExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void StaticMemberFunctionExample();
6 | }
--------------------------------------------------------------------------------
/05/StaticMemberVariableExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Cat2.h"
3 | #include "StaticMemberVariableExample.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void StaticMemberVariableExample()
10 | {
11 | Cat2* myCat1 = new Cat2(2, "Lulu");
12 | Cat2* myCat2 = new Cat2(5, "Poppy");
13 | Cat2* myCat3 = new Cat2(3, "Teemo");
14 | Cat2* myCat4 = new Cat2(7, "Amumu");
15 |
16 | cout << "myCat1's type : " << myCat1->GetType() <GetType() << endl;
18 | cout << "myCat3's type : " << myCat3->GetType() << endl;
19 | cout << "myCat4's type : " << myCat4->GetType() << endl;
20 | cout << "global cat type :" << Cat2::GetType() << endl;
21 |
22 | delete myCat1;
23 | delete myCat2;
24 | delete myCat3;
25 | delete myCat4;
26 | }
27 | }
--------------------------------------------------------------------------------
/05/StaticMemberVariableExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void StaticMemberVariableExample();
6 | }
--------------------------------------------------------------------------------
/05/Tiger.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Tiger.h"
3 |
4 | namespace samples
5 | {
6 | Tiger::Tiger(int age)
7 | : mAge(age)
8 | {
9 | }
10 |
11 | void Tiger::PretendIAmAZebra() const
12 | {
13 | std::cout << "I Has Zebra Stripes" << std::endl;
14 | }
15 | }
--------------------------------------------------------------------------------
/05/Tiger.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Tiger
6 | {
7 | public:
8 | Tiger(int age);
9 |
10 | void PretendIAmAZebra() const;
11 |
12 | private:
13 | int mAge;
14 | };
15 | }
--------------------------------------------------------------------------------
/05/ValueCastingExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "ValueCastingExample.h"
5 |
6 | using namespace std;
7 |
8 | namespace samples
9 | {
10 | void ValueCastingExample()
11 | {
12 | const int intNumber = 10;
13 |
14 | const float floatNumber = static_cast(intNumber);
15 | const short shortNumber = static_cast(intNumber);
16 |
17 | cout << showbase << hex << uppercase;
18 | cout << "int: " << intNumber
19 | << ", short: " << shortNumber
20 | << endl;
21 | cout << "int: " << intNumber
22 | << ", float: " << *(int*)(&floatNumber)
23 | << endl;
24 | }
25 | }
--------------------------------------------------------------------------------
/05/ValueCastingExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void ValueCastingExample();
6 | }
--------------------------------------------------------------------------------
/05/Vector.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Vector.h"
3 |
4 | namespace samples
5 | {
6 | Vector::Vector()
7 | : Vector(0, 0)
8 | {
9 | }
10 |
11 | Vector::Vector(int x, int y)
12 | : mX(x)
13 | , mY(y)
14 | {
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/05/Vector.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | class Vector
6 | {
7 | public:
8 | Vector();
9 | Vector(int x, int y);
10 |
11 | inline int GetX() const;
12 | inline void SetX(int x);
13 |
14 | inline int GetY() const;
15 | inline void SetY(int y);
16 |
17 | inline bool IsEqual(const Vector& v) const;
18 | inline Vector Multiply(const Vector& v) const;
19 | inline Vector Multiply(int multiplier) const;
20 |
21 | inline void Scale(const Vector& v);
22 | inline void Scale(int multiplier);
23 |
24 | private:
25 | int mX;
26 | int mY;
27 | };
28 |
29 | int Vector::GetX() const
30 | {
31 | return mX;
32 | }
33 |
34 | void Vector::SetX(int x)
35 | {
36 | mX = x;
37 | }
38 |
39 | void Vector::SetY(int y)
40 | {
41 | mY = y;
42 | }
43 |
44 | int Vector::GetY() const
45 | {
46 | return mY;
47 | }
48 |
49 | bool Vector::IsEqual(const Vector& v) const
50 | {
51 | return (mX == v.mX && mY == v.mY);
52 | }
53 |
54 | Vector Vector::Multiply(const Vector& v) const
55 | {
56 | Vector result(mX * v.GetX(), mY * v.GetY());
57 |
58 | return result;
59 | }
60 |
61 | Vector Vector::Multiply(int multiplier) const
62 | {
63 | Vector result(mX * multiplier, mY * multiplier);
64 |
65 | return result;
66 | }
67 |
68 | void Vector::Scale(const Vector& v)
69 | {
70 | mX *= v.mX;
71 | mY *= v.mY;
72 | }
73 |
74 | void Vector::Scale(int multiplier)
75 | {
76 | mX *= multiplier;
77 | mY *= multiplier;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/05/Vector2.cpp:
--------------------------------------------------------------------------------
1 | #include "Vector2.h"
2 |
3 | namespace samples
4 | {
5 | Vector2::Vector2()
6 | : Vector2(0, 0)
7 | {
8 | }
9 |
10 | Vector2::Vector2(int x, int y)
11 | : mX(x)
12 | , mY(y)
13 | {
14 | }
15 | }
--------------------------------------------------------------------------------
/05/Vector2.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | namespace samples
6 | {
7 | class Vector2
8 | {
9 | public:
10 | Vector2();
11 | Vector2(int x, int y);
12 |
13 | inline int GetX() const;
14 | inline void SetX(int x);
15 |
16 | inline int GetY() const;
17 | inline void SetY(int y);
18 |
19 | inline bool operator==(const Vector2& rhs) const;
20 |
21 | inline Vector2 operator*(const Vector2& rhs) const;
22 | inline Vector2 operator*(int multiplier) const;
23 | friend inline Vector2 operator*(int multiplier, const Vector2& v);
24 |
25 | inline Vector2& operator*=(const Vector2& rhs);
26 | inline Vector2& operator*=(int multiplier);
27 |
28 | friend inline std::ostream& operator<<(std::ostream& out, const Vector2& vector);
29 |
30 | private:
31 | int mX;
32 | int mY;
33 | };
34 |
35 | int Vector2::GetX() const
36 | {
37 | return mX;
38 | }
39 |
40 | void Vector2::SetX(int x)
41 | {
42 | mX = x;
43 | }
44 |
45 | void Vector2::SetY(int y)
46 | {
47 | mY = y;
48 | }
49 |
50 | int Vector2::GetY() const
51 | {
52 | return mY;
53 | }
54 |
55 | bool Vector2::operator==(const Vector2& rhs) const
56 | {
57 | return (mX == rhs.mX && mY == rhs.mY);
58 | }
59 |
60 | Vector2 Vector2::operator*(const Vector2& rhs) const
61 | {
62 | Vector2 result(mX * rhs.mX, mY * rhs.mY);
63 |
64 | return result;
65 | }
66 |
67 | Vector2 Vector2::operator*(int multiplier) const
68 | {
69 | Vector2 result(mX * multiplier, mY * multiplier);
70 |
71 | return result;
72 | }
73 |
74 | Vector2 operator*(int multiplier, const Vector2& v)
75 | {
76 | Vector2 result(v.mX * multiplier, v.mY * multiplier);
77 |
78 | return result;
79 | }
80 |
81 | Vector2& Vector2::operator*=(const Vector2& rhs)
82 | {
83 | mX *= rhs.mX;
84 | mY *= rhs.mY;
85 |
86 | return *this;
87 | }
88 |
89 | Vector2& Vector2::operator*=(int multiplier)
90 | {
91 | mX *= multiplier;
92 | mY *= multiplier;
93 |
94 | return *this;
95 | }
96 |
97 | std::ostream& operator<<(std::ostream& out, const Vector2& v)
98 | {
99 | out << v.mX << ", " << v.mY << std::endl;
100 |
101 | return out;
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/06/06.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | 1. Adding elements
7 |
8 |
9 | Bonus. Swapping elements
10 |
11 |
12 | 2. Calculating sum
13 |
14 |
15 | 3. Object vector
16 |
17 |
18 | 3. Object vector
19 |
20 |
21 | 4. Pointer vector
22 |
23 |
24 |
25 |
26 | {5d0c8812-e38a-4e38-91f1-0b543ea782b8}
27 |
28 |
29 | {a2e73944-2407-4cf1-a92d-1b516acdebcb}
30 |
31 |
32 | {a365519b-6bf6-4278-9ad8-a373d0fb4c4b}
33 |
34 |
35 | {6b86ce6e-7638-428d-95d4-6a4f9011f5a4}
36 |
37 |
38 | {7f882b1f-f279-43c4-bcc1-4fe4718dc9c0}
39 |
40 |
41 |
42 |
43 | 1. Adding elements
44 |
45 |
46 | Bonus. Swapping elements
47 |
48 |
49 | 2. Calculating sum
50 |
51 |
52 | 3. Object vector
53 |
54 |
55 | 3. Object vector
56 |
57 |
58 | 4. Pointer vector
59 |
60 |
61 |
--------------------------------------------------------------------------------
/06/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "ObjectVectorExample.h"
2 | #include "PointerVectorExample.h"
3 | #include "VectorAddingElementsExample.h"
4 | #include "VectorCalculatingSumExample.h"
5 | #include "VectorSwappingElementsExample.h"
6 |
7 | using namespace samples;
8 |
9 | int main()
10 | {
11 | // 1. Adding elements
12 | VectorAddingElementsExample();
13 |
14 | // 2. Swapping elements
15 | VectorSwappingElementsExample();
16 |
17 | // 3. Calculating sum
18 | VectorCalculatingSumExample();
19 |
20 | // 4. Object vector
21 | ObjectVectorExample();
22 |
23 | //5. Pointer vector
24 | PointerVectorExample();
25 |
26 | return 0;
27 | }
--------------------------------------------------------------------------------
/06/ObjectVectorExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "ObjectVectorExample.h"
4 | #include "Score.h"
5 |
6 | using namespace std;
7 |
8 | namespace samples
9 | {
10 | void ObjectVectorExample()
11 | {
12 | vector scores;
13 | scores.reserve(5);
14 |
15 | scores.push_back(Score(30, "C++"));
16 | scores.push_back(Score(59, "Algorithm"));
17 | scores.push_back(Score(87, "Java"));
18 | scores.push_back(Score(74, "Data Comm"));
19 | scores.push_back(Score(41, "Android"));
20 |
21 | cout << "Before erasing the element" << endl;
22 | PrintVector(scores);
23 |
24 | cout << "After erasing the element" << endl;
25 |
26 | vector::iterator iter = scores.begin();
27 | while (iter != scores.end())
28 | {
29 | if (iter->GetClassName() == "Java")
30 | {
31 | iter = scores.erase(iter);
32 | }
33 | else
34 | {
35 | iter++;
36 | }
37 | }
38 | PrintVector(scores);
39 |
40 | cout << "After changing the score of class 1" << endl;
41 | for (vector::iterator iter = scores.begin(); iter != scores.end(); ++iter)
42 | {
43 | Score score = *iter;
44 |
45 | if (score.GetScore() == 30)
46 | {
47 | score.SetScore(100);
48 | }
49 | }
50 |
51 | PrintVector(scores);
52 | }
53 |
54 | void PrintVector(const vector& scores)
55 | {
56 | for (vector::const_iterator iter = scores.begin(); iter != scores.end(); ++iter)
57 | {
58 | iter->PrintVariables();
59 | }
60 | cout << endl;
61 | }
62 | }
--------------------------------------------------------------------------------
/06/ObjectVectorExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | #include "Score.h"
6 |
7 | namespace samples
8 | {
9 | void ObjectVectorExample();
10 |
11 | void PrintVector(const std::vector& scores);
12 | }
--------------------------------------------------------------------------------
/06/PointerVectorExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "PointerVectorExample.h"
4 | #include "Score.h"
5 |
6 | using namespace std;
7 |
8 | namespace samples
9 | {
10 | void PointerVectorExample()
11 | {
12 | vector scores;
13 | scores.reserve(5);
14 |
15 | Score* cppScore = new Score(30, "C++");
16 | Score* algoScore = new Score(59, "Algorithm");
17 | Score* javaScore = new Score(87, "Java");
18 | Score* dataCommScore = new Score(74, "Data Comm");
19 | Score* androidScore = new Score(41, "Android");
20 |
21 | scores.push_back(cppScore);
22 | scores.push_back(algoScore);
23 | scores.push_back(javaScore);
24 | scores.push_back(dataCommScore);
25 | scores.push_back(androidScore);
26 |
27 | PrintVector(scores);
28 |
29 | vector::iterator iter = scores.begin();
30 | while (iter != scores.end())
31 | {
32 | Score* score = *iter;
33 | if (score->GetClassName() == "Java")
34 | {
35 | iter = scores.erase(iter);
36 | }
37 | else
38 | {
39 | iter++;
40 | }
41 | }
42 |
43 | PrintVector(scores);
44 |
45 | for (vector::iterator iter = scores.begin(); iter != scores.end(); ++iter)
46 | {
47 | Score* score = *iter;
48 |
49 | if (score->GetScore() == 30)
50 | {
51 | score->SetScore(100);
52 | }
53 | }
54 |
55 | cout << "After chaning the score of class 1" << endl;
56 | PrintVector(scores);
57 |
58 | delete cppScore;
59 | delete algoScore;
60 | delete javaScore;
61 | delete dataCommScore;
62 | delete androidScore;
63 | }
64 |
65 | void PrintVector(const vector& scores)
66 | {
67 | for (vector::const_iterator iter = scores.begin(); iter != scores.end(); iter++)
68 | {
69 | Score* score = *iter;
70 | score->PrintVariables();
71 | }
72 | cout << endl;
73 | }
74 | }
--------------------------------------------------------------------------------
/06/PointerVectorExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | #include "Score.h"
6 |
7 | namespace samples
8 | {
9 | void PointerVectorExample();
10 |
11 | void PrintVector(const std::vector& scores);
12 | }
--------------------------------------------------------------------------------
/06/Score.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Score.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | Score::Score(int score, const string& className)
10 | : mScore(score)
11 | , mClassName(className)
12 | {
13 | }
14 |
15 | int Score::GetScore() const
16 | {
17 | return mScore;
18 | }
19 |
20 | void Score::SetScore(const int score)
21 | {
22 | mScore = score;
23 | }
24 |
25 | const string& Score::GetClassName() const
26 | {
27 | return mClassName;
28 | }
29 |
30 | void Score::PrintVariables() const
31 | {
32 | cout << mClassName << ": " << mScore << endl;
33 | }
34 | }
--------------------------------------------------------------------------------
/06/Score.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | class Score
10 | {
11 | public:
12 | Score(int score, const string& className);
13 |
14 | int GetScore() const;
15 | void SetScore(const int score);
16 |
17 | const string& GetClassName() const;
18 |
19 | void PrintVariables() const;
20 |
21 | private:
22 | int mScore;
23 | string mClassName;
24 | };
25 | }
--------------------------------------------------------------------------------
/06/VectorAddingElementsExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "VectorAddingElementsExample.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | void VectorAddingElementsExample()
10 | {
11 | vector scores;
12 | scores.reserve(5);
13 |
14 | scores.push_back(30);
15 | scores.push_back(50);
16 | scores.push_back(80);
17 | scores.push_back(65);
18 | scores.push_back(73);
19 |
20 | PrintScores(scores);
21 |
22 | scores.pop_back();
23 | scores.pop_back();
24 |
25 | PrintScores(scores);
26 |
27 | scores.resize(10);
28 |
29 | PrintScores(scores);
30 | }
31 |
32 | void PrintScores(const vector& scores)
33 | {
34 | cout << "Current elements : ";
35 | for (vector::const_iterator iter = scores.begin(); iter != scores.end(); ++iter)
36 | {
37 | cout << *iter << " ";
38 | }
39 | cout << endl;
40 |
41 | cout << "Current capacity : " << scores.capacity() << endl;
42 | cout << "Current size : " << scores.size() << endl << endl;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/06/VectorAddingElementsExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | namespace samples
6 | {
7 | void PrintScores(const std::vector& scores);
8 |
9 | void VectorAddingElementsExample();
10 | }
--------------------------------------------------------------------------------
/06/VectorCalculatingSumExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "VectorCalculatingSumExample.h"
4 |
5 | using namespace std;
6 |
7 | namespace samples
8 | {
9 | int CalculateSum(const vector& scores)
10 | {
11 | int sum = 0;
12 |
13 | for (size_t i = 0; i < scores.size(); i++)
14 | {
15 | sum += scores[i];
16 | }
17 |
18 | return sum;
19 | }
20 |
21 | void VectorCalculatingSumExample()
22 | {
23 | vector myScores;
24 | myScores.reserve(5);
25 |
26 | vector yourScores;
27 | yourScores.reserve(5);
28 |
29 | myScores.push_back(13);
30 | myScores.push_back(21);
31 | myScores.push_back(35);
32 | myScores.push_back(76);
33 | myScores.push_back(94);
34 |
35 | yourScores.push_back(61);
36 | yourScores.push_back(27);
37 | yourScores.push_back(83);
38 | yourScores.push_back(91);
39 | yourScores.push_back(100);
40 |
41 | yourScores.clear();
42 |
43 | cout << "myScores" << endl;
44 | cout << "Sum : " << CalculateSum(myScores) << endl << endl;
45 |
46 | cout << "yourScores" << endl;
47 | cout << "Sum : " << CalculateSum(yourScores) << endl << endl;
48 | }
49 | }
--------------------------------------------------------------------------------
/06/VectorCalculatingSumExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | namespace samples
6 | {
7 | int CalculateSum(const std::vector& scores);
8 |
9 | void VectorCalculatingSumExample();
10 | }
--------------------------------------------------------------------------------
/06/VectorSwappingElementsExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "VectorSwappingElementsExample.h"
4 | #include "VectorAddingElementsExample.h"
5 |
6 | using namespace std;
7 |
8 | namespace samples
9 | {
10 | void VectorSwappingElementsExample()
11 | {
12 | vector scores;
13 | scores.reserve(2);
14 |
15 | scores.push_back(85);
16 | scores.push_back(73);
17 |
18 | cout << "scores" << endl;
19 | PrintScores(scores);
20 |
21 | vector anotherScores;
22 | anotherScores.assign(7, 100);
23 |
24 | scores.swap(anotherScores);
25 |
26 | cout << "scores" << endl;
27 | PrintScores(scores);
28 |
29 | cout << "anotherScores" << endl;
30 | PrintScores(anotherScores);
31 | }
32 | }
--------------------------------------------------------------------------------
/06/VectorSwappingElementsExample.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | namespace samples
4 | {
5 | void VectorSwappingElementsExample();
6 | }
--------------------------------------------------------------------------------
/07/07.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | 1. Inserting
7 |
8 |
9 | 3. User-defined types as key 1
10 |
11 |
12 | 3. User-defined types as key 1
13 |
14 |
15 | 4. User-defined types as key 2
16 |
17 |
18 | 4. User-defined types as key 2
19 |
20 |
21 | 2. Inserting with operator
22 |
23 |
24 |
25 |
26 | {4c1620fe-ec05-419b-930a-24a28dcc5bcc}
27 |
28 |
29 | {a39cd267-2c62-4b31-a900-45117176a23a}
30 |
31 |
32 | {c84ff7ca-b0b3-4a76-ab89-7b27555431df}
33 |
34 |
35 | {b4f1c6a5-47fa-4111-986d-c07ec04f178a}
36 |
37 |
38 |
39 |
40 | 1. Inserting
41 |
42 |
43 | 2. Inserting with operator
44 |
45 |
46 | 3. User-defined types as key 1
47 |
48 |
49 | 3. User-defined types as key 1
50 |
51 |
52 | 4. User-defined types as key 2
53 |
54 |
55 | 4. User-defined types as key 2
56 |
57 |
58 | 4. User-defined types as key 2
59 |
60 |
61 |
--------------------------------------------------------------------------------
/07/Main.cpp:
--------------------------------------------------------------------------------
1 | #include "MapInsertingExample.h"
2 | #include "MapInsertingWithOperatorExample.h"
3 | #include "MapUserDefinedTypesExample1.h"
4 | #include "MapUserDefinedTypesExample2.h"
5 |
6 | using namespace samples;
7 |
8 | int main()
9 | {
10 | // 1. Inserting
11 | MapInsertingExample();
12 |
13 | // 2. Inserting with operator
14 | MapInsertingWithOperatorExample();
15 |
16 | // 3. User-defined types as key 1
17 | MapUserDefinedTypesExample1();
18 |
19 | // 4. User-defined types as key 2
20 | MapUserDefinedTypesExample2();
21 |
22 | return 0;
23 | }
--------------------------------------------------------------------------------
/07/MapInsertingExample.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include