Week 10

Lecture slides (Classes Part 2)

Constructors

Note: correction; the default constructor does not initialize variables.

struct S {
    int i;
}

int main() {
    S s;
    cout << s.i << endl;  // this is undefined behaviour, 'i' has not been initialized
}

Synthetic Default Constructor and Initializer Lists

16.1-clock.cpp

16.1-clock.h

16.1-main.cpp

Converting Constructor

Note: correction; constructors with default parameters work in the same way as functions with default parameters. Clock("11:11:PM") will invoke the string conversion constructor, not the three-argument constructor because the three-argument constructor requires that its first argument is an int (not a string).

Code Walkthrough

16.2-clock.cpp

16.2-clock.h

16.2-main.cpp