COMP20021: Imperative Programming with C and C++ (2008-2009)
This course unit introduces students to the C and C++ programming languages and the tools used to support them.
A student completing this course unit should:
Be able to critically compare C, C++ and Java and identify the strengths and weaknesses of each for a given application area (A2, A3, B3).
Understand the syntax and structure of C and C++ programs (A2).
Be able to design C and C++ programs to meet requirements expressed in English (B2).
Write and debug programs in both languages using appropriate tools (C5, C6).
Use 3rd party libraries of functions or classes (C5).
Understand how C and C++ are implemented on the host architecture (A3).
Assessment of Learning outcomesLearning outcome (1) is assessed by examination. Learning outcome (2), (4) and (5) in the laboratory, and learning outcome (3), and (6) by both examination and in the laboratory.
Contribution to Programme Learning OutcomesA2, A3, B2, B3, C5, C6, D4
Introduction and Motivation 
What C and C++ are used for (embedded systems, operating systems, real-time systems, device drivers, computer graphics). Brief recap on the CPU/store model. Compilation of Java to its VM and byte-codes compared with compilation of C/C++ to native instructions. Pros and cons of these approaches.
Basic C 
Very similar constructs to Java (control statements, loops, basic data types, expression evaluation). Similar rules for variable names, similar variable scope, similar operator precedence. Some of the differences (size of data types not fixed size). Functions not methods.
The anatomy a simple `standalone' C program
Compiling, linking and executing 
The compiler and pre-compiler. Header files and libraries. Dynamic and static linking. Running a program. Comparison of command line and IDE compilers.
Introduction to Lab Exercise 1: The Mandelbrot Set 
What is the Mandelbrot Set, why is it interesting? Writing a C program from the ground up: some good practice hints. The MSet library and header file. Inspecting a program with nm and ldd. The Mset formula. Progressive refinement. Exploring the set with a mouse.
Aggregate types 
Structs and Typedefs, Unions, Arrays and Enums. How these relate to objects/classes or abstract data types.
Pointers and memory management 
C Pointers and Java references. Malloc compared to Java's `new'. Manual memory management. Const pointers. Pointers and arrays. Command line arguments. Pointer errors: segmentation faults and bus errors. Examples of dynamic data structures: linked lists, trees.
Function Pointers and callbacks 
Comparison with non-final methods in Java / `dynamic binding' of functionality at runtime.
Introduction to Lab Exercise 2: Pangolins 
What is a pangolin? A simple expert system, using a tree to store questions. The user things of an object, the program learns how to guess what this object is by accumulating a decision tree of `yes/no' questions. Exercises string manipulation, dynamic data structures (trees) and memory management. Introduces the `valgrind' program to test for memory corruption or leaks. The basic program is extended to have a `load', `save' and `merge' functions.
I/O in C 
File handling, incompatibilities between operating system versions (e.g. handling of carriage return)
Esoteric Features 
Variable parameter function calls, dark and scary features (`,', `?:', goto, break, continue)
Variations on a theme: overview of Objective C, C# and C++ 
Basic C++ 
Simple class definition, similarities with Java (private, public, protected). Constructors and Destructors.
Inheritance and Virtual Functions 
The QT toolkit as an example of a deep hierarchy of conserved functions. Virtual functions and dynamic binding (comparison with Java methods). Multiple inheritance compared to Java interfaces.
Introduction to Exercise 3: A drawing program 
A more complex program. Use of multilple files, a makefile and the linker. Dynamic data structures, a hierarchy of drawing primitives (bitmaps, poly-lines). An event based programming model.
Templates and the STL 
Class templates for implementing generic constructs like vectors, stacks, lists, queues that can be used with any arbitrary type. C++ templates provide a way to re-use source code as opposed to inheritance and composition which provide a way to re-use object code. Function templates as a way of building generic algorithms.
C++ behind the scenes 
How C++ classes are `implemented in C'; constructors and destructors and virtual functions. What inherited classes look like in memory.
C++ Type system 
Old style deprecated casts and coercions. New style casts (const, static, dynamic and void) and the run-time type system.
C and C++ Best practice 
Side effects, obfuscated C, dangers of multiple inheritance, mad operator overloading. Use const.