New Starter C++


Version Control

You will be using the git 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 github so to get started you will need an account.

  1. Navigate to github, fill in the details: username (e.g. firstnamelastname), email & password and click sign up.

  2. Go to and follow the instructions to set up git for your environment (for windows do NOT use the native app)

Before you start the exercises below it is a good idea to read this page that discusses the basic operations of working with git.

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 commit 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 main. 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 here.

While developing the code for your exercises you will work in a separate repository here but the intention is that you will follow the workflow described in the document above and in particular using the commands described in this section. Do not use the macros yet, the aim is to understand the process by using the real commands.

Before starting the exercises, there are some setup steps:

  • Clone this repository:

  • Make a new branch git checkout --no-track -b firstname_lastname_exercises origin/main, where firstname, lastname should be replaced as appropriate.

  • Make a copy of the exercises-cpp/template directory and name it firstname_lastname. The directory should be in the exercises directory.

  • Make a directory called builds in the root of the newstarter repository. Git is setup to ignore this directory.

  • Start the CMake Gui

  • Point the source directory at exercises-cpp/firstname_lastname and build directory at builds

  • Click Configure and select Unix Makefiles on Linux/OS X or Visual Studio 2019 and click Finish

  • Assuming there are no errors click Generate

Now you are ready to code the solution to the exercise in your chosen native build environment.

  • As you work use the git commands to commit to your branch and push to GitHub.

  • 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 create a pull request for your branch so that it can be reviewed by a senior developer.

  • 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.

C++ Basics

Feel free to skim read sections that you understand, just pay attention to anything that is new to you.

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++.


  • Chapter 13 Introduction to Strings

  • Chapter 14 Automatic type deduction

  • Chapter 19 Functions

    • also look up online methods for returning more than one value (eg std::tuple)

  • Chapter 31 Organizing Code

  • Chapter 38 C++ Standard Library and Friends

    • std::vector and std::map

    • Range based for loops

    • Lambda expressions

    • Algorithms. Suggest also looking up:

      • std::remove (and use with std::erase)

      • std::transform


The code should be placed in exercises-cpp/firstname_lastname/ex01_basics/src”

The Visual Studio solution is placed in builds/ex01_basics. On Unix you can type make in builds/ex01_basics. The executable will be place in builds/ex01_basics/bin.

Write a command line program that will:

  1. Take a filename of an ascii file as an argument (you can use the example file here)

  2. Load that ascii file.

  3. 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 .,?'"!(): (hint: you will need a backslash escape character for the double-quote)

  4. 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.

  5. Write out a results file containing the unique words and the number of uses in descending order of usage, e.g.

Word    Usage

which           55
holmes          49
there           32
could           25
photograph      21

Object Oriented C++ Basics


  • Chapter 23 Classes - Introduction

    • Member initialization

  • Chapter 25 Classes - Inheritance and Polymorphism

  • Chapter 26 Exercises

    • const modifier

    • calling base class constructor

  • Chapter 33 Conversions

  • Chapter 35 Smart Pointers

  • Chapter 36 Exercises


The code should be placed in exercises-cpp/firstname_lastname/ex02_oo_basics/src”

The Visual Studio solution is place in builds/ex02_oo_basics. On Unix you can type make in builds/ex02_oo_basics. The executable will be place in builds/ex02_oo_basics/bin.

Write a command line program that:

  1. Has classes to allow number of shapes to be defined: square (side1), rectangle(side1, side2), circle(radius), triangle(height, base).

    1. Each shape class should know it’s type (“Square”), how many sides it has.

    2. 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 p = b + 2\sqrt{h^2+(b^2/4)}, where b is the base and h is the height.

  2. Within the Main method create a variety of the shapes and put them in a std::vector

  3. Create a class ShapeSorter which should contain four methods

    1. Print out the Shapes that match a chosen type

    2. Print out the Shapes that match a chosen number of sides

    3. Print out the Shapes in order of area descending

    4. Print out the Shapes in order of perimeter descending

Further reading

Further modern C++: