From C++03 to C++11

From Gridkaschool
Revision as of 10:23, 1 September 2014 by Mheck (talk | contribs) (Gridka School slides)

Gridka School 2014 C++ course

  • Martin Heck, KIT EKP
  • Jörg Meyer, KIT SCC

Literature on C++

C++ books

  • The C++ Programming Language, Bjarne Stroustrup
  • Effective C++, Scott Meyers
  • More Effective C++: 35 New Ways to Improve Your Programs and Designs
  • Modern C++ Design, Andrei Alexandrescu
  • The C++ Standard Library, Nicolai M. Josuttis
  • C++ Templates, David Vanevoorde, Nicolai M. Josuttis
  • Exceptional C++, Herb Sutter
  • More Exceptional C++, Herb Sutter

C++11 books

  • The C++ Programming Language, 4th Edition, Bjarne Stroustrup
  • The C++ Standard Library: A Tutorial and Reference (2nd Edition), Nicolai M. Josuttis
  • C++11 programmieren, Torsten T. Will (German)
  • C++11: Der Leitfaden für Programmierer zum neuen Standard, R. Grimm (German)

C++ links

C++11 links

Technical aspects for the course

Gridka School slides

  • Bug avoidance: The C++11 Standard offers new ways in which the compiler can help you to avoid bugs. Especially the new keywords override, final, and smart pointers.

access to machines

You can run the exercises either on your own computer or ssh to one of our prepared machines using your Gridka School account.

  • connection from Linux/Unix: ssh -p24 <username>@gks-virt<xxx>.scc.kit.edu
  • connection from Windows: use an ssh client like putty, or use cygwin

This is a list of hostnames: gks-virt084 gks-virt088 gks-virt098

compilaton and execution of code

Most examples consist of just one cpp file, i.e. no header file, no additional library. Use the following commands to compile and run the code:

g++ mycode.cpp
./a.out

For C++11 support do:

g++ -std=c++11 mycode.cpp
./a.out

exercises

smart pointer

  • Add a memory management to the binary search tree by introducing adequate smart pointers.
  • In which order do the nodes get deleted?

binary_tree.hpp

#ifndef BINARY_TREE_HPP
#define BINARY_TREE_HPP

#include<initializer_list>

class binary_tree {
  struct node {
    node(int k): key(k), left(0), \
         right(0), p(0) {}
    int key;
    node *left, right, *p;
  };

public:
  node* root;
  void insert(node* z);
  void inorder_print(node* x);
  binary_tree(std::initializer_list<int> values): root(0) {
    for (auto it=values.begin(); \
      it!=values.end();++it) 
	insert(new node(*it));
  }
};

#endif

binary_tree.cpp

#define BINARY_TREE_CPP
#include<iostream>
#include "binary_tree.hpp"

void binary_tree::insert(node* z) {
    node* y=0;
    node* x=root;
    while (x) {
      y=x;
      if (z->key < x->key) 
	x=x->left;
      else
	x=x->right;    
    }
    z->p = y;
    if (!y)
      root=z; // tree was empty
    else if (z->key < y->key)
      y->left = z;
    else
      y->right = z;
}

void binary_tree::inorder_print(node* x) {
    if (x) {
      inorder_print(x->left);
      std::cout<<x->key<<" ";
      inorder_print(x->right);
    }
}


int main() {
  binary_tree bt{12,5,5,7,2,4};
  bt.inorder_print(bt.root);//2 4 5 5 7 12  
}