Index

Symbol

+ (addition operator), 58

& (address-of operator), 178, 181, 185, 200

&& (and operator), 76, 80

= (assignment operator), 36, 64, 76, 359–367

* (asterisk), 185–186, 189, 477

(backslash), 19, 48, 385

, (comma), 98, 148

+ (concatenation operator), 51, 291

* (dereferencing operator), 190

/ (division operator), 58

\ (double backslashes), 385

// (double forward slashes), 20, 22

= (equal sign), 80

== (equality operator), 76, 445

> (greater than operator), 76

>= (greater than or equal to operator), 76

< (less than operator), 76

<= (less than or equal to operator), 76

. (membership operator), 289

% (modulus operator), 58

*/ (multiline comment), 20

/* (multiline comment), 20

* (multiplication operator), 39, 58, 292

!= (not equal to operator), 76, 80

! (not operator), 76, 80

|| (or operator), 76

<< (overloaded assignment operator), 297–300, 362–367

. (period), 395, 455–456, 477

| (pipe character), 129

+ (plus sign), 51, 291

“ (quotation marks), 48, 494

‘ (quote marks), 48, 494

:: (scope resolution operator), 213, 216, 289

; (semicolon), 10, 11, 12, 395

- (subtraction operator), 58

?: (ternary operator), 72–75, 289

~ (tilde), 226

_ (underscore character), 35, 395

{ } (curly braces)

code blocks, 164

If conditionals, 64, 67, 71

nested control structures, 87

source file creation, 4

switch conditional, 85

< > angle brackets, 13, 184, 373, 380

[ ] square brackets, 101, 349

” (double quotation mark) character, 48, 494

’ (single quotation mark) character, 48, 494

-> operator, 344, 346

./ operator, 14

<< operator, 10, 295–300, 382

>> operator, 300

A

About.com Web site, 491

abs() function, 59

absolute values, 59

abstract methods, 283–287

aCar structure, 459

access control, 249–253

ADC (Apple Developer Connection), 27, 485

addition operations, 288, 294

addition operator (+), 58

add() method, 288

addresses

arrays, 190

described, 176

incrementing, 191

memory blocks, 185

passing to functions, 200–206

stored in pointers, 183–185, 189, 193

variables, 178–180, 185, 200

vs. pointers, 181

addresses, computer, 176, 177–180

address-of operator (&), 178, 181, 185, 200

a.exe file, 7

aggregation, 243

algorithms, 290, 433, 440–442

aliases, type, 204, 206

alignment, 177

and operator (&&), 76, 80

angle brackets < >, 13, 184, 373, 380

Anjuta DevStudio IDE, 486

a.out file, 7

append() function, 449

Apple Developer Connection (ADC), 27, 485

Apple Developer Tools, 27, 28, 485

applications

command-line arguments in, 473–477

compiling, 6–9

crashing, 372

creating, 2

executing. See execution

invoked from command-line, 14, 473

keeping open, 16–17

pausing execution of, 16–17, 26

printing values, 38–40

purpose of, 29

running, 2, 14–17

run-time errors, 317–319

stopping, 98

testing, 319

writing in C++, 1–3

area() function, 218

argc (argument count), 473–474

argument count (argc), 473–474

argument vector (argv), 473–475

arguments

command-line, 473–477

considerations, 444

default values, 149–153

functions, 5, 139, 143–148

multiple, 143

optional, 149–153

required, 148

vs. parameters, 148

argv (argument vector ), 473–475

arithmetic operations, 58–63. See also calculations; numbers

addition, 288, 294

cmathheader file, 59

comparison operators, 76–80

division, 58, 292

fractions, 288, 293–295, 386–387

multiplication, 39, 58, 288, 292

operator overloading, 294, 360–367

overview, 58–59

performing, 60–63

subtraction, 58, 292

arithmetic operators, 58–63, 88–91

arithmetic, pointer, 190–191, 193

array elements

accessing with loops, 171, 175

accessing with pointers, 190

assigning values to, 170, 175

calling member functions for, 225

array indexes, 170, 175

array subscription operator [ ], 349

arrays

addresses, 190

advanced concepts, 175

allocating, 349–353

considerations, 432

as containers, 432

creating, 170, 175, 192, 225

data types, 193

deleting, 350

described, 170

dynamic, 349–353, 424–425

elements in. See array elements

for loops and, 98

multidimensional, 175

names, 190, 193

object, 225

passing to functions, 477

pointers to, 190–193

printing values, 175

size of, 170, 175, 351–353, 434

subarrays, 175

using, 171–175, 225

vs. pointers, 349–350

vs. vectors, 198

working with, 170–175

ASCII character chart, 493–494

ASCII files, 458

assert() function, 326–331

assert.h library, 326

assignment operator (=), 36, 63, 76, 359–367

asterisk (*), 185–186, 189, 477

attributes

access levels, 249–253

ambiguity, 232–235

in classes, 208–211

declaring, 210

inherited, 253

static, 268–274

auto storage class, 406

automatic variables, 168, 406, 412

B

 (backspace character), 19, 48, 494

backslash (), 19, 385

backspace character (), 19, 48, 494

bad() function, 122, 131

base classes, 238, 243, 244, 310, 337, 348

base constructor, 245–247

basename, 5

BBEdit, 3

big endian machines, 466

binary files, 458–472

C-style strings, 459

random access to data in, 467–472

reading from, 464–466

vs. plain-text files, 458

writing to, 458–463

binary trees, 432

bitwise copy, 359, 361

blank lines, 18, 19

body, function, 2

bool data type, 55

Boolean values, 131, 214, 219, 228, 321

brackets [ ], 101, 349

branching structure, 64

break statement, 82, 98

buffers, 107

C

C++ code

basic syntax, 2–5

comments in. See comments

compiling. See compiling

generic, 415

implementation, 388

indenting, 18, 19, 71, 87

interface, 388

multiple lines in, 12

organizing in folders, 4

templates. See templates

white space, 18–19

C++ code block, 164

C++ escape sequences, 48

.C extension, 5

.c extension, 5

C++ FAQ Lite site, 490

C++ language

basics, 1–28

case sensitivity of, 2

newsgroups, 491

paradigms, 415

printing text in, 11–13

resources, 489–494

Web sites, 490–491

C preprocessor, 10, 13, 55, 394–397

C++ programs. See applications; code

C++ Standard, 433

C++ Standard Library, 433, 440–442, 441, 442

C++ tools, 479–487

Apple Developer Tools, 27, 28, 485

command-line tools, 27

for debugging, 316, 319, 487

Dev-C++. See Dev-C++ for Windows

overview, 3

trigonometric tools, 59

Unix tools, 486

Xcode. See Xcode

calculated totals, 39, 42–43

calculations. See also arithmetic operations

factorial, 92–97, 320–325

formatting, 42–43

interest rate formula, 59, 61–62

loan calculation formulas, 60–63

mortgage calculation formula, 60–63, 68–70

sensitive, 40

temperature conversion, 108–110, 114–117

capacity() method, 457

carriage character ( ), 48

case sensitivity, 2, 5

casting operators, 47, 182. See also type casting

casts. See type casts

catch block, 337

catch statements, 332

.cc extension, 5

C/C++ Style Guides, 491

cd command, 8

changeVar() function, 199–200, 203–204

char data type, 48–50, 101

character input, 100–104

character variables, 49–50

characters. See also text

ASCII character chart, 493–494

deleting, 105–107

escaping, 19, 494

introduction to, 48–50

printing, 10–13, 49

reading single character, 100–104

in strings, 154

validating, 126

cin (console in), 16–17

cin objects, 100, 104, 105, 108–110, 122

cin.bad() call, 123

cin.fail() call, 123

cin.get() call, 16–17

class hierarchy, 280, 302

class keyword, 416

class T, 416, 418, 421

class templates, 422–427

classes

access control, 249–253

adding attributes to, 208–209

adding methods to, 212–216

adding static members to, 270–274

attributes in, 208–211

base, 238, 243, 244, 310, 337, 348

constants in, 211

creating, 208–211, 217

creating objects from, 217–220

derived, 243

described, 198, 208

design of, 231

friend, 261–266

hierarchy, 238–239

inheritance. See inheritance

instances, 217

instantiating, 431

members, 211, 270–274

methods in, 211

multiple objects from, 217

names, 208, 209, 222, 262, 422

pointers to, 344–348

protection, 249

storage, 406

variables in, 210

clear() function, 123, 124

climits header file, 320

close() function, 133

cmath header file, 59, 60

cmath library, 59

cmd utility, 7

Cocoa technology, 27

code

basic syntax, 2–5

comments in. See comments

compiling. See compiling

generic, 415

implementation, 388

indenting, 18, 19, 71, 87

interface, 388

multiple lines in, 12

organizing in folders, 4

templates. See templates

white space, 18–19

code block, 164

coercion technique, 44

comma (,), 98, 148

command line, 7–9, 14, 476, 477

command-line arguments, 473–477

command-line tools, 27

comments

adding, 20–22

control structures, 87

errors and, 315, 317

format of, 20

header files, 381–382

importance of, 32

multiline, 20–22

uses for, 20, 21

compare() function, 445

comparison operators, 76–80

compiler directives, 13

compiler warnings, 4, 34, 46–47, 318

compilers

cmd utility, 7

g++. See g++ compiler

header files, 388

implementation files, 388–391

purpose of, 5, 9

Terminal application, 7

compile-time check, 279

compile-time errors, 6, 314–316

compile-time type, 279

compiling

entire application, 387

multiple files, 392–393

procedure for, 6–9

single file, 387

while executing, 15

comp.lang.c++ newsgroup, 491

composition, 243

computer memory, 176–180

concatenation, 51, 449

concatenation operator (+), 51, 291

conditional directives, 395

conditionals

else clause, 68–71

if, 64–67

reversing values in, 80

switch, 82–87, 98, 206

console in (cin), 16–17

console input. See cin

console out (cout), 10, 38, 41–43, 49

const char * type, 337

const keyword, 54

constant definitions, 394–397

constant float, 54

constants

assigning values to, 55

in classes, 211

declaring, 54, 55

defining parameters as, 206

defining with preprocessor directives, 55

described, 54

introduction to, 54–55

names, 54, 55, 395

static, 211

using, 54–55

const_cast data type, 47

const_cast operator, 182, 372

constructors

access control, 250–251

base, 245–247

copy, 223, 361–367

creating, 221, 222–225

declaring, 228

default, 223

defining, 221–225, 228

described, 221

empty, 223

exceptions and, 337

inherited, 243–248

names, 221, 250

overloaded, 224

size of, 248

vs. destructors, 226

containers, 432–439

continue statement, 98

control structures, 57, 87

copy constructor, 223, 359–367

cout (console out), 10, 38, 41–43, 49

cout stream, 10, 11

cplusplus.com Web site, 490

.cpp extension, 2, 4, 5

.cpp files, 388

Cprogramming.com Web site, 491

cross-platform issues, 13, 132. See also specific platform

cstddef header file, 343

c_str() function, 459

C-style strings, 459

ctype library, 126

curly braces { }

code blocks, 164

If conditionals, 64, 67, 71

nested control structures, 87

source file creation, 4

switch conditional, 85

.cxx extension, 5

D

.dat extension, 460

data, 467–472

Data Display Debugger (DDD), 487

data types. See also specific data types

aliases, 204, 206

arrays and, 193

complex, 169–206

described, 55

enumeration, 206

numeric, 30–31, 493

signed, 30

size of, 45

unsigned, 30, 33, 351, 493

DDD (Data Display Debugger), 487

debugging. See also errors

assert() function, 326–331

catching exceptions, 332–337

compile-time errors, 6, 314–316

with Gnu Debugger, 487

multiline comment type and, 22

printing variable values, 40, 318

run-time errors, 317–319

techniques for, 314–319

tools for, 316, 319, 331, 487

decimal numbers, 30, 493–494

decimal points, 31, 41–42

declaring variables, 30–35, 37

decrement operators, 88–91

default case, 82, 85

delete [ ] statement, 350–352, 376

delete keyword, 275–278

delete statement, 341–342, 347, 348, 376–378

deleting

arrays, 350

buffers, 107

characters, 105–107

denominator, 288, 293

dereferencing operator (*), 190

dereferencing pointers, 185–186, 189, 194, 200, 275

dereferencing structures, 194

destructors

automatically generated, 231

creating, 227–231

declaring, 228

defining, 226–231

exceptions and, 337

inherited, 244–248

names, 226

purpose of, 226, 248

virtual, 280, 348

vs. constructors, 226

Dev-C++ for Windows, 480–484

adding g++ compiler, 484

configuring, 482

customizing, 483–484

described, 480

Gnu Debugger, 487

included files, 393

installing, 480–481

running for first time, 482

specifying command-line arguments in, 477

using, 24–26

Dev-C++ Resource Site, 26

Developer Tools, 27, 28, 485

directives

compiler, 13

conditional, 395

creating conditionals, 395

defining constants with, 55

preprocessor, 10, 13, 55

semicolons and, 395

directories, 8, 9, 13, 159

division operations, 58, 292

division operator (/), 58

DMCInsights.com Web site, 491

dot syntax, 195

double data types, 30, 31

double quotation mark (”) character, 48, 494

double forward slashes (//), 20, 22

double type, 493

double variables, 417

do...while loops, 92, 95

dynamic arrays, 349–353, 424–425

dynamic memory, 339–378

array allocation, 349–353

assignment operator, 359–367

copy constructor, 359–367

described, 339

dynamic objects and, 372–375

object allocation, 344–348

requesting, 342–343

returning, 342–343, 354–357

scope and, 377

static objects and, 368–375

vs. static memory, 340–343

dynamic object type casts, 372–375

dynamic_cast data type, 47, 373–375

dynamic_cast operator, 182, 372–375

E

E notation, 31

Eclipse IDE, 486

#elif directive, 395

else clause, 68–71, 130, 462

#else directive, 395

else if clause, 68–71

emacs editor, 3, 486

empty() function, 448

#endif directive, 395

endl marker, 18, 33

enum keyword, 206

enumeration, 206

eof() function, 122

equal sign (=), 80

equality operator (==), 76, 445

erase() function, 457

error codes, 320–325

errors. See also debugging; troubleshooting

assert() function, 326

binary files, 463

case sensitivity and, 2

casting operators and, 182

catching exceptions, 332–337

comments and, 315, 317

compile-time, 6, 314–316

namespaces, 400

“off-by-one,” 175

programmers and, 326

return codes, 321–325

run-time, 317–319, 326, 332

templates, 421

throwing exceptions, 332, 337

undefined function, 220

unmatched pointer types, 181

users and, 326

vector elements, 436

virtual methods and, 283

escape characters, 19, 494

escape sequences, 48, 494

Euclid’s algorithm, 290

exception file, 337

exception handling, 332–337

exceptions

catching, 332–337

constructors and, 337

destructors and, 337

managing, 337

objects as, 337

run-time errors and, 332

throwing, 332, 337

use of, 337

vs. return statements, 325

executable files, 7, 14–15

execution

applications, 2, 14–17, 26

compiling and executing in one step, 15

Mac OS X systems, 14–15

stopping, 158

Unix systems, 14–15

Windows systems, 14–15

exit statement, 98

Explorer, 9, 26

exponential power, 59

extensions

.C, 5

.c, 5

.cc, 5

.cpp, 2, 4, 5

.cxx, 5

.dat, 460

determining, 446

.h, 13

header files, 13

.rtf, 3

searching for, 455

source files, 5

strings and, 455

substrings and, 446–448

.txt, 460

external linkage, 168, 408

external storage class, 406, 408, 409

F

f (new page) character, 494

factorial calculation, 92–97

factorial class, 321

factorial objects, 324

factorial values, 155–157, 320–325

factorials, 320–325, 333–336

factory functions, 355–357

factory methods, 357

fail() function, 122

fall-through situations, 102

false conditions, 64–70

comparison operators, 76

for loops, 96

ternary operator, 72–75

while loops, 92–95, 439

false value, 55

file input, 132–136, 318

file output, 127–131

file scope, 408

files

adding white space to, 19

ASCII, 458

binary. See binary files

closing, 133

compiling. See compiling

creating, 3–5

executable, 7, 14–15

extensions. See extensions

header. See header files

implementation, 388–391

included, 380–393

invalid name/path, 231

makefiles, 393

names, 5, 129, 132, 134

opening, 129, 131, 132

paths, 134, 231, 385, 387, 484

source, 3–5, 19

text, 32, 458

unable to open, 131, 231

writing to, 128–131

find feature, 454–457

find _last_of() function, 454

Finder, 9

find_first_not_of() function, 454

find_first_of() function, 454

find() function, 453, 454–456

find_last_not_of() function, 454

fixed-point notation, 41

flags, 17, 131, 494

float data types, 30, 31, 493

float variables, 417

floating-point numbers, 30, 31, 40, 41, 131

floats, equality of, 80

folders, 4

foo() function, 377, 416

for loops, 96–98

formatting numbers, 41–43

forward declaration, 384

forward slash (/), 385

fractions, 288, 293–295, 386–387

friend classes, 261–266

“friends” concept, 211

fstream flags, 494

fstream library, 127, 132

fstream type, 131, 463

function templates, 417–421

functions, 137–168

allocating objects from, 355–357

arguments, 5, 139, 143–148

body of, 2

calling, 141, 225, 372

contents of, 2

creating functions that return values, 154–159

creating functions that take arguments, 143–148

creating simple functions, 138–143

declaring, 216

defining, 137–142, 144–148, 216

described, 137

factory, 355–357

friends, 261

implementing, 139

inline, 153, 216

multiple, 141

names, 138, 142

overloaded, 160–163, 203, 258

overriding, 161

passing addresses to, 200–206

passing arrays to, 477

passing values by reference, 203–206

passing values to, 199, 201–206

prototypes, 138, 139, 418–419, 421

recursive, 159

returning memory, 354–357

returning objects, 355–357

returning values, 3, 4, 154–159

static, 414

stopping execution of, 158

stored in memory, 180

throwing errors from, 337

undefined, 220

user-defined, 199–206

using, 140–142, 144–148

validation, 145, 147, 320

variables within, 164–165, 168

G

g++ compiler

basics, 6–9

compiling multiple files with, 392–393

Mac systems, 28

Windows systems, 484

gcd (greatest common divisor), 293

gcount() function, 107

GDB (Gnu Debugger), 487

generic programming, 415, 416

get() function, 104

getline() function, 118–120, 132–136

getter methods, 274

global scope, 377, 398, 401, 404

global variables, 164–168, 377, 406, 410

Gnu Debugger (GDB), 487

good() function, 122, 127, 131

goto control structure, 87

graphical user interface (GUI), 3

greater than operator (>), 76

greater than or equal to operator (>=), 76

greatest common divisor (gcd), 293

GUI (graphical user interface), 3

H

.h extension, 13

hash mark (#), 394

hash tables, 432

header files

class inheritance and, 384

creating, 381–384

custom, 380–384

described, 10, 380

duplicate, 394, 395

extension for, 13

importance of, 388

included, 380–393

linkage, 410

modifying, 396–397

names, 384

scope, 410

types of, 380–381

using directive and, 405

hexadecimal numbers, 178

hexadecimal values, 178

hierarchy, 238–239, 280, 302. See also inheritance

I

IDE (integrated development environment)

advantages of, 3, 23

alternatives to, 393

Anjuta DevStudio IDE, 486

Eclipse IDE, 486

KDevelop IDE, 486

using, 6, 23–28

identifiers, 31, 128, 198, 398, 399

if conditionals, 64–67

#if directive, 395

#ifdef directive, 395

if-else if statement, 71

if-else statement, 69–70, 71

#ifndef directive, 395

ifstream type, 465, 467

ignore() function, 105–107, 123, 124

implementation, 388

implementation file, 388–391

implicit conversion, 44

#include directive, 13, 380, 394

included files, 380–393

increment operator, 88–91

incrementing pointers, 191

indention, code, 18, 19, 71, 87

indexes, 170, 175, 437–439, 439

infinite loops, 92, 98, 159, 472

infinity value, 95

inheritance, 237–266

access control, 249–253

attributes, 253

basic, 238–243

constructors, 243, 244–248

described, 237

destructors, 244–248

friend classes, 261–266

header files and, 384

methods, 253

multiple, 301–307

protecting members, 250–253

relationships, 243

virtual, 308–311

vs. composition, 243

inherited classes, 344–348

initialization, 36, 37

inline functions, 153, 216

inline keyword, 153

inline notation, 429–431

inline templates, 428–431

input, 100–126

character, 100–104

discarding, 105–107

invalid, 110

multiple, 114–117

numeric, 108–110

reading file input, 132–136

reading in entire lines, 118–120

reading numeric input, 108–110

string, 111–113

unable to read in, 172

user, 111–113

validating, 121–126, 318

input/output stream. See iostream

insert() function, 449

Insight Debugger, 487

instances, 217, 269, 361

int data type, 3, 30, 35, 493

integer variables, 183

integers. See also numbers

ASCII character chart, 493–494

calculations, 40

dividing, 58

overflow, 95

pointers to, 183

reading in, 172

returning factorial value, 155–157

type casting addresses as, 180

underflow, 95

unsigned, 351

vs. real numbers, 58

integrated development environment (IDE)

advantages of, 3, 23

alternatives to, 393

Anjuta DevStudio IDE, 486

Eclipse IDE, 486

KDevelop IDE, 486

using, 6, 23–28

interest rate formula, 59, 61–62

Interface Builder, 28

interfaces, 28, 310, 388. See also base classes

internal linkage, 168, 408

INT_MAX value, 320

INT_MIN value, 320

ios::app flag, 127, 494

ios::ate flag, 494

ios::binary flag, 494

ios::in flag, 494

ios::nocreate instruction, 131

ios::noreplace instruction, 131

ios::out flag, 494

iostream file, 10, 32, 104, 131

iostream header file, 382

iostream library, 13, 296

ios::trunc flag, 494

isalnum() function, 126

isalpha() function, 126

is_open() function, 127, 460

isspace() function, 126

iterators, 437–440

K

KDevelop IDE, 486

L

length() function, 444

less than operator (<), 76

less than or equal to operator (<=), 76

libraries, 10, 13

cross-platform compatibility, 13

external, 354, 378

extra, 398

failure to include, 315

including, 10, 13

names, 433

standard, 13, 300

third-party, 378

types of, 433

limits.h header file, 320

lines

blank, 18, 19

multiple, 12

linkage, 168, 406–414

linker, 7

Linux systems, 132, 486

little endian machines, 466

loan calculation formulas, 60–63

local variables, 164–168, 276–278, 358, 412

logical operators, 76–80

long data types, 30

long double data types, 30, 31, 493

long int type, 493

loops

for, 96–98

accessing array elements with, 171, 175

do...while, 92, 95

increment/decrement operators, 91

infinite, 92, 98, 159, 472

while, 92–95, 439

M

Mac OS X systems

BBEdit, 3

case sensitivity and, 5

Cocoa technology, 27

compiling C++ from command line, 7–9

Developer Tools, 27, 28, 485

executing files, 14–15

file name restrictions, 5

g++ compiler, 28

Interface Builder, 28

Objective-C, 28

slashes and, 132

Terminal application, 7

TextEdit, 3

variable addresses, 180

Xcode. See Xcode

magic numbers, 54, 77

main() function, 2–5, 473

make utility, 393

makefiles, 393

mathematical operations. See arithmetic operations

member variables, 238–239

members. See also variables

access control, 250–251

bitwise copy of, 359

classes, 211, 270–274

inherited, 253

protecting, 250–253

referring to, 195

static, 270–274

structures and, 194–195

values, 195

membership operator (.), 289

memory. See also dynamic memory

allocating, 269, 275, 376–378

buffers, 107

characters and, 50

computer, 176–180

dynamic. See dynamic memory

fixed amount of, 339

freeing, 276, 277, 348, 371, 376–378

functions stored in, 180

pointers and, 176–177, 185, 189, 275

problems with, 341–343

requesting, 340

resetting, 341

returning for functions, 354–357

static, 340–343

variables and, 30, 177, 185

memory addresses, 176–180

memory blocks, 37, 185–190, 340–343

memory leaks

avoiding, 376–378

creating, 341

described, 376

factory functions and, 357

missing delete statements, 341

virtual methods and, 277, 280

memory scope, 377

methods

abstract, 283–287

access levels, 249–253

calling other methods, 216–217

in classes, 208, 211–216

creating, 213–215

described, 208, 212

factory, 357

getter, 274

names, 222

overloading, 258–260

overriding, 254–257, 260

returning memory, 354–357

setter, 274

static, 268–274, 280, 414

virtual, 275–282, 348

modularization, 379

modulus operator (%), 58

mortgage calculation formula, 60–63, 68–70

multidimensional arrays, 175

multiline comments type (/* */), 20, 21, 22

multiplication operations, 39, 58, 288, 292

multiplication operator (*), 39, 58, 292

N

(newline) character, 18, 19, 33, 48, 110, 494

name attribute, 308–311

namespace keyword, 398, 399, 401

namespaces, 398–405

accessing items in, 404

alias to, 401

creating, 398–403

custom, 405

debugging and, 316

declaring, 399–400

described, 12, 379

names, 400

using, 404–405

natural boundaries, 177

negative numbers, 290

nesting, 87, 98

new keyword, 275–276

new page (f) character, 494

new statement, 340–343, 347, 351, 376, 378

newline character ( ), 18, 19, 33, 48, 110, 494

newsgroups, 491

nonvolatile designation, 182

normalization, 290

normalize() method, 290, 291

not equal to operator (!=), 76, 80

not operator (!), 76, 80

Notepad, 3

NULL pointer, 343, 347

number types, 126, 493

numbers. See also arithmetic operations

absolute values, 59

decimal, 30, 31, 41–42, 493–494

exponential power, 59, 154

factorials, 92–97, 320–325

floating-point, 30, 31, 40, 41, 131

formatting, 41–43

hexadecimal, 178

infinity value, 95

integers. See integers

magic, 54, 77

negative, 290

normalization, 290, 291

overflow, 95

prime, 159

rational, 288–289

real, 31, 58, 63

real vs. integers, 58

returning, 154

rounding, 41, 45

square root of, 59

static, 54

numerator, 288, 293

numeric data types, 30–31, 493

numeric input, 108–110

numeric variables, 41–47

O

-o flag, 17

Objective-C, 28

object-oriented paradigm, 415

object-oriented programming (OOP), 267–311

<< operator, 295–300

abstract methods, 283–287

example of, 231

multiple inheritance, 301–307

object-oriented paradigm, 415

overloading operators, 288–295

polymorphism, 287

static attributes, 268–274

static methods, 268–274

virtual inheritance, 308–311

virtual methods, 275–282

vs. procedural programming, 211

objects, 207–235

allocating, 344–348, 355–357

arrays of, 225

cin, 100, 104, 105, 108–110, 122

classes. See classes

creating, 217–220, 224, 345–348

dynamic, 372–375

as exceptions, 337

factorial, 324

instances, 361

pointers to, 275–278, 344–348

relationships between, 243

returning, 355–357

static, 368–375

using, 217–220

vs. structures, 211

ofstream file, 127–131

ofstream object, 224

ofstream type, 458, 467

OOP (object-oriented programming), 267–311

<< operator, 295–300

abstract methods, 283–287

example of, 231

multiple inheritance, 301–307

object-oriented paradigm, 415

overloading operators, 288–295

polymorphism, 287

static attributes, 268–274

static methods, 268–274

virtual inheritance, 308–311

virtual methods, 275–282

vs. procedural programming, 211

open() function, 131

operating systems. See also specific operating systems

binary files, 458

cross-platform issues, 13, 132

file name restrictions, 5

numeric data types, 30, 493

slashes and, 132

operator<<() function, 295–300

operator+() method, 292

operators. See also specific operators

arithmetic, 58–63, 88–91

casting, 182

comparison, 76–80

decrement, 88–91

increment, 88–91

logical, 76–80

overloading, 288–295, 360–367

precedence, 81, 317, 492

ternary, 72–75

or operator (||), 76

ostream file, 297, 389

ostream library, 13, 296, 297

output, file, 127–131

overflow, 95

overloaded assignment operator (<<), 297–300, 362–367

overloaded items

constructors, 224

functions, 160–163, 203, 258

methods, 258–260

operators, 288–295, 360–367

overriding functions, 161

overriding methods, 254–257, 260

P

padding, 43

parameters

defining as constants, 206

formatting, 38

string, 420

vs. arguments, 148

parent classes, 238

( ) parentheses

arguments and, 139

If conditionals, 64

logical operators, 76

pointer cast, 369

pointers and, 184

precedence and, 81

type casts and, 374

parentheses ( )

arguments and, 139

If conditionals, 64

logical operators, 76

pointer cast, 369

pointers and, 184

precedence and, 81

type casts and, 374

pathnames, 385, 387

paths, 134, 231, 385, 387, 484

pause command, 17

pausing programs, 16–17, 26

period (.), 395, 455–456, 477

pipe character (|), 129

placeholders, 416, 418, 431

plain-text files, 32, 458

plus sign (+), 51, 291

pointer arithmetic, 190–191, 193

pointer cast, 369

pointer types, 181, 185, 190, 203

pointer variables, 377

pointers

addresses stored in, 183–185, 189, 193

alignment, 177

array, 190–193

assigning values to, 181

assignment operators and, 362–367

to base class, 348

changing values with, 185–189

to classes, 344–348

copy constructors and, 362–367

creating, 183, 184, 189

declaring, 181

dereferencing, 185–186, 189, 194, 200, 275

finding addresses, 176, 177–180

incrementing, 191

to integers, 183

local variables and, 358

memory and, 176–177, 185, 189, 275

multiple, 184, 189

NULL values, 343, 347

object allocation and, 344–348

to objects, 275–278, 344–348

overview, 176, 181–184

returning, 358

stack, 423, 425

to structures, 194

this, 232–235, 274

using, 183–184

values, 185, 186, 187, 188, 189

void, 189

vs. addresses, 181

vs. arrays, 349–350

vs. local variables, 276–278

working with, 176–193

polymorphism, 287

pop() method, 424, 426, 430–431

postfix increment/decrement operator, 88–90

pow() function, 59, 60, 61, 154

precedence, 81, 317, 492

precision function, 41

prefix increment/decrement operator, 88–90

preprocessor directives, 10, 13, 55, 294–397

prime numbers, 159

printing

approval message, 65–70, 73, 75

array values, 170

text, 10–13, 48

variables, 38–40, 48, 318

print() method, 290, 293, 294, 297–298

private access level, 249, 252, 253, 261

private keyword, 211

problems. See errors; troubleshooting

procedural paradigm, 415

procedural programming, 211

programmers, 326, 388

programming

described, 29

generic, 415, 416

programs. See applications

promptAnd Wait() function, 414

protected access level, 249, 252, 253, 261

protected keyword, 211

prototypes, 138, 139, 418–419, 421

public access level, 249, 252, 253, 261

public keyword, 209, 211

push_back() function, 434

push() function, 424, 427, 430–431

Q

quotation mark (”) character, 48, 494

quotation marks (“), 48, 494

quote mark (’) character, 48, 494

quote marks (‘), 48, 494

R

(carriage character), 48

(return) character, 494

Rational class, 288–300

rational numbers, 288–289

read() function, 464–466

real numbers, 31, 58, 63

recursion, 159, 360

recursive functions, 159

reference tables, 492–494

register storage class, 406

reinterpret_cast data type, 47, 184

reinterpret_cast operator, 182, 343, 372

relationships

“friends,” 261–266

to functions, 2

inheritance, 243

“is-a,” 301

linkage and, 407

between pointers/arrays, 191

rename() method, 262–265

replace() function, 450, 453

reserve() function, 457

resources, 489–494

return ( ) character, 494

return codes, 321–325

return statements

including, 5

multiple, 158

name arrays and, 358

vs. exceptions, 325

returned values, 44

returnFactorial() function, 413

rfind() function, 454

Rich Text Format (.rtf), 3

rounding numbers, 41, 45

RTF (Rich Text Format), 3

.rtf extension, 3

run-time errors, 317–319, 326, 332

S

scalar types, 190

scalar values, 169

scope

debugging and, 316

described, 164

global, 398, 401, 404

linkage and, 406–414

memory, 377

variable, 98, 164–168, 406

scope resolution operator (::), 213, 216, 289

scripts

blank lines in, 18, 19

white space in, 18–19

searches, 454–457

seekg() function, 467, 470

seekp() function, 467

semicolon (;), 10, 11, 12, 395

setColor() method, 211

setName() function, 241, 242

setSize() function, 215, 218, 222

setter methods, 274

setWeight() class, 211

setw() function, 43

short data types, 30

short int type, 493

SHRT_MAX value, 320

SHRT_MIN value, 320

signed integer types, 30

single quotation mark (’) character, 48, 494

size() function, 112, 154, 444

size() method, 53, 434, 435

sizeof operator, 91, 289, 459

sort() algorithm, 440–442

source code. See code

source files

adding white space to, 19

creating, 3–5

extensions for, 5

SourceForge, 486

spaces, 43

spacing, 18–19, 63

sqrt() function, 59

square brackets [ ], 101, 349

square root, 59

stack pointer, 423, 425

stacks

adding values to, 427

automatic variables, 406

creating, 424–427

described, 423

empty, 423, 425

removing values from, 427

size of, 425

storage classes, 406

template-based, 423

Standard C++ Library, 433, 436

Standard Template Library (STL), 415, 433

statements. See also specific statements

catch, 332

multiple, 12, 158

multiple lines, 12

on one line, 12

return. See return statements

static attributes, 268–274

static constants, 211

static functions, 414

static keyword, 343

static members, 269, 270–274

static memory, 340–344

static methods, 268–274, 280, 414

static numbers, 54

static object type casts, 368–375

static storage class, 406, 409

static type cast, 368–371

static variables, 408, 412, 414

static_cast data type, 47

static_cast operator, 182, 372

std namespace, 104, 468, 494

STL (Standard Template Library), 415, 433

stop character, 106

storage classes, 406

streams, 10–13

string data type, 51–53, 444

string functions, 444

string header file, 111, 446

string input, 111–113

string type, 198

strings, 444–457

adding to, 449–453

allotted space for, 457

assigning values to, 51

comparing, 445

concatenation, 449

C-style, 459

described, 51

escape sequences, 494

introduction to, 51–53

length of, 53

removing characters from, 457

replacing part of, 450

returning number of characters in, 154

searching through, 454–457

substring creation, 445–448

translating into number types, 126

using, 51

strncpy() function, 459

Stroustrup, Bjarne, 337, 490

strtod() function, 126

strtold() function, 126

strtol() function, 126

structure variables, 194–198

structures, 194–198

arrays of, 198

complex, 198

control, 57, 87

defining, 195–198

dereferencing, 194

described, 194

members and, 194–195

pointers to, 194

unions, 198

using, 195–198

variables and, 194, 195

vs. objects, 211

style guides, 491

subarrays, 175

subclass, 238, 244

subclass constructor, 245–248

subfolders, 4

substr() function, 446–448

substrings, 445–448

substr() method, 445

subtraction operations, 58, 292

subtraction operator (-), 58

superclass, 238, 239

swap() function, 201–205, 417–421

switch conditionals, 82–87, 98, 206

system header files, 380

T

(tab) character, 48, 494

T placeholder, 424

tab character ( ), 48, 494

tables, reference, 492–494

tellg() function, 467, 468, 470

tellp() function, 467

templates, 415–442

algorithms, 433, 440–442

basic syntax, 416–427

class, 422–427

containers, 432–439

creating, 418–420, 424–427

creating code from, 421

described, 415

Dev-C++, 25, 26

errors, 421

function, 417–421

inline, 428–431

iterators, 437–439

Standard Template Library, 433

vectors, 434–436

Terminal application, 7

ternary operator (?:), 72–75, 289

testing programs, 319

text. See also characters; input

editing, 2, 3, 23, 24, 32

printing, 10–13, 49

reading in lines of, 118–120

Rich Text Format, 3

text editors, 2, 3, 23, 24, 32

text files, 32, 458

TextEdit, 3

this pointer, 232–235, 274

throw keyword, 332

throw statement, 333–334

throwing exceptions, 332, 337

tilde (~), 226

tolower() function, 126, 454

tools, 479–487

Apple Developer Tools, 27, 28, 485

command-line tools, 27

for debugging, 316, 319, 487

Dev-C++. See Dev-C++ for Windows

overview, 3

trigonometric tools, 59

Unix tools, 486

Xcode. See Xcode

toupper() function, 126, 450, 454

translation unit, 406, 407, 413

trigonometric tools, 59

trinary operator. See ternary operator

troubleshooting. See also debugging; errors

ambiguity problems, 161, 232, 233

arrays, 175

compiler warnings, 4, 34, 46–47, 318

function calls, 372

memory issues, 341–343

true conditions, 64–70

comparison operators, 76

for loops, 96

ternary operator, 72–75

while loops, 92–95

true value, 55

try block, 332, 334, 337

.txt extension, 460

type aliases, 204, 206

type cast operator, 369–371

type casting, 44, 45–47, 180

type casts

dynamic, 372–375

static, 368–375

type conversion, 44–47

typedef keyword, 206

U

UINT_MAX value, 320

Ulong_MAX value, 320

#undef directive, 397

underflow, 95

underscore character (_), 35, 395

unions, 198

Unix systems

case sensitivity and, 5

compiling C++ from command line, 7–9

emacs editor, 3, 486

executing files, 14–15

file name restrictions, 5

slashes and, 132

source file creation, 3

Terminal application, 7

tools for, 3, 486

vi editor, 3, 486

unsigned int type, 493

unsigned integer types, 30

unsigned long int type, 493

unsigned short data types, 33

unsigned short int type, 493

user input, 111–113, 318

users, 326

USHRT_MAX value, 320

using declaration, 404, 405

using directive, 404, 405

utilities. See tools

V

validating input, 121–126, 318

validation functions, 145, 147, 320

values

absolute, 59

arguments, 149–153

assigning to array elements, 170, 175

assigning to character variables, 48–50

assigning to constants, 55

assigning to numeric variables, 36–37

assigning to pointers, 181

assigning to strings, 51

changing with pointers, 185–189

converting data types, 44–47

false, 55

functions that return, 154–159

hexadecimal, 178

members, 195

passing by address, 201–203

passing by reference, 203–206

passing to functions, 199, 201–206

pointers, 185, 186, 187, 188, 189

printing for arrays, 175

printing for variables, 39–40, 318

returned, 44, 154

returning from functions, 3, 4

scalar, 169

swapping, 201–203

true, 55

unions, 198

variables, 185, 187, 188

variable declarations, 409

variable scope, 98, 164–168, 406

variables. See also members

addresses, 178–180, 185, 200

alignment, 177

ambiguity, 232–235

assigning character values to, 48–50

assigning numeric values to, 36–37

automatic, 168, 406, 412

in classes, 210

converting data types, 44–47

creating, 194

declaring, 30–35, 37, 51

exchanging values of, 417

within functions, 164–165, 168

global, 164–168, 406, 410

initializing, 36, 37

integer, 183

local, 164–168, 276–278, 358, 406, 412

memory and, 30, 177, 185

multiple, 35, 37

names, 30, 31–32, 35, 168

non-static, 414

numeric, 41–47

printing, 38–40, 318

returned values based on, 154

spaces in, 43

static, 408, 412, 414

for string types, 51

structure, 194–198

swapping values in, 201–203

types of, 30–31

values, 185, 187, 188

vs. pointers, 276–278

vector type, 175, 434

vectors

accessing members of, 438–439

adding elements to, 435

arrays and, 175

creating, 434

iterators and, 438–439

modifying element values, 436

retrieving items from, 435

size of, 434, 436

types of, 434

vs. arrays, 198

working with, 434–436

vi editor, 3, 486

virtual destructors, 280, 348

virtual inheritance, 308–311

virtual keyword, 279

virtual methods, 275–282, 348

void pointer, 189

volatile designation, 182

W

wait application, 16–17

warnings

compiler, 4, 34, 46–47, 318

type casting and, 46

Web sites, 490–491

About.com, 491

Apple Developer Connection, 27, 485

C++ FAQ Lite, 490

C++ language, 490–491

cplusplus.com, 490

Cprogramming.com, 491

Dev-C++ Resource Site, 26

DMCInsights.com, 491

while loops, 92–95, 439

white space, 18–19, 104

Windows Explorer, 9, 26

Windows Notepad, 3

Windows systems

adding g++ to system path, 484

case sensitivity and, 5

cmd utility, 7

compiling C++ from command line, 7–9

Dev-C++. See Dev-C++ for Windows

executing files, 14–15

file name restrictions, 5

g++ compiler, 484

slashes and, 132

variable addresses, 180

WordPad, 3

WordPad, 3

write() function, 458–463

write() method, 230, 330

X

Xcode

described, 485

Gnu Debugger, 487

help with, 485

included files, 393

installing, 485

obtaining, 27

specifying command-line arguments in, 477

using, 27–2

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.194