288x Filetype PDF File size 1.28 MB Source: cse.unl.edu
Lecture Notes on C++ for Java Programmers
Jonathan G. Campbell
Department of Computing,
Letterkenny Institute of Technology,
Co. Donegal, Ireland.
email: jonathan dot campbell (at) gmail.com, jonathan.campbell@lyit.ie
URL:http://www.jgcampbell.com/cpp4jp/cpp4jp.pdf
Report No: jc/09/0011/r
Revision 3.0, minor edits, new references, new later chapters, 2009-08-12
12th August 2009
Contents
1 Introduction 1
1.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Recommended Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2.1 Programming and Object-oriented Programming through C++ . . . . . . 1
1.2.2 Games Software Engineering . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.3 General Software Engineering . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.4 Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.5 The C Programming Language . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.6 Games Programming using C++ . . . . . . . . . . . . . . . . . . . . . . 3
1.2.7 Cross Platform Windows Programming . . . . . . . . . . . . . . . . . . . 3
1.2.8 Specification and Correctness . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Plan for the course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Tutorial Introduction to C++ 1
2.1 Get Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.1.1 Your First C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Variables and Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Do While . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 Symbolic Constants and the Preprocessor . . . . . . . . . . . . . . . . . . . . . . 11
2.7 Character Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7.1 File copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.7.2 Character counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.7.3 Line Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.7.4 Word Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.8 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.9 Functions in more detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.9.1 Declaration of functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.9.2 Program Split into Modules . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.10 Creation of Executables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.10.1 Some Basics – Compiling and Linking . . . . . . . . . . . . . . . . . . . . 26
2.10.2 Other Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.10.3 Static versus Shared Libraries . . . . . . . . . . . . . . . . . . . . . . . . 27
2.10.4 Make and Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.10.5 Interpreted Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.10.6 Java — Compiler AND Interpreter . . . . . . . . . . . . . . . . . . . . . . 29
2.10.7 Java — Dynamic Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.10.8 Java Enterprise Edition (J2EE) and .NET . . . . . . . . . . . . . . . . . . 30
0–1
2.10.9 Ultimately, All Programs are Interpreted . . . . . . . . . . . . . . . . . . . 30
2.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3 Variables, Types, etc. in C++ 1
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3.2 Variable names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3.3 Elementary Data Types and Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.3.1 Integer types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.3.2 Floating-point types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3.3 Implementation Dependencies . . . . . . . . . . . . . . . . . . . . . . . . 5
3.4 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.6 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.6.2 Declaration / Definition of Pointers . . . . . . . . . . . . . . . . . . . . . 8
3.6.3 Referencing and Dereferencing Operators . . . . . . . . . . . . . . . . . . 8
3.6.4 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.7 Strings in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.7.2 C-strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.8 Standard String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.9 Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.10 Templates, brief introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.10.1 Template Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.11 Polymorphism – Parametric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.12 Constants and their Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.13 Declaration versus Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.14 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.15 Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.16 Type Conversions and Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.17 Assignment Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . . . 21
3.18 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.19 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.20 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.21 Precedence and Order of Evaluation of Operators . . . . . . . . . . . . . . . . . . 22
4 Control Flow 1
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
4.2 Statements and Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
4.3 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
4.3.1 Two-way Selection – if else . . . . . . . . . . . . . . . . . . . . . . . . . 2
4.3.2 Multi-way Selection - else if . . . . . . . . . . . . . . . . . . . . . . . . . 2
4.3.3 Multi-way Selection – switch - case . . . . . . . . . . . . . . . . . . . . . 3
4.4 Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.4.1 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.4.2 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.4.3 do - while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.5 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.6 goto and Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
0–2
5 C++Program Structure 1
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
5.2 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
5.3 Declarations of Functions – Prototypes . . . . . . . . . . . . . . . . . . . . . . . 2
5.4 Function parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.4.1 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.4.2 Pass-by-value parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.4.3 Pass-by-reference parameters . . . . . . . . . . . . . . . . . . . . . . . . 4
5.4.4 Programmed pass-by-reference via pointers . . . . . . . . . . . . . . . . . 5
5.4.5 Semantics of parameter passing . . . . . . . . . . . . . . . . . . . . . . . 5
5.4.6 Arrays as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.4.7 Default parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.5 Function return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.6 Overloaded function names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.7 Inline functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.8 External variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.9 Scope of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.10 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.11 Heap memory management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.11.2 Operator new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.11.3 Operator delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.11.4 Anonymous variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.11.5 Garbage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.12 Lifetime of variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.12.1 Lifetime of variables – summary . . . . . . . . . . . . . . . . . . . . . . . 18
5.13 Memory layout of a C++ program – a model . . . . . . . . . . . . . . . . . . . . 20
5.14 Initialisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.15 Register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.16 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.17 The C++ Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.17.1 File inclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.17.2 Symbolic constants and text substitution . . . . . . . . . . . . . . . . . . 24
5.17.3 Macro substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
5.17.4 Conditional compilation / inclusion . . . . . . . . . . . . . . . . . . . . . 25
6 Struct, class, union 1
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
6.2 APoint struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
6.3 Operations on Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
6.4 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7 Introduction to Classes and Objects 1
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
7.2 AMemory Cell Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
7.2.1 Informal Specification of the Class . . . . . . . . . . . . . . . . . . . . . . 1
7.2.2 Class Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
7.3 Using Separate Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
0–3
no reviews yet
Please Login to review.