C++ Introduction. Andrew J. Bennieston. July 6, Hello, World or, how to patronise beginning programmers 2

C++ Introduction Andrew J. Bennieston July 6, 2011 Contents 1 An introduction to the Introduction 2 2 Hello, World — or, how to patronise beginning...
Author: Raymond Haynes
5 downloads 2 Views 395KB Size
C++ Introduction Andrew J. Bennieston July 6, 2011

Contents 1 An introduction to the Introduction

2

2 Hello, World — or, how to patronise beginning programmers

2

3 Types, Variables & Operators 3.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4 4

4 Input, Output and command-line arguments 4.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5 6

5 Flow Control 5.1 Conditionals 5.2 Loops . . . 5.3 switch . . . 5.4 Exercises . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

6 . 6 . 7 . 8 . 10

6 Functions 6.1 Function declarations and definitions . . . . . . . . . . . . . . . . . . . . . 6.2 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11 11 12 13

7 Arrays 14 7.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 8 Pointers 14 8.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 9 References 17 9.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1

10 const Variables, const Pointers and const References 10.1 const variables . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Pointer-to-const, const-pointer and const-pointer-to-const 10.3 Reference-to-const . . . . . . . . . . . . . . . . . . . . . . . 10.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

18 19 19 20 20

11 Classes 20 11.1 Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 11.2 Member functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 11.3 Access control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 12 Additional (More Advanced) Exercises

25

References

25

1 An introduction to the Introduction This document does not aim to teach introductory C++ (for documents have no sentience), but the author aims, through the medium of this document, to teach introductory C++. Or, at least, to provide some examples and exercises which may be used to familiarise oneself with the fundamental aspects of the language. It is assumed that the reader is not a complete beginner, and has some exposure to C or rudimentary C++ before reading this. However, the level assumed is low enough that it can be adequately cleared by most intelligent human beings without much forethought. Some of the exercises will be marked as challenging with the notation [Challenge]. These are more challenging than other exercises at that point of the document, and not necessarily particularly challenging to an experienced C++ programmer. On the other hand, one or two exercises (likely towards the end) will include a warning that they do contain relatively advanced concepts or langauge issues, for which one may have to dig into a good C++ book [1]. These exercises are marked with a star, i.e. [Challenge ?]. Multiple ? challenge exercises also exist... these are even more difficult!

2 Hello, World — or, how to patronise beginning programmers In this section, we’ll explore a program which is not quite the simplest valid C++ program. In fact, just for fun, here’s the simplest valid C++ program: int main() { return 0; }

2

A valid hosted C++ program (that is, one which runs on an operating system with a C library) requires an entry point; a function named main() which returns an int and (optionally) takes two arguments, an integer argument count, and an array of pointers to char, representing command-line arguments passed to the program. The return value must be int — programs which use void main() are not valid, and should be treated with the contempt they deserve — the C++ Standard (see [2]) describes this behaviour in more detail. The program above is, therefore, the minimum valid C++ program. It defines a function called main which returns the value 0. Onward, now, to something a little more useful. The classic example is to write a program which prints the message “Hello, World” to the screen. Here goes: #include int main() { std::cout