+ All Categories
Home > Documents > Problem Solving and Programming - GBV

Problem Solving and Programming - GBV

Date post: 04-Apr-2022
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
7
c++ Problem Solving and Programming KENNETH A. BARCLAY Department of Computer Studies Napier University, Edinburgh and BRIAN J. GORDON Information and Statistics Division Scottish Health Services, Edinburgh Prentice Hall New York London Toronto Sydney Tokyo Singapore
Transcript
Page 1: Problem Solving and Programming - GBV

c++ Problem Solving and Programming

KENNETH A. BARCLAY Department of Computer Studies

Napier University, Edinburgh

and

BRIAN J. GORDON Information and Statistics Division Scottish Health Services, Edinburgh

Prentice Hall New York London Toronto Sydney Tokyo Singapore

Page 2: Problem Solving and Programming - GBV

Preface xi

PART I 1

1 Programming Paradigms 3 1.1 Procedural abstraction 3 1.2 Data abstraction 4 1.3 Object orientation 4

2 Essentials of a C++ Program 7 2.1 Afirst program 7 2.2 Compiling and running 12 2.3 Summary 13 2.4 Exercises 13

3 Types, Operators and Expressions 15 3.1 Integer constants ' 15 3.2 Floating point constants 17 3.3 Character constants 18 3.4 String constants 19 3.5 Identifiers 21 3.6 Variable definitions 22 3.7 Qualifiers 24 3.8 Arithmetic expressions 26 3.9 Type conversions 28 3.10 The assignment Operator 32 3.11 The Compound assignment Operators 34 3.12 The increment and decrement Operators 35 3.13 The type cast Operator 36 3.14 The comma Operator 37 3.15 Derived types 38 3.16 Reference types 39 3.17 Pointer types 40

Page 3: Problem Solving and Programming - GBV

vi Contents

3.18 Enumeration types 43 3.19 The typedef Statement 44 3.20 Summary 46 3.21 Exercises 47

4 Simple Input and Output 49 4.1 Access to the Standard C++library 49 4.2 Unformatted output 50 4.3 Unformatted input 50 4.4 Output manipulators 51 4.5 The functions get and put. 52 4.6 Summary 52 4.7 Exercises 53

5 Program Structure 54 5.1 The structure of a function 54 5.2 Multi-function programs 59 5.3 Automatic variables 64 5.4 Function values 66 5.5 Function arguments 68 5.6 Function argument agreement and conversion 73 5.7 Reference arguments 74 5.8 Pointers and function arguments 80 5.9 Default arguments 83 5.10 Inline functions 84 5.11 Overloaded function names 85 5.12 Template functions 87 5.13 Mathematical functions 88 5.14 Summary 89 5.15 Exercises 90

6 FlowofControl 95 6.1 Relational Operators and expressions 95 6.2 Equality Operators and expressions 97 6.3 Logical Operators and expressions 98 6.4 The conditional Operator 100 6.5 The while Statement 101 6.6 The for Statement 110 6.7 The do Statement 114

CASE STUDY 6.1 Reports 116 6.8 The if Statement 119

CASE STUDY 6.2 Bank Statement 130 6.9 The switch Statement 134

CASE STUDY 6.3 Printing bank cheques 138

Page 4: Problem Solving and Programming - GBV

Contents vn

Program Files 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10

Types, storage class and scope Local duration Static duration Storage class static The C++ pre-processor Macro definitions File inclusion Conditional compilation Summary Exercises

6.10 The break Statement 142 6.11 The continue Statement 145 6.12 Summary 146 6.13 Exercises 147

152 152 153 156 164 166 167 168 169 173 175

Arrays and Pointers 178 8.1 Defining and referencing arrays 178 8.2 Multi-dimensional arrays 182 8.3 Arrays as function arguments 184 8.4 Array initialization 188 8.5 Pointers and arrays 189 8.6 Functions returning pointers 193 8.7 External array referencing 194 8.8 Character strings 194 8.9 Character string input/output 196 8.10 Arrays of pointers and pointers to pointers 199 8.11 Command line arguments 202 8.12 Initializing pointer arrays 203 8.13 Caveat 204 8.14 Summary 204 8.15 Exercises 205

PART II 209

9 Classes 211 9.1 Class types 211 9.2 Calling other member functions 221 9.3 Class constructors and class initialization 222 9.4 Arrays and class objects 234 9.5 Destructors 239 9.6 Class scoping rules 242

Page 5: Problem Solving and Programming - GBV

viii Contents

9.7 9.8

9.9 9.10 9.11 9.12 9.13 9.14

Constant member functions Class member objects CASE STUDY 9.1 Geometry CASE STUDY 9.2 Patient statistics Static class members Pointers and class objects The implicit pointer this Class templates Summary Exercises

10 Operator Overloading 10.1 10.2 10.3 10.4 10.5 10.6 10.7

10.8 10.9 10.10

Operator functions X& X : : o p e r a t o r = ( c o n s t X&) Friend functions Constructors as conversion Operators Conversion Operators Overloading the input/output Operators Friend classes CASE STUDY 10.1 Rational numbers Commentary Summary Exercises

11 Inheritance 11.1 11.2 11.3 11.4 11.5 11.6

11.7 11.8 11.9

Class derivation Initialization and assignment under derivation Private base classes Scope rules and access control under derivation Polymorphism and dynamic binding Abstract base classes CASE STUDY 11.1 Number classes CASE STUDY 11.2 Patient statistics (revisited) Assessment Summary Exercises

12 Storage Management 12.1

12.2

12.3

Implementation CASE STUDY 12.1 Dynamic vectors Containers CASE STUDY 12.2 Patient statistics (yet again) Sets, bags and iterators

243 244 248 252 256 258 260 261 265 266

271 271 276 277 280 284 286 289 291 296 296 297

298 298 310 312 316 319 330 333 341 344 345 345

349 350 355 363 376 379

Page 6: Problem Solving and Programming - GBV

Contents ix

12.4 Dictionaries and Relations 391 CASE STUDY 12.3 Class hierarchies 401

12.5 Representation 403 12.6 Summary 405 12.7 Exercises 406

13 Dynamic Data Structures 410 13.1 The linear linked list 412 13.2 List processing 417 13.3 List representation 420 13.4 Summary 428 13.5 Exercises 428

14 More on Functions 430 14.1 Recursive functions 430 14.2 Pointers to functions 434 14.3 Functions as arguments 437 14.4 Functions with variable number of arguments 438

CASE STUDY 14.1 Quicksort 440 CASE STUDY 14.2 Higher order functions 444

14.5 Summary 446 14.6 Exercises 447

15 Stream Input/Output 450 15.1 File access 452 15.2 The functions get and put 455 15.3 Manipulators 458 15.4 String stream processing 460 15.5 The functions ge t l ine , read and write 461 15.6 ein, cout and cerr 465 15.7 Direct access 465 15.8 Commentary 468 15.9 Summary 469 15.10 Exercises 469

16 Structures, Unions and Bit Fields 471 16.1 Unions 471 16.2 Bit fields 476 16.3 Summary 479 16.4 Exercises 479

17 Operations on Bits 481 17.1 Bitwise Operators and expressions 481

CASE STUDY 17.1 Bitvectors 485

Page 7: Problem Solving and Programming - GBV

Contents

17.2 17.3

Summary Exercises

Further Language Constructs 18.1 18.2 18.3

18.4 18.5 18.6 18.7 18.8

Multiple inheritance Virtual base classes Multiple inheritance and Virtual functions CASE STUDY 18.1 Sales team Exception handling Linkage specification The goto Statement Summary Exercises

490 490

18 Further Language Constructs 491 491 497 498 500 504 508 509 512 512

APPENDICES 515

A Hardware Characteristics 517

B ASCII Character Set 520

C Reserved Keywords 524

D Identifiers 525

E Operators 526

F C++ Library Functions 528

F.l Character functions 528 F.2 String processing 531 F.3 General Utilities 534 F.4 C++I/O stream library 538 F.5 Mathematical functions 545 F.6 Variable argument lists 547

Bibliography 548

Index 549


Recommended