-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewStarterC++.html
308 lines (264 loc) · 18.8 KB
/
NewStarterC++.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>New Starter C++</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/bootstrap-sphinx.css?v=fadd4351" />
<link rel="stylesheet" type="text/css" href="_static/custom.css?v=77160d70" />
<script src="_static/documentation_options.js?v=a8da1a53"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="New Starter Python" href="NewStarterPython.html" />
<link rel="prev" title="Doxygen Setup" href="DoxygenSetup.html" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59110517-1', 'auto');
ga('send', 'pageview');
</script>
</head><body>
<div id="navbar" class="navbar navbar-default ">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://www.mantidproject.org">
</a>
<span class="navbar-text navbar-version pull-left"><b>main</b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li class="divider-vertical"></li>
<li><a href="index.html">Home</a></li>
<li><a href="https://download.mantidproject.org">Download</a></li>
<li><a href="https://docs.mantidproject.org">User Documentation</a></li>
<li><a href="http://www.mantidproject.org/contact">Contact Us</a></li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="index.html">Documentation</a> »</li>
<li class="nav-item nav-item-this"><a href="">New Starter C++</a></li>
</ul>
</div> </p>
</div>
<div class="container">
<div class="row">
<div class="body col-md-12 content" role="main">
<section id="new-starter-c">
<span id="newstarterc"></span><h1>New Starter C++<a class="headerlink" href="#new-starter-c" title="Link to this heading">¶</a></h1>
<nav class="contents local" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#prerequisites" id="id3">Prerequisites</a></p></li>
<li><p><a class="reference internal" href="#version-control" id="id4">Version Control</a></p></li>
<li><p><a class="reference internal" href="#c-basics" id="id5">C++ Basics</a></p>
<ul>
<li><p><a class="reference internal" href="#reading" id="id6">Reading</a></p></li>
<li><p><a class="reference internal" href="#exercise" id="id7">Exercise</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#object-oriented-c-basics" id="id8">Object Oriented C++ Basics</a></p>
<ul>
<li><p><a class="reference internal" href="#id1" id="id9">Reading</a></p></li>
<li><p><a class="reference internal" href="#id2" id="id10">Exercise</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#further-reading" id="id11">Further reading</a></p></li>
</ul>
</nav>
<section id="prerequisites">
<h2><a class="toc-backref" href="#id3" role="doc-backlink">Prerequisites</a><a class="headerlink" href="#prerequisites" title="Link to this heading">¶</a></h2>
<ul class="simple">
<li><p>A copy of the book <a class="reference external" href="https://www.amazon.co.uk/Modern-Absolute-Beginners-Introduction-Programming/dp/1484260465">Modern C++ for Absolute Beginners</a> by Slobodan Dmitrovic. The source code used in the examples and exercises in this book can be found at: <a class="reference external" href="https://github.com/Apress/modern-cpp-for-absolute-beginners">https://github.com/Apress/modern-cpp-for-absolute-beginners</a></p></li>
<li><p>You will also need a computer with a C++ development environment.</p></li>
<li><p>Git: <em>Windows/Mac</em> - Download the latest version from <a class="reference external" href="http://git-scm.com/">http://git-scm.com/</a>; <em>Linux</em> - Install from package manager (ubuntu pkg=git-gui)</p></li>
<li><p>CMake: <em>Windows/Mac</em> - Download from <a class="reference external" href="http://www.cmake.org/download/">http://www.cmake.org/download/</a>; <em>Linux</em> - Install from package manager (ubuntu pkg=cmake-qt-gui)</p></li>
</ul>
</section>
<section id="version-control">
<h2><a class="toc-backref" href="#id4" role="doc-backlink">Version Control</a><a class="headerlink" href="#version-control" title="Link to this heading">¶</a></h2>
<p>You will be using the <a class="reference external" href="http://git-scm.com/documentation">git</a> version control system with Mantid. Here we aim to get you started using git while working through the new exercises. Our code is stored on <a class="reference external" href="https://www.github.com/">github</a> so to get started you will need an account.</p>
<ol class="arabic simple">
<li><p>Navigate to <a class="reference external" href="https://github.com/">github</a>, fill in the details: username (e.g. firstnamelastname), email & password and click sign up.</p></li>
<li><p>Go to <a class="reference external" href="https://help.github.com/articles/set-up-git">https://help.github.com/articles/set-up-git</a> and follow the instructions to set up git for your environment (for windows do NOT use the native app)</p></li>
</ol>
<p>Before you start the exercises below it is a good idea to read <a class="reference external" href="http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository">this</a> page that discusses the basic operations of working with git.</p>
<p>The idea of version control is that snapshots of the development history can be recorded and these states returned to if necessary. As you go through the exercises and get to a point where something is working it is a good idea to <em>commit</em> the changes to your new repository. On Mantid we use what are known as branches within the repository to keep track of a single piece of work. The idea is that each feature/bugfix is developed independently on a separate branch within the repository. When the work is complete, it is tested by another developer and merged within a special branch, called <strong>main</strong>. This branch is reserved code that will form part of that distributed to users. More details on Mantid’s workflow with git can be found <a class="reference external" href="https://developer.mantidproject.org/GitWorkflow.html">here</a>.</p>
<p>While developing the code for your exercises you will work in a separate repository <a class="reference external" href="https://github.com/mantidproject/newstarter">here</a> but the intention is that you will follow the workflow described in the document above and in particular using the commands described in <a class="reference external" href="https://developer.mantidproject.org/GitWorkflow.html#Workflow_Git_Commands">this</a> section. Do not use the macros yet, the aim is to understand the process by using the real commands.</p>
<p>Before starting the exercises, there are some setup steps:</p>
<ul class="simple">
<li><p>Clone this repository: <a class="reference external" href="https://github.com/mantidproject/newstarter">https://github.com/mantidproject/newstarter</a></p></li>
<li><p>Make a new branch <code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">checkout</span> <span class="pre">--no-track</span> <span class="pre">-b</span> <span class="pre">firstname_lastname_exercises</span> <span class="pre">origin/main</span></code>, where <em>firstname</em>, <em>lastname</em> should be replaced as appropriate.</p></li>
<li><p>Make a copy of the <em>exercises-cpp/template</em> directory and name it <em>firstname_lastname</em>. The directory should be in the exercises directory.</p></li>
<li><p>Make a directory called <em>builds</em> in the root of the <em>newstarter</em> repository. Git is setup to ignore this directory.</p></li>
<li><p>Start the CMake Gui</p></li>
<li><p>Point the source directory at <em>exercises-cpp/firstname_lastname</em> and build directory at <em>builds</em></p></li>
<li><p>Click <em>Configure</em> and select <em>Unix Makefiles</em> on <em>Linux/OS X or Visual Studio 2019</em> and click <em>Finish</em></p></li>
<li><p>Assuming there are no errors click <em>Generate</em></p></li>
</ul>
<p>Now you are ready to code the solution to the exercise in your chosen native build environment.</p>
<ul class="simple">
<li><p>As you work use the git commands to commit to your branch and push to GitHub.</p></li>
<li><p>When you think you have completed the exercise you can use the continuous integration build servers to check your work. To do this you first need to create a pull-request. See <a class="reference external" href="https://help.github.com/articles/creating-a-pull-request/">create a pull request</a> for your branch so that it can be reviewed by a senior developer.</p></li>
<li><p>The pull request will kick-off builds on Red Hat and Windows platforms and GitHub will mark up the results of these builds on the pull requests. Try and get each build to a green status before saying it is ready for review. As you push further commits to your branch the PR will update and new builds will kick off to check your work. Continue in this pattern until the builds pass. If you’re not sure how to resolve some errors check with another member of the team.</p></li>
</ul>
</section>
<section id="c-basics">
<h2><a class="toc-backref" href="#id5" role="doc-backlink">C++ Basics</a><a class="headerlink" href="#c-basics" title="Link to this heading">¶</a></h2>
<p>Feel free to skim read sections that you understand, just pay attention to anything that is new to you.</p>
<p>Mantid uses C++17. Comments relating to useful additional features have been added below the relevant sections to introduce further useful concepts in modern C++.</p>
<section id="reading">
<h3><a class="toc-backref" href="#id6" role="doc-backlink">Reading</a><a class="headerlink" href="#reading" title="Link to this heading">¶</a></h3>
<ul class="simple">
<li><p>Chapter 13 Introduction to Strings</p></li>
<li><p>Chapter 14 Automatic type deduction</p></li>
<li><p>Chapter 19 Functions</p>
<ul>
<li><p>also look up online methods for returning more than one value (eg std::tuple)</p></li>
</ul>
</li>
<li><p>Chapter 31 Organizing Code</p></li>
<li><p>Chapter 38 C++ Standard Library and Friends</p>
<ul>
<li><p>std::vector and std::map</p></li>
<li><p>Range based for loops</p></li>
<li><p>Lambda expressions</p></li>
<li><p>Algorithms. Suggest also looking up:</p>
<ul>
<li><p>std::remove (and use with std::erase)</p></li>
<li><p>std::transform</p></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section id="exercise">
<h3><a class="toc-backref" href="#id7" role="doc-backlink">Exercise</a><a class="headerlink" href="#exercise" title="Link to this heading">¶</a></h3>
<p>The code should be placed in <em>exercises-cpp/firstname_lastname/ex01_basics/src”</em></p>
<p>The Visual Studio solution is placed in builds/ex01_basics. On Unix you can type make in <em>builds/ex01_basics</em>. The executable will be place in <em>builds/ex01_basics/bin</em>.</p>
<p>Write a command line program that will:</p>
<ol class="arabic simple">
<li><p>Take a filename of an ascii file as an argument (you can use the example file <a class="reference external" href="https://github.com/martyngigg/cpp-examples/raw/master/Holmes.txt">here</a>)</p></li>
<li><p>Load that ascii file.</p></li>
<li><p>Count the number of occurrences of unique words (longer than 4 characters and split hyphenated words, treating each part as different words). It should be case and punctuation insensitive. You only need to consider the following punctuation characters <code class="docutils literal notranslate"><span class="pre">.,?'"!():</span></code> (hint: you will need a backslash escape character for the double-quote)</p></li>
<li><p>Consider handling of common error cases, such as the wrong file name specified. Return error and status information to the user of the command line tool.</p></li>
<li><p>Write out a results file containing the unique words and the number of uses in descending order of usage, e.g.</p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Word</span> <span class="n">Usage</span>
<span class="n">which</span> <span class="mi">55</span>
<span class="n">holmes</span> <span class="mi">49</span>
<span class="n">there</span> <span class="mi">32</span>
<span class="n">could</span> <span class="mi">25</span>
<span class="n">photograph</span> <span class="mi">21</span>
<span class="o">...</span>
</pre></div>
</div>
</section>
</section>
<section id="object-oriented-c-basics">
<h2><a class="toc-backref" href="#id8" role="doc-backlink">Object Oriented C++ Basics</a><a class="headerlink" href="#object-oriented-c-basics" title="Link to this heading">¶</a></h2>
<section id="id1">
<h3><a class="toc-backref" href="#id9" role="doc-backlink">Reading</a><a class="headerlink" href="#id1" title="Link to this heading">¶</a></h3>
<ul class="simple">
<li><p>Chapter 23 Classes - Introduction</p>
<ul>
<li><p>Member initialization</p>
<ul>
<li><p>also worth looking at this on approaches where an argument is copied in the constructor: <a class="reference external" href="https://clang.llvm.org/extra/clang-tidy/checks/modernize-pass-by-value.html">Modernize Pass By Value</a></p></li>
</ul>
</li>
</ul>
</li>
<li><p>Chapter 25 Classes - Inheritance and Polymorphism</p></li>
<li><p>Chapter 26 Exercises</p>
<ul>
<li><p>const modifier</p></li>
<li><p>calling base class constructor</p></li>
</ul>
</li>
<li><p>Chapter 33 Conversions</p></li>
<li><p>Chapter 35 Smart Pointers</p></li>
<li><p>Chapter 36 Exercises</p></li>
</ul>
</section>
<section id="id2">
<h3><a class="toc-backref" href="#id10" role="doc-backlink">Exercise</a><a class="headerlink" href="#id2" title="Link to this heading">¶</a></h3>
<p>The code should be placed in <em>exercises-cpp/firstname_lastname/ex02_oo_basics/src”</em></p>
<p>The Visual Studio solution is place in builds/ex02_oo_basics. On Unix you can type make in <em>builds/ex02_oo_basics</em>. The executable will be place in <em>builds/ex02_oo_basics/bin</em>.</p>
<p>Write a command line program that:</p>
<ol class="arabic simple">
<li><p>Has classes to allow number of shapes to be defined: square (side1), rectangle(side1, side2), circle(radius), triangle(height, base).</p>
<ol class="arabic simple">
<li><p>Each shape class should know it’s type (“Square”), how many sides it has.</p></li>
<li><p>Each shape needs to be able to calculate it’s perimeter and area. For the triangle you can assume it is isoceles and the perimeter can be computed using <span class="math notranslate nohighlight">\(p = b + 2\sqrt{h^2+(b^2/4)}\)</span>, where <span class="math notranslate nohighlight">\(b\)</span> is the base and <span class="math notranslate nohighlight">\(h\)</span> is the height.</p></li>
</ol>
</li>
<li><p>Within the Main method create a variety of the shapes and put them in a std::vector</p></li>
<li><p>Create a class ShapeSorter which should contain four methods</p>
<ol class="arabic simple">
<li><p>Print out the Shapes that match a chosen type</p></li>
<li><p>Print out the Shapes that match a chosen number of sides</p></li>
<li><p>Print out the Shapes in order of area descending</p></li>
<li><p>Print out the Shapes in order of perimeter descending</p></li>
</ol>
</li>
</ol>
</section>
</section>
<section id="further-reading">
<h2><a class="toc-backref" href="#id11" role="doc-backlink">Further reading</a><a class="headerlink" href="#further-reading" title="Link to this heading">¶</a></h2>
<p>Further modern C++:</p>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#nullptr">nullptr</a></p></li>
<li><p><a class="reference external" href="https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#strongly-typed-enums">Strongly Typed Enums</a></p></li>
<li><p><a class="reference external" href="https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#constexpr">Constexpr</a></p></li>
<li><p><a class="reference external" href="https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#lambda-expressions">Lambdas</a></p></li>
<li><p><a class="reference external" href="https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#type-aliases">Type Aliases</a></p></li>
<li><p><a class="reference external" href="https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#move-semantics">Move Semantics/R-Value References</a></p></li>
</ul>
</section>
</section>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<ul class="nav navbar-nav" style=" float: right;">
<li>
<a href="DoxygenSetup.html" title="Previous Chapter: Doxygen Setup"><span class="glyphicon glyphicon-chevron-left visible-sm"></span><span class="hidden-sm hidden-tablet">« Doxygen Setup</span>
</a>
</li>
<li>
<a href="NewStarterPython.html" title="Next Chapter: New Starter Python"><span class="glyphicon glyphicon-chevron-right visible-sm"></span><span class="hidden-sm hidden-tablet">New Starter Python »</span>
</a>
</li>
<li><a href="#">Back to top</a></li>
</ul>
<p>
</p>
</div>
</footer>
</body>
</html>