The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Introduction to Programming with C++, 3rd Edition, By Y. Daniel Liang

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by pujzbekedjlbzuizpx, 2021-01-13 21:41:53

Introduction to Programming with C++, 3rd Edition, By Y. Daniel Liang

Introduction to Programming with C++, 3rd Edition, By Y. Daniel Liang

678 Index Monitors, 8
Monte Carlo simulation
Mathematical functions
computing angles of triangle, 120–122 estimating p, 182–183
exponent functions, 119 loop exercises, 201
geometry exercises, 148–150 Motherboards, 3–4
guessing exercises, 128–131 Multidimensional arrays. see Arrays, two-dimensional and
min, max, and abs functions, 120
overloading, 221 multidimensional
overview of, 118 Multiple inheritance, 567
rounding functions, 119–120 Multiple-choice test, 315–317
summary and exercises, 147–154 Multiplication (*) operator, 17, 43, 414
trigonometric functions, 118–119 Multiplication assignment operator (*=)

Matrices augmented assignment operators, 49
multidimensional arrays, 326–328 overloading, 533
two-dimensional arrays, 310 Multiplication table, nested for loop example, 176–178
vectors, 489 Multiplicity, object composition and, 398
Multiprocessing, 12–13
max function Multiprogramming, 12–13
testing, 210–212 Multithreading, 12–13
trigonometric functions, 120 Multi-way if-else statements, 78–79
Mutators
max_element function, 427–428 making private data fields accessible outside class, 567
maxValue function, 457–460 set function, 356–358
MB (megabyte), units of storage, 4 subscript operator as, 533
Megabyte (MB), units of storage, 4
Megahertz (MHz), measuring computer clock N

speed in, 3 Named constants, for permanent values, 39–40
Member access operator, invoking object Namespace, use in C++ program to avoid naming

functions, 349 conflicts, 14
Member functions Naming conventions

objects and, 349 for classes and objects, 349
operator overloading and, 543 consistency and, 403
Memberwize copy, copying contents between for constants, 39
identifiers for program elements, 35
objects, 349 for pointers, 415
Memory for variables, 36
Narrowing (of types), numeric type conversions, 53
RAM (random-access memory), 5 Nested if statements
storage devices, 5–7 computing BMI (Body Mass Index), 84–85
units of storage, 4–5 computing taxes, 86–88
Memory address, pointers holding, 412 overview of, 77–78
Memory leaks, 432 Nested loops, 176–178
Merge sorts, 304–305 Network interface card (NIC), 8
Mersenne prime, 257 new operator, creating persistent memory with, 429–430
Meters, converting to/from, 65, 253 NIC (network interface card), 8
MHz (megahertz), measuring computer clock speed in, 3 No-arg constructors
Mice, as input device, 8 anonymous objects and, 350
Miles, converting to/from kilometers, 194 class design guidelines, 403
min function, 120 overview of, 348
min_element function, 427–428 Nonmember functions, 542–543
Mnemonic, in machine-language instructions, 10 Nontype parameters, in template class, 467
Modem, types of communication devices, 8 Not (!) operator, 91
Modifier keys, on keyboards, 7
Modularizing code. see Code reuse
Modulus (%) operator, 43
Modulus assignment operator (%=), 49, 533
Monetary units, counting, 59–61

Not equal to (!=) operator, 377 Index 679
NULL, 415
Null terminator (\0), C-string, 292, 372 destructors, 436–439
Numbers encapsulating data fields, 356–359
exercises, 367–370
binary. see Binary numbers inline functions in classes, 355–356
complex, 556–557 naming conventions, 349
converting C-strings to/from, 296–297 overview of, 341–342
converting strings to/from, 378 passing to functions, 381–384
counting occurrences using array, 298–299 preventing multiple inclusions of header file,
decimal. see Decimal numbers
floating-point. see Floating-point numbers 354–355
hexadecimal. see Hexadecimal numbers referencing with pointers, 412
integer. see Integers returning arrays from functions, 281
loop exercises, 195–196 scope of variables and, 359–361
overview of, 658 separating class definition from class implementation,
perfect numbers, 200
prime. see Prime numbers 351–354
random. see Random numbers storing in vectors, 471
rational. see Rational numbers string type as, 134
Numerators summary, 365
accessing with subscript operator ([]), 532 this pointer and, 435–436
in rational numbers, 524–525 Octal literals, 42
return-by-reference, 533 Off-by-one errors, loops and, 158
Numeric data types, 40, 124 ofstream class
Numeric errors, 178–179 file modes and, 502
Numeric keypad, on keyboards, 7 for working with files, 492
Numeric literals, 42–43 writing to a file, 145–146, 492–494
Numeric operators OOP (object oriented programming)
applying to characters, 124 appending to strings, 373
overview of, 43–44 applying length, size, and capacity functions to strings,
Numeric type conversions, 52–54
374–375
O array of objects, 384–386
assigning strings, 373–374
Object composition, 398–399 class design guidelines, 402–404
Object file, machine code file as, 18 comparing strings, 375
Object oriented programming. see OOP (object oriented constant member functions, 390–392
constructing strings, 372
programming) converting numbers to strings, 378
Objects/object types creating class for modeling stacks, 400–402
exercises, 405–410
arrays of, 384–386 history of C++ and, 13–14
binary object I/O, 509–513 inserting/replacing strings, 376–377
class abstraction and encapsulation, 361–365 instance and static class members, 386–389
composition, 398–399 list of string operators, 377
constructing and using, 348–351 object composition, 398–399
constructors, 347–348 obtaining substrings, 375–376
converting functions to, 541–542 overview of, 342
creating and accessing dynamic objects, 433–435 passing objects to functions, 381–384
creating circle object, 344–345 replacing strings, 379–381
creating file objects, 502–503 searching in strings, 376
creating TV object, 345–347 splitting strings, 378
declaring from template class, 466 string class, 372
defining classes for, 342–343 summary, 404–405
thought process for, 392–398
transitioning from procedural programming to, 372

680 Index

Operands Output. see also I/O (input/output)
overloading and, 531 displaying formatted output on console, 140–141
storing in Stack class, 477–478 formatting with stream manipulators, 498–499
values operated on by operators, 43
Output redirection, 166
Operating systems (OSs), 12–13 Output stream, 492
Operator associativity Overflow, integer errors, 61
Overloading functions, 218–221
chart of, 656–657 Overloading operators. see Operator overloading
overloading and, 531 Overriding a function, 577
overview of, 103–104
operator keyword, 524, 530 P
Operator overloading
automatic conversion to primitive data type, 541 Page Down/ Page Up keys, on keyboards, 7
defining friend functions and friend classes, Palindromes

537–538 checking for, 188–190
defining nonmember functions for, 542–543 function exercises, 252, 257
defining operators as functions, 530–531 OOP exercises, 406
exercises, 555–558 RecursivePalindrome.cpp, 634–635
operator functions, 541–542 RecursivePalindromeUsingHelperFunction.
operators that can be overloaded, 531–532
overloading assignment operators, 551–554 cpp, 635–636
overloading augmented assignment operators, 534 selection exercises, 116
overloading pre- and post-increment and decrement tail recursion and, 645
Paragraph comment, in C++ programs, 15
operators, 535–537 Parameter list, defining functions and, 209
overloading stream extraction and insertion operators, Parameter order association, 215
Parameters
539–540 defining function templates using type parameters,
overloading subscript operator, 532–534
overloading unary operators, 535 457–458
overview of, 523–524 defining functions and, 209
Rational class for modeling rational numbers, 524–526 nontype parameter use in template class, 467
Rational class with overloaded function operators, Parent classes. see Base classes
Parentheses (()), use of special characters in C++, 16
543–551 Parity checking, multidimensional array exercises, 340
RationalClass.cpp, 527–530 Pascal programming language, 11
summary, 555 Pass-by-reference
TestRationalClass.cpp, 526–527 arguments, 233–234
Operator precedence comparing with pass-by-value, 239
chart of, 656–657 GenericMaxValuePassByReference.cpp, 458–460
dereference operator and, 420 object parameters, 394
overloading and, 531 objects to functions, 382–384
overview of, 102–103 pointer arguments in function calls, 422–423
Operators Pass-by-value
arithmetic, 43 arguments, 215–216, 230–231
augmented assignment operators, 49–50 arrays to functions, 279–280
bitwise and shift, 662 comparing with pass-by-reference, 239
defining as functions, 530–531 objects to functions, 382
evaluating operator precedence, 45–47 pointer arguments in function calls, 422–423
keyword alternatives, 653 VirtualFunctionDemoPassByValue.cpp,
precedence chart, 656–657
storing in Stack class, 477–478 578–579
string, 377 Passwords, loop exercises, 205
that can/cannot be overloaded, 531 Patterns/pattern recognition
Or (||) operator, 91, 93, 111
OSs (operating systems), 12–13 array exercises, 304
Out-of-bounds errors, array indices and, 269 function exercises, 253

loop exercises, 196 Index 681
multidimensional array exercises, 335
vector exercises, 485–486 Predecrement operator
Pentagon, determining area of, 148–149 overloading, 535–537
Perfect numbers, 200 overview of, 50
Performance issues, due to recursion, 645
Physics exercises Prediction exercises, 181–182
computing runway length for airfield, 67 prefix function, 262, 306
formula for acceleration and velocity, 66 Preincrement operator
Pivot element, partitioning list with, 305
Pixels, in screen resolution, 8 overloading, 535–537
Pointer variables. see Pointers overview of, 50
Pointer-based strings, 422 Preprocessor, 18
Pointers Preprocessor directive
arrays and, 419–422 in C++ programs, 15
constant pointers, 418–419 to compiler, 14
exercises, 449–454 Pretest loops, while loops and for loops as, 174
functions for manipulating arrays and, 427–429 Prime factors, OOP exercises, 409
overview of, 412–413 Prime numbers
passing pointer arguments in function displaying, 190–192
function exercises, 257
calls, 422–426 loop exercises, 196
returning pointers from functions, 426–427 modularizing code and, 217–218
summary, 448–449 OOP exercises, 409
TestPointer.cpp, 413–417 Primitive (fundamental) data types
this pointer, 435–436 converting functions to, 541
typedef declaration and, 417–418 overview of, 31
VirtualFunctionDemoUsingPointer.cpp, returning arrays from functions, 281
Printing arrays, 270
577–578 printStack function, adding to Stack class, 483
Pointers, file, 513–514 private keyword
Points data field encapsulation and, 356–359
visibility keywords, 580–581
finding distance between two points, 317–318 Problem solving, recursion in, 633–635
finding nearest point, 330, 484 Procedural programming, compared with OOP, 396
Polygons Programming
determining area of, 150, 305, 489 assignment statements and expressions, 37–39
OOP exercises, 408–409 augmented assignment operators, 49–50
Polymorphic types, 579 counting monetary units, 59–61
Polymorphism displaying current time, 47–49
overview of, 575–576 errors, 21–24, 61–62
PolymorphismDemo.cpp, 576 evaluating expressions and operator precedence, 45–47
Population projection exercise, 66 exponent operations, 44–45
Postdecrement operator generic, 568–569
overloading, 535–537 identifiers in, 35
overview of, 50 increment and decrement operators, 50–52
Postfix notation, 484–485 named constants in, 39–40
Postincrement operator numeric data types, 40–42
overloading, 535–537 numeric literals, 42–43
overview of, 50 numeric operators, 43–44
Posttest loops, do-while loops as, 174 numeric type conversions, 52–54
Pound sign (#), use of special characters in C++, 16 overview of, 29–30
Pounds, converting to/from kilograms, 65, 194 program defined, 2
pow(a, b) function program development cycle in C++, 18–20
ComputeLoan.cpp, 57 reading input from keyboard, 33–35
exponent operations, 44–45 software development process, 55–59
style and documentation in C++, 20–21

682 Index

Programming (continued ) exception handling exercises, 623
summary and exercises, 62–69 exercises, 555
variables in, 35–37 modeling rational numbers, 524–526
writing a simple program, 30–32 with overloaded function operators, 543–551
overloading augmented assignment operators, 533
Programming languages RationalClass.cpp, 527–530
assembly language, 9–10 TestRationalClass.cpp, 526–527
high-level languages, 10–11 Rational numbers
machine language, 9 accessing with subscript operator ([]), 532
use by software developers, 2 modeling, 524–526
read function, binary I/O, 507
Prompt, for user input, 33 Reading
Properties from a binary file, 507–508
characters from keyboard, 123
class design guidelines, 403 strings, 137
object, 342 from a text file, 146–147, 166–168, 494–495
protected keyword, 580–581 Rectangles
Prototypes. see Function prototypes computing area of, 27
Pseudocode, describing algorithms in natural derived classes, 586–587
finding point within, 110
language, 30 inheritance examples, 565–566
Pseudorandom numbers, 89 Recursion
public keyword, 345, 580–581 binary searches and, 637
Pure virtual functions, 581–582 Eight Queens puzzle, 642–644
put function, 500 exercises, 647–650
Python programming language, 11 factorials, 626–630
Fibonacci numbers, 630–632
Q vs. iteration, 645
overview of, 625–626
Quadratic equations, solving, 106 problem solving, 633–635
Quincunx (bean machine), array exercises, 301–302 recursive helper functions, 635–636
Quotation marks (."") recursive selection sort, 636–637
summary, 647
missing quotation marks causing error, 23 tail recursion, 645–646
use of special characters in C++, 16 Towers of Hanoi problem and, 638–642
Recursive call, 626
R Recursive functions
characteristics of, 633
RAM (random-access memory), 5 helper functions, 635–636
rand() function, 89–91, 126–128 overview of, 626
Random access files Recursive selection sort, 636–637
Reference variables
overview of, 513–514 example using, 233
RandomAccessFile.cpp, 514–516 increment function used with, 234
Random characters, generating, 126–128 overview of, 232
Random numbers swap function used with, 234–239
case study applying to lottery, 95–97 reinterpret_cast, 507
generating, 89–91 Relational operators
in Monte Carlo simulation, 182 comparing characters, 125–126
Random point, determining random coordinate, 116 comparing pointers to, 421
Random shuffling. see Shuffling comparing strings, 136–138
Random values, initializing arrays with, 270 list of, 72
random_shuffle function, 427–428 string operators and, 377
Random-access memory (RAM), 5
Rational class
converting Rational object to object data type, 541
converting Rational object to primitive data type, 541
defining nonmember functions for overloading operators,

542–543

Relative file names, 492 Index 683
Remainder (%) operator, 43
replace function Seek base, moving file pointers in random access
files, 514
ReplaceString.cpp, 379–381
replacing strings, 376–377 seekg() function, 513–514
Requirements specification, in software development life seekp() function, 513–514
Selection sorts
cycle, 55–56
Reserved word (keywords). see also by individual type array exercises, 301
Reserved word (Keywords) generic, 460–462
recursive, 636–637
in C++, 653 sorting arrays, 290–292
overview of, 15 Selections
Rethrowing exceptions, 618–620 applying random numbers to lottery, 95–97
return keyword bool data type and, 72–73
value-returning functions and, 209 common errors in selection statements, 79–84
void functions and, 214 computing BMI (Body Mass Index), 84–85
Return-by-reference, function returns aliases to computing taxes, 86–88
conditional expressions, 101–102
variables, 533 debugging and, 104
Reusable code. see Code reuse determining leap year, 94–95
reverse function generating random numbers, 89–91
if statements, 73–76
returning arrays from functions, 282–283 logical operators and, 91–94
returning pointers from functions, 426–427, 429, 431 nested if and multi-way if-else statements, 77–79
right manipulators, stream manipulators, 143–144 operator precedence and associativity and, 102–104
Rounding errors, 62 overview of, 72
Rounding functions, 119–120 selection statements, 72
Rule of three, 554 switch statements, 97–101
Runtime errors two-way if-else statements, 76–77
QuotientThrowRuntimeError.cpp, 604 selectionSort function, 462
runtime_error class, 603 semicolon (;). see ; (semicolon)
stack overflow due to recursion, 645 Sentinel-controlled loops, 164–165
types of programming errors, 22 Separation of responsibility, class design guidelines, 402
Rvalue (right value), assignment operators and, 533 Sequential access files, 514
Service functions, 120
S set function, 356–358
setprecision(n) manipulator, 141
Science exercises set(width) manipulator, 143
programming exercises, 66, 68 Shallow copy
selection exercises, 109, 115 assignment operators and, 551
copy constructors and, 443
Scientific notation, of floating-point literals, 43 Shift operators, 662
Scissor, rock, paper game short type, 40
Short-circuit operator, 93
loop exercises, 200 showpoint manipulator, 142–143
selection exercises, 108–109 Shuffling
Scope of variables multidimensional arrays, 329
initializing variables and, 37 simple arrays, 271
local and global variables, 225–227 two-dimensional arrays, 313
for loop and, 227–228 vectors, 485–486
objects and, 359–361 Signed integers, vs. unsigned, 41
overview of, 225 Simulation
Screen resolution, 8 array exercises, 303
Searching arrays vector exercises, 483
binary searches, 287–290 size() function, applying to strings, 374–375
linear searches, 286–287
overview of, 286
Searching strings, 376

684 Index if statements. see if statements
if-else statements. see if-else statements
sizeof function statement terminator, 15
data type size and, 41 switch statements. see switch statements
object size and, 349 throw statements. see throw statements
Static binding, 578
size_t type, 293 Static functions
Slope, determining slope of a line, 69 accessing without creating objects, 389
Software, computer components, 2 class design guidelines, 403–404
Software development life cycle class membership and, 386–387
vs. instance functions, 389
deployment, 56 static keyword, 228
implementation, 55, 57–58 Static members
maintenance, 56 class membership, 386–389
overview of, 55 in template class, 468
requirements specification, 55–56 Static variables
in software development life cycle, 58–59 class membership and, 386
system analysis, 55–56 declaring, 387
system design, 55–57 local variables, 228–230, 359
testing, 56 static_cast operator, 590–591
sort function, 427–428, 462 Statistics
Sorts array exercises, 300, 303
bubble-sort algorithm, 301 loop exercises, 202
characters in string, 406, 454 Stepwise refinement
generic sort, 460–462 benefits of, 249
merge sorts, 304–305 PrintCalendar.cpp example, 246–249
multidimensional array exercises, 330, 337 in problem solving, 242
recursive selection sort, 636–637 top-down design, 242–244
selection sorts, 290–292 top-down or bottom-up implementation, 244–245
Source code, in high-level languages, 10 Stopping conditions, in recursion, 626
Source files, .cpp extension for, 18 Storage devices
Source program, in high-level languages, 10 CDs/DVDs, 6
Spacing hard disks, 6
in C++ programs, 15 overview of, 5–6
programming style and, 20 USB flash drives, 6–7
Special characters, in C++ programs, 16 strcat/strncat function, concatenating C-strings, 295
Stack class strcmp function, comparing C-strings, 295–296
developing generic class with class templates, 462–464 strcopy/strncopy function, copying C-strings,
dynamic version, 469–471
EvaluateExpression.cpp, 479–482 293–294
evaluating expressions with, 477–478 Stream extraction operator (>>)
exception handling exercises, 623
generic version, 464–465 in ComputeAreaWithConsoleInput.cpp, 32
inheritance exercises, 596 implementing as friend nonmember functions, 537
template exercises, 483 overloading, 539–540
TestGenericStack.cpp, 465–466 reading strings that include whitespace, 499
TestGenericStackWithTemplateFunction.cpp, string operators, 377
Stream insertion operator (<<)
467–468 in C++ programs, 15
Stack overflow, runtime errors, 645 implementing as friend nonmember functions, 537
Stacks overloading, 539–540
string operators, 377
class for modeling, 400–402 Stream manipulators
invoking functions and, 211–212 displaying formatted output on console, 140–141
operator overloading exercises, 556 fixed manipulator, 142
State, of an object, 342
Statements
assignment statements, 37–38
in high-level languages, 10

Index 685

formatting file output with, 498–499 Subclasses. see Derived classes
left and right manipulators, 143–144 Subscript operator ([])
setprecision(n) manipulator, 141
set(width) manipulator, 143 accessing characters in strings, 135–136, 372
showpoint manipulator, 142–143 operator overloading exercises, 556
Stream states, 504–505 overloading, 532–534
Streams string operators, 377
file. see fstream class uses of, 524
input. see ifstream class substr function, obtaining substrings, 375–376
output. see ofstream class Substrings, obtaining, 375–376
testing stream states, 504–505 Subtraction (-) operator, 43, 535
Strictly identical arrays, 303–304, 338 Subtraction, vector exercises, 490
string class Subtraction assignment operator (-=) 49, 533
functions for comparing strings, 375 Subtypes, 575–576
functions for finding substrings, 376 Sudoku
functions for inserting or replacing strings, 376–377 multidimensional array exercises, 336
implementing, 454 two-dimensional array examples, 319–322
overview of, 372 Sums
String index, 135–136 loop exercises, 198
Strings. see also C-strings total variable and, 270, 312
appending to, 373 Superclasses. see Base classes
applying length, size, and capacity functions to, 374–375 Supertypes, 575–576
applying recursion to determine permutations of, 626 swap function
applying to lottery program, 138–140 pass-by-value and, 231–232
assigning, 373–374 template exercises, 483
checking substrings, 262 used with reference variable, 234–239
comparing, 136–138, 375 swapCase function, array exercises, 307
concatenating, 136 switch statements
constructing, 372 ChineseZodiac.cpp, 99–101
converting floating-point numbers to, 307 overview of, 97–98
converting numbers to, 378 problem solving using recursion, 633
C-strings compared with, 292 rules, 99
exercises, 152–153 syntax, 98
function exercises, 258 Synonymous types, defining, 417–418
inserting, 376–377 Syntax
list of string operators, 377 of class templates, 465
obtaining substrings, 375–376 of functions, 209
OOP exercises, 405–407 rules in C++ programs, 16
operator overloading exercises, 556 of switch statements, 98
overview of, 134–135 of two-dimensional arrays, 310
pointer-based, 422 Syntax error, 21
reading, 137 System analysis, in software development life cycle, 55–56
replacing, 379–381 System design, in software development life cycle, 55–57
reversing, 202
searching in, 376 T
sorting characters in, 454
splitting, 378 Tables, data representation with two-dimensional
string class, 372 array, 310
string indexes and subscript operator and, 135–136
using loops to check for palindromes, 188–190 Tail recursive function, 645–646
stringstream class, 378 Tax computation, selection exercises, 86–88, 108
strlen function, finding length of C-string, 293–294 TB (terabyte), units of storage, 4
Stub functions, 244 Teamwork, facilitation of, 249
tellg() function, 514
tellp() function, 514

686 Index

Temperature Top-down implementation, 244–249
multidimensional array example, 323–324 total variable, summing arrays, 270, 312
selection exercises, 107 Towers of Hanoi problem, applying recursion to, 638–642
Tracing programs, 32
Template class, 462–464 Transistors, CPUs built on, 3
Template function, 467 Triangles
template keyword, 457
Template prefix, 458, 465 computing angles of, 120–122
Templates computing area of, 68
computing perimeter of, 109
for dynamic Stack class, 469–471 finding point within, 111
exercises, 483–490 inheritance and, 595–596
for generic sort, 460–462 TriangleException class, 608–612
for generic Stack class, 464–468 Trigonometric functions, 118–119, 194–195
GenericMaxValue.cpp, 457–458 True/false values. see bool data type
GenericMaxValuePassByReference.cpp, 458–460 Truth tables, for Boolean operators, 91
overview of, 456–457 try-catch blocks
summary, 482 BadAllocExceptionDemo.cpp, 605
Template class, 462–464 BadCastExceptionDemo.cpp, 605–606
Terabyte (TB), units of storage, 4 catching exceptions, 598
Ternary operator, as conditional operator, 102 exception classes and, 603
Testing exception propagation, 617–618
benefits of stepwise refinement, 249 exception specification and, 620–621
in software development life cycle, 56, 58–59 InvalidArgumentExceptionDemo.cpp, 606–607
Text files multiple catches, 612–617
comparing text files with binary files, 506 QuotientThrowRuntimeError.cpp, 604
CopyFile.cpp, 500–502 QuotientWithException.cpp, 599–600
creating file objects using fstream class, 502–503 QuotientWithFunction.cpp, 602
formatting output with stream manipulators, 498–499 when to use exceptions, 621
get and put functions, 500 Type parameters
getline function, 499–500 for class templates, 465
letting user enter file names, 497–498 defining function templates, 457–458
overview of, 492 typedef declaration, 417–418
reading from, 494–495 typedef keyword, 417–418
testing end of file, 495–497
testing file existence, 495 U
testing stream states, 504–505
writing to, 492–494 UML (Unified Modeling Language)
Thinking recursively, 633–634 class diagrams, 343
this keyword, 435 creating UML diagram for class, 409
this pointer, 435–436 diagram of Rational class, 525
Throw lists, exception handling and, 620–621
throw statements Unary operators
BadAllocExceptionDemo.cpp, 605 addition and subtraction operators, 44
InvalidArgumentExceptionDemo.cpp, 606–607 overloading, 535
QuotientThrowRuntimeError.cpp, 604
QuotientWithException.cpp, 600 Underflow error, floating-point numbers, 62
QuotientWithFunction.cpp, 602 Unified Modeling Language. see UML (Unified Modeling
rethrowing exceptions, 618–620
throwing exceptions, 598 Language)
TicTacToe game, multidimensional array exercises, 334 Universal serial bus (USB), 6
Tilde (~) use with destructors, 436 UNIX epoch, 47
time(0) function, 47–49 Unsigned integers
Top-down design, 242–244
vs. signed, 41
unsigned type, 417
Upcasting, 591

Updating files, 516–517 Index 687
Upper case characters
Velocity exercises
converting to lower case, 124–125 computing runway length for airfield, 67
counting, 202 relationship to acceleration, 66
USB (universal serial bus), 6
USB flash drives, 6–7 Virtual functions
Users dynamic casting and, 591
controlling loops via user confirmation, 164 overriding a function, 577
letting user enter file names, 497–498 overview of, 576–577
pure virtual functions, 581–582
V VirtualFunctionDemoPassByValue.cpp,
578–579
Value-returning functions VirtualFunctionDemoUsingPointer.cpp,
invoking, 210 577–578
overview of, 209
void functions compared with, 212–215 virtual keyword, 578
Visibility keywords, 580
Values, binary searches based on list of, 287–290 Visual Basic programming language, 11
Variables Void functions, 212–215
Vowel counting exercise, 202
address operator (&) and, 412
Boolean, 72 W
classes using, 342
declaring, 31–32 while loops
displaying/modifying in debugging, 104 applying to guessing numbers, 159–161
initializing, 36–37 applying to subtraction quiz, 158–159, 162–164
local and global, 225–227 controlling with sentinel values, 164–165
pointer. see Pointers controlling with user confirmation, 164
referencing, 412 converting for loop to, 174
scope of, 37, 227–228 converting to do-while loop, 170
static local variables, 228–230 deciding which type of loop to use, 174–176
for storing changeable values, 35–36 design strategies, 162
undeclared, uninitialized, and unused, 61 do-while variation, 168–170
using with data types, 36 input/output redirection, 166
vector class overview of, 156–158
DeckOfCardsUsingVector.cpp, 474–476 reading all data from a file, 166–168
exercises, 483–490
overview of, 471–472 Whitespace characters, 123, 499
replacing arrays with, 474 Widening (of types), 53
summary, 482 Wind-chill, in computation of temperature, 68, 109
TestVector.cpp, 472–474 write function, binary I/O, 506–507
TwoDArraysUsingVector.cpp, 476–477 Writing

to binary file, 506–507
to a file, 145–146, 492–494

This page intentionally left blank

CREDITS

Cover © Tetra Images/Glow Images
Figure 1.1a © Studio 37/Shutterstock
Figure 1.1b © Arno van Dulmen/Shutterstock
Figure 1.1ci © Peter Gudella/Shutterstock
Figure 1.1cii © Vasilius/Shutterstock
Figure 1.1ciii © Nata-Lia/Shutterstock
Figure 1.1di © Dmitry Rukhlenko/Shutterstock
Figure 1.1dii © Andrey Khrobostov/Shutterstock
Figure 1.1diii © George Dolgikh/Shutterstock
Figure 1.1ei © Nikola Spasenoski/Shutterstock
Figure 1.1eii © restyler/shutterstock
Figure 1.1fi © prism68/Shutterstock
Figure 1.1fii © moritorus/Shutterstock
Figure 1.1fiii © tuanyick/Shutterstock
Figure 1.2 © Xavier P/Shutterstock
Figure 1.4 © Peter Gudella/Shutterstock
Figure 1.5a © Vasilius/Shutterstock
Figure 1.5b © xj/Shutterstock
Figure 1.6 © Dmitry Rukhlenko/Shutterstock
Figure 1.7a © Madlen/Shutterstock
Figure 1.7b © Dmitry Melnikov/Shutterstock
Figure 1.7c © moritorus/Shutterstock
Figure 9.5 © Microsoft Visual C++ screenshot © 2012 by Microsoft Corporation. Reprinted with permission.
Figures 1.8–1.10, 4.1, 18.1, 18.2, 18.4, 18.5, 19.3–19.18 INTRODUCTION TO JAVA PROGRAMMING by Daniel Liang,
9th Ed. copyright © 2013 by Pearson Education, Inc. Reprinted and Electronically reproduced by permission of Pearson
Education, Inc., Upper Saddle River, New Jersey. Copyright © 2012 by Microsoft Corporation. Used with permission from
Microsoft. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE
SUITABILITY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUB-
LISHED AS PART OF THE SERVICES FOR ANY PURPOSE. ALL SUCH DOCUMENTS AND RELATED GRAPH-
ICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE
SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMA-
TION, INCLUDING ALL WARRANTIES AND CONDITIONS OF MERCHANTABILITY, WHETHER EXPRESS,
IMPLIED OR STATUTORY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN
NO EVENT SHALL MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS BE LIABLE FOR ANY SPECIAL, INDI-
RECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF INFORMATION
AVAILABLE FROM THE SERVICES. THE DOCUMENTS AND RELATED GRAPHICS CONTAINED HEREIN
COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODI-
CALLY ADDED TO THE INFORMATION HEREIN. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS MAY
MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE PROGRAM(S) DESCRIBED
HEREIN AT ANY TIME. PARTIAL SCREEN SHOTS MAY BE VIEWED IN FULL WITHIN THE SOFTWARE VER-
SION SPECIFIED.

689

C++ Quick Reference

if Statements switch Statements Array/Initializer

if (condition) switch (intExpression) int list[10];
{ { int list[] = {1, 2, 3, 4};

statements; case value1: Multidimensional Array/Initializer
} statements;
break; int list[10][15];
if (condition) int list[2][2] = {{1, 2}, {3, 4}};
{ ...
case valuen: Dynamic Memory Creation/Deletion
statements; statements;
} break; int* p1 = new int;
else default: int* p2 = new int[10];
{ statements; delete p1;
delete [] p2;
statements; }
}
Frequently Used functions
if (condition1)
{ time(0) returns current time
srand(seed)
statements; rand() sets a new seed for generating random numbers
} pow(a, b)
else if (condition2) returns a random integer
{ returns ab

statements; Character Functions
}
else isdigit(c) returns true if c is a digit.
{ isalpha(c) returns true if c is a letter.
isalnum(c) returns true if c is a letter or a digit.
statements; islower(c) returns true if c is a lowercase letter.
} isupper(c) returns true if c is an uppercase letter.
tolower(c) returns a lowercase for c.
Loop Statements toupper(c) returns an uppercase for c.

while (condition) C-String Functions
{
strlen returns string length
statements; strcpy copies a string
} strcat concatenate two strings
strcmp compares two strings
do atol converts a string to a long value
{ itoa converts a an integer a string

statements; The string Class Member Functions
} while (condition);
append appends new contents to the string
for (init; condition; insert inserts new contents to the string
adjustment) at retrieves character from the string
[] string subscript operator
{ length returns the length of the string.
statements; substr returns a substring from the string

}

Companion Website: www.cs.armstrong.edu/liang/cpp3e


Click to View FlipBook Version