Notities V2CPSE2

Submitting your solutions

Place your solution code in a private github repository. Invite github user ‘fdannenberg’ to your repository. Send me the link to your repository by emailing

Your repository should not contain *.o, *.exe, *.dll or *.vcxproj files. Your repository needs to contain a makefile with the appropriate “make build”, “make clean” and “make run” commands, using the gcc compiler. If you work in visual studio, this means your program now has two compilation paths: one via Microsoft C/C++ Compiler and one via gcc. It is also possible to make Visual Studio use gcc compiler.

The name of your repository should be ‘v2cpse2-last-name’ or similar.

Week 1: Introduction

Example repository is found here:

git clone

You will need to have installed:

sfml sfml-devel boost

To compile and run 01-02 static ball on linux, do:

g++ -std=c++17 ball.cpp main.cpp -o ball -lsfml-graphics -lsfml-window -lsfml-system -lstdc++

and run normally with ./ball. This gives:

Screenshot from 2018-11-18 21-15-10

Assignment 2: Walls and bouncing

Sf objects you will use:

sf::RectangleShape, sf::CircleShape, sf::Vector2f

Some steps along the way:

  1. Create a makefile with the appropriate clean, build and run commands.
  2. Create a ‘drawable’ superclass with virtual method draw. The signature of ball becomes: class ball : public drawable.
  3. Create a wall class with signature “class wall: public drawable” and create the appropriate walls.
  4. Rename the ‘ball’ class to be ‘controllable’ and replace the ball shape with a rectangle.

After steps 1-4,  your program could look like:

Screenshot from 2018-11-20 14-45-35

where the small square is controllable by mouse and keyboard. To finish the assignment:

  1. Create a ball class that is ‘drawable’ and add it to the scene.
  2. Extend the drawable class with a method ‘move’ and set up the ball to have an initial velocity.
  3. Use the method getGlobalBounds to obtain instances of Rect from RectangularShape objects. The Rect class has attributes top, left, height and width and the method <Rect>.intersects(<Rect>). Use these attributes to make the ball bounce off the various objects.

Tip: A different routine can be used instead of step (3). The ball will have a velocity in the x and y direction. By first moving the ball in the x direction, and checking for overlap, before doing the same for the y direction, you can infer in which direction the ball should bounce to.

An instance of your program could look like:

Screenshot from 2018-11-20 20-10-30

where the ball is bouncing and the rectangle is controlled by keyboard and mouse.

To complete the exercise, make sure that:

  1. The application has a makefile and can be run from the commandline.
  2. The dimensions of the ball, walls and movable rectangle are approximately as in the figure above.
  3. The ball bounces around naturally, and
  4. You applied some sophistication to the collision detection of the ball.

Week 3: Factory

In this assignment you have to:

  1. Read graphical object descriptions from a file.
  2. Instantiate the appropriate graphical objects using a factory method.
  3. Display the graphical objects using the SFML library.
  4. Allow the user to select the objects on the screen, and change their position.
  5. Upon closing the program, the graphical objects need to be saved to file.

In addition, make sure that you:

  1. Implement the Factory pattern, and
  2. Use the Resource Acquisition Is Initialization (RAII) pattern to manage the lifetime of your graphical objects.
  3. Have a minimum of two movable objects.
  4. Employ error handling to prevent changes to the file when errors occur.

Here is an example of how a factory could look like:

unique_ptr<drawable> graph_factory::parse(vector<string>& line) {

    float height = stof(line[1]);
    float width = stof(line[2]);

    if (line[0] == "wall") {

        unique_ptr<drawable> nwall (new wall { height, width });
        return nwall;


    return nullptr;

where the parse method itself is declared static in the header. Observe that this code works when class <drawable> is a superclass of <wall>. Note how the ownership of the object, in the form of a unique_ptr, is passed from the factory method, and handed off to where the code is called. Also note the ‘stof’ function that creates a float from a std::string.

Calling the method might be done as follows

unique_ptr<drawable> wall = graphics_factory::parseInfo(fileline);

To implement the RAII pattern, the header for a class that reads and writes graphical object to file and maintains ownership of the graphical objects, could look like:

class graphic_io {

graphic_io(string filename);

vector<unique_ptr<drawable>> graphical_objects;

string filename;


where the deconstructor is responsible for both writing the file and releasing the  graphical objects.

Here is how input to the program and corresponding display could look like:

wall 160.0 80.0 320.0 20.0
wall 160.0 80.0 20.0 320.0
wall 480.0 400.0 -320.0 -20.0
wall 480.0 400.0 -20.0 -320.0
ball 280.0 280.0 20.0
ball 320.0 320.0 20.0

Screenshot from 2018-11-27 18-34-01

After adjusting the some of the graphical objects, the program writes to disk:

wall 186.0 52.0 320.0 20.0
wall 160.0 80.0 20.0 320.0
wall 480.0 400.0 -320.0 -20.0
wall 521.0 413.0 -20.0 -320.0
ball 280.0 280.0 20.0
ball 406.0 257.0 20.0

Screenshot from 2018-11-27 18-31-32

Your program should throw exceptions when the input file is not of the proper format.

Week 4: STD Algorithms

Wanneer je een std::array<int,26> hebt met daarin hoe vaak iedere letter voorkomt, dan kun je de lijst sorteren op de frequentie op de volgende manier. Maak eerst een std::vector met daarin std::pair<char,int>. Pas vervolgens std::sort toe. Zie ook

Week 5: Tic-Tac-Toe

Use enums to structure your program. The following example, from cpp reference, helps with writing enums to cout.

// based on

enum class altitude: char
     mid = 'm',

std::ostream& operator<<(std::ostream& os, altitude c)
        case high  : os << "hoog";    break;
        case mid   : os << "midden"; break;
        case low   : os << "laag";  break;
        default    : os.setstate(std::ios_base::failbit);
    return os;

Include a ‘restart’ and an ‘undo’ button, like so:

Screenshot from 2018-12-11 20-23-01Screenshot from 2018-12-11 20-22-50