Dr Christos Kloukinas
- OOP in C++
Reader
Office A205
Department of Computer Science
City St George's, University of London
Northampton Square
London EC1V 0HB
C.Kloukinas (at) City.Ac.UK
tel: +44 20 7040 8848
fax: +44 20 7040 0244
Staff.City.Ac.UK/c.kloukinas
Reaching the College building (where my office is) from Angel. (https://bit.ly/fdc0K2)
Reaching the main University building from Angel. (https://bit.ly/fNKDnq)
Programming in C++
Getting the C++ tools
You can use whatever C++ compiler you like, as long as it supports C++20. Note however that your coursework will need to compile and run with g++ version 11 or higher, as that's what I'll test it against.Practical notes:
- Quick way to test your code - An on-line C++ compiler (g++ with C++20 - actually it supports C++23!)
- Instructions for installing command-line tools (Mac, Windows+WSL) on your computer
- Instructions for using C++ in the labs
- A fast and easy to install Windows editor with syntax highlighting for various languages is Notepad++ (can even install it on your City account - just download its zip file and extract it somewhere, no admin rights needed)
Material
-
General:
- Syllabus in HTML
- Handouts in PDF (these may change!)
- Avoid casts, as if they were Burr holes... (if you think you need a cast, you've probably misunderstood something very basic)
- C++ reference
- C++ FAQ
- C++ FQA (a critique of C++)
- C++ Core Guidelines
- Anthony Calandra's cheatsheet of modern C++ language and library features
- Bjarne Stroustrup's C++ web page
- Collected Papers of Alexander A. Stepanov (creator of the Standard Template Library) - check his (free) Elements of Programming book at some point.
- Procedural, OO, functional, monadic, RESTful - what do these programming styles actually mean/look like?
Have a look at the code
from Cristina Videira Lopes's excellent book Exercises
in Programming Style (2nd edition).
Same problem, coded in a different style each time, all in Python (some have more than one language implementation): - Floating-point numbers are *NOT* Real numbers (1.0/3.0)*3.0 != 1.0 - avoid them! (unless you know what numerical stability is and are implementing a numerical function... then be careful!)
By the way, sometimes (f != f) when f is a floating point number... (did I mention that you should avoid using floating-point numbers?) - Misc:
- Reference cards for various things (and more)
- What's the best text editor? - Andrei Alexandrescu (author of "Modern C++ Design", scopes, etc.)
- Reference cards for various things (and more)
- C++ Common Knowledge - Dawid Zalewski - Meeting C++ 2023 (what this module tries to teach)
- Typical C++, But Why? - Björn Fahller - Meeting C++ 2023 (how to use types to make some errors impossible - security, using apples where oranges are expected, etc.)
- Back to Basics: Casting - Brian Ruth - CppCon 2021 (the complexity of casts)
- Slides of the "Back to Basics: Casting" talk
- If in this module you feel you need to use a cast, then you have misunderstood something *very* basic.
- Casts are like Burr holes - "small holes that a neurosurgeon makes in the skull [...] to help relieve pressure on the brain when fluid, such as blood, builds up and starts to compress brain tissue."
They are not to be done by anyone, nor are they to be done casually.
Casts drill holes in what the compiler knows about the type of different things - they tell it that what it thought it knew is no longer so (and it throws its little arms up in the air in frustration, no more putting any effort in making sure that your code type-matches, i.e., that you're not filling up a diesel car with petrol...)
-
Sessions:
(Lab material are part of each session)Note 1: Lab solutions will only be provided at about a week after the lab.
Note 2: If out of curiosity you look at a later session and find it difficult to understand, don't worry, we'll explain it all in the class! (Also: ask questions!!!)- Session 1: Introduction to C++; Parameter passing by value and reference.
- Session 2: Classes in C++.
- Session 3: Operator overloading; I/O in C++.
- Session 4: Genericity and C++ templates; Introducing the Standard Template Library (STL).
- Session 5: Pointers and arrays; Iterators in the STL.
- Session 6: inheritance and dynamic binding in C++; Genericity, pointers and inheritance.
- Session 7: Multiple inheritance.
- Session 8: Memory management: static, stack, dynamic; Construction and destruction of objects.
- Session 9: Memory management, continued; Implementing a container class; Program structure, separate compilation, header files.
- Session 10: Resource management and exceptions.
- Quick summary of C++11 main changes (from Herb Sutter's talk below on what's new in C++11)
- Herb Sutter's whole talk (and slides) presenting C++11 changes
- Learn to use (none of these will be examined but needed for a job as a C++ programmer):
- auto
- Lambda (anonymous) functions
The reference page for lambdas. - for_each instead of for
- the smart pointers unique_ptr (replaced auto_ptr that was broken), shared_ptr, and weak_ptr
- Rvalue references and moving
- A Brief Introduction to Rvalue References by Howard E. Hinnant, Bjarne Stroustrup, and Bronek Kozicki
- C++ Rvalue References Explained by Thomas Becker
- Move semantics and rvalue references in C++11 by Alex Allain
- Universal References in C++11 - T&& Doesn't Always Mean "Rvalue Reference" by Scott Meyers (just when one thinks they've understood rvalue references...)
Exams:
Well, as of 2020 there's no longer an exam for this module but this is a quick summary of what you should learn in this module so that you can program in C++.
Use the handouts (one pdf file accessible above) to easily search for something.
Christos' journey in C++:
- 2005 - RAII doesn't always work (when there's an exception but no corresponding try-catch block) - G++ bug report (not a bug, a feature!): https://gcc.gnu.org/bugzilla/show_bug.cgi?id=20976
- 2022 - no copy_if_and_transform in the standard library? Here's how to do it! (with execution policies and all): https://stackoverflow.com/questions/23579832/why-is-there-no-transform-if-in-the-c-standard-library/74288551#74288551