C70083 Principles and Practice of Programming


Part I: An Introduction to the Imperative Part of C++


Rob Miller, September 1996

amended by David Clark, September 1997

amended by Bob White, September 1998

amended by William Knottenbelt, September 1999 - September 2023



Go straight to contents of:

Lecture 1, Lecture 2, Lecture 3, Lecture 4, Lecture 5, Lecture 6, Lecture 7, Lecture 8, Guide to emacs and g++, Debugging


These lecture notes are designed for an introductory course on programming, using the imperative core of C++, and given to MSc (Computing Science) students at Imperial College London at the very beginning of their course. The students attend an intensive series of lectures and laboratory sessions, carrying out lab work using the GNU g++ compiler on PCs running a flavour of UNIX. Since the course is intended for graduates from disciplines other than Computer Science, very little previous programming experience is assumed.

Program Listings in the Notes

All the example programs referred to in the lecture notes and all the example answers to the exercises have been written in ANSI/ISO standard C++, and have been tested using the GNU g++ compiler.

Recommended Books

The books recommended to accompany this course are:

Use of These Notes

Please feel free to use, edit and re-distribute these notes as you wish. It would be appreciated, however, if you could ensure that all references to the original author (i.e. Rob Miller) within both the text and the .html file names are preserved.

William Knottenbelt, Imperial College London, 30 September 2023


Course Structure


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 1: Introducing C++

1.1 Some Remarks about Programming

1.2 The Origins of C++

1.3 ANSI/ISO C++

1.4 The C++ Programming Environment in UNIX

1.5 An Example C++ Program

1.6 Very Simple Input, Output and Assignment

1.7 Simple Flow of Control

1.8 Preliminary Remarks about Program Style

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 2: Variables, Types and Expressions

2.1 Identifiers

2.2 Data Types

Integers

Real numbers

Type Casting

Characters

Strings

User Defined Data Types

2.3 Some Tips on Formatting Real Number Output

2.4 Declarations, Constants and Enumerations

Enumerations

Where to put Constant and Variable Declarations

2.5 Assignments and Expressions

Shorthand Arithmetic Assignment Statements

Boolean Expressions and Operators

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 3: Functions and Procedural Abstraction

3.1 The Need for Sub-programs

3.2 User-defined Functions

3.3 Value and Reference Parameters

Functions which use Value Parameters are Safe

Reference Parameters

3.4 Polymorphism and Overloading

3.5 Procedural Abstraction and Good Programming Style

3.6 Splitting Programs into Different Files

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 4: Branch and Loop Statements

4.1 Boolean Values, Expressions and Functions

Note: The Identifiers "true" and "false" in C++

4.2 "For", "While" and "Do ... While" Loops

4.3 Multiple Selection and Switch Statements

4.4 Blocks and Scoping

4.5 A Remark about Nested Loop Statements

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 5: Files and Streams

5.1. Why Use Files?

5.2 Streams

Creating Streams

Connecting and Disconnecting Streams to Files

5.3 Checking for Failure with File Commands

5.4 Character Input and Output

Input using "get()"

Output using "put()"

The "putback()" Function

5.5 Checking for the End of an Input File

5.6 Streams as Arguments in Functions

5.7 Input and Output Using ">>" and "<<"

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 6: Arrays and Strings

6.1 The Basic Idea and Notation

Declaring an array

Assignment Statements and Expressions with Array Elements

6.2 Arrays as Parameters in Functions

6.3 Sorting Arrays

6.4 Two-dimensional Arrays

6.5 Strings

The Sentinel String Character '\0'

String Variable Declarations and Assignments

Some Predefined String Functions

String Input using "getline()"

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 7: Pointers

7.1 Introducing Pointers

Declaring Pointers

Assignments with Pointers Using the Operators "*" and "&"

The "new" and "delete" operators, and the constant "NULL"

7.2 Array Variables and Pointer Arithmetic

7.3 Dynamic Arrays

7.4 Automatic and Dynamic Variables

7.5 Linked Lists

The "." and "->" Operators

Creating a Linked List

Printing a Linked List

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Lecture 8: Recursion

8.1 The Basic Idea

8.2 A Simple Example

8.3 The Mechanics of a Recursive Call

8.4 Three More Examples

8.5 Recursion and Iteration

8.6 Recursive Data Structures

8.7 Quick Sort - A Recursive Procedure for Sorting

Exercises


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8

Appendix 1 - Guide to emacs and g++


Appendix 2 - Debugging

A.2.1 General Tips on Debugging

The "assert" Function

Stubs and Drivers

Some Common Errors to Look Out For

Debugging Very Bad Programs

A.2.2 The GNU debugger gdb


Contents of Lecture: 1, 2, 3, 4, 5, 6, 7, 8