Advanced Programming

course notes by Kees van Kempen ()

Notes are going to be quite brief, with a sparkle of personal comments. As there only was one whole lecture, the rest is gonna be something of a summary of useful facts.

The course seems to follow Wouter Verkerke’s course on Introduction to C++ and Object Oriented Programming from 2006 with updates in 2015 called v55, although I found v60 from 2018 somewhere on a NIKHEF webpage.

I found out the hard way that copying code does not work. The rendered LaTeX pdf files sometimes changes hyphens and quotation marks to different characters depending on their place. BAH.

See notes.pdf for a pdf of these wonderful notes.

I figured out http://cpp.sh/ exists. It is really useful for quick debugging of snippets.

Lecture 1 (2021-11-08)

Administrative

Substance

He asks who knows what a pointer is. “Quite a few do not.” Pointers are references to memory addresses. (A reference is something else, though.) The number of memory addresses is limited by the CPU arch. Here comes the difference between 32 and 64 bits. The length of the memory address is 32 bits vs 64 bits. 32 bit computers can often only handle some 4 GB of RAM. He asks us to familiarize ourselves with the concept, but is not giving a thorough lecture about them.

Wrapping up

He wraps up after the 45 minutes with Walter (TA) giving some explanation about that he is writing some introductory thing and that Discord is our communication channel of choice. He and Frank encourage us to ask questions in there so other can also benefit from it. They also recommend looking at the C&CZ wiki for information about, e.g., Gitlab and ssh.

1 The basics of C++

2 Files and Functions

3 Class Basics

4 Class Analysis & Design

Object Oriented Analysis and Design

A first shot at decomposing your problem into classes.

Designing the class interface

A story about const

One of the most ubiquitous qualifier in C++ is const. A variable can be declared constant, a pointer can be declared constant, a reference can be declared constant. We can also declare constant what a variable points to. We can also declare that a class cannot be changed by some member function, thus also using constant. Let’s see some examples. Major inspiration is stolen from http://duramecho.com/ComputerInformation/WhyHowCppConst.html.

// A constant value
const int var1 = 69;

// A variable pointer to a constant value
const int*    var2 = 69;
int const*    var3 = 69;
(int const)*  var4 = 69;
(int const) * var5 = 69;

// A pointer to a constant address with a variable value
int* const   var6 = 69;
(int*) const var7 = 69;

// A constant address to a constant variable
int const* const   var8  = 69;
(int const)* const var9  = 69;
const (int const)* var10 = 69;

And a completely working example:

#include <iostream>

int main()
{
    int* temp = new int;
    *temp = 70;
    
    // A constant pointer to a variable value
    int* const var1 = temp;
        
    // A constant address to a constant variable
    int const* const var2 = temp;
    const int* const var3 = temp;
    
    // A constant value
    const int var4 = 69;
    
    // A variable pointer to a constant value
    const int* var5;
    int const* var6;
    
    std::cout << *var2;
    *temp = 71;
    std::cout << *var2;
    // The following is illegal
    //*var2 = 90;
    // But we can alter temp, and thus alter the value of var2
    std::cout << *var2;
    std::cout << std::endl << temp << std::endl << var2;
}

I mostly like the conclusion of said website:

Basically ‘const’ applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right).