PDF Archive

Easily share your PDF documents with your contacts, on the Web and Social Networks.

Send a file File manager PDF Toolbox Search Help Contact



Thinking in C++.pdf


Preview of PDF document thinking-in-c.pdf

Page 1 2 3451104

Text preview


3: The C in C++

73

Creating functions .........................73
Using the C function library ............ 76
Creating your own libraries with the
librarian ........................................ 76

Controlling execution....................77
True and false................................ 77
if-else ........................................... 77
while............................................ 78
do-while....................................... 79
for ............................................... 80
The break and continue Keywords . 81
switch .......................................... 82
Recursion...................................... 84

Introduction to operators ..............84
Precedence.................................... 85
Auto increment and decrement ........ 85

Introduction to data types .............86
Basic built-in types ........................ 86
bool, true, & false ......................... 87
Specifiers...................................... 88
Introduction to Pointers.................. 90
Modifying the outside object ........... 94
Introduction to C++ references........ 96
Pointers and references as modifiers 97

Scoping............................................99
Defining variables on the fly ......... 100

Specifying storage allocation.....102
Global variables........................... 102
Local variables ............................ 103
static.......................................... 103
extern ........................................ 105
Constants.................................... 106
volatile....................................... 107

Operators and their use...............108
Assignment ................................. 108
Mathematical operators................ 108
Relational operators..................... 110
Logical operators......................... 110
Bitwise operators......................... 111
Shift operators............................. 111
Unary operators........................... 114
The ternary operator..................... 115
The comma operator .................... 115
Common pitfalls when using operators116
Casting operators......................... 116
sizeof – an operator by itself ......... 117
The asm keyword........................ 117
Explicit operators......................... 117

Composite type creation.............118
Aliasing names with typedef......... 118
Combining variables with struct ... 119
Clarifying programs with enum .... 122
Saving memory with union........... 123
Arrays ........................................ 124

Debugging hints .......................... 132
Debugging flags........................... 133
Turning variables and expressions into
strings......................................... 134
The C assert( ) macro................... 135

Make: an essential tool for separate
compilation................................... 136
Make activities............................. 137
Makefiles in this book.................. 139
An example makefile ................... 140

Summary ....................................... 142
Exercises....................................... 142

4: Data abstraction

145

A tiny C-like library.................... 146
Dynamic storage allocation ........... 149
Bad guesses ................................. 152

What's wrong? ............................. 153
The basic object........................... 154
What's an object? ........................ 160
Abstract data typing.................... 161
Object details ............................... 161
Header file etiquette.................... 163
Importance of header files............. 163
The multiple-declaration problem .. 165
The preprocessor directives #define,
#ifdef and #endif ......................... 165
A standard for header files ............ 166
Namespaces in headers................. 167
Using headers in projects.............. 167

Nested structures ......................... 168
Global scope resolution................. 171

Summary ....................................... 172
Exercises....................................... 172

5: Hiding the implementation
177
Setting limits ................................ 177
C++ access control...................... 178
protected.................................... 180

Friends .......................................... 180
Nested friends.............................. 182
Is it pure?.................................... 185

Object layout................................ 185
The class ....................................... 186
Modifying Stash to use access control188
Modifying Stack to use access control189

Handle classes ............................. 190
Hiding the implementation ............ 190
Reducing recompilation ................ 190

Summary ....................................... 193