8
DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN C++ Keith E.Gorier«, National Institutes of Health Maryland, USA Sanford M. Orlow, Systex, Inc. Maryland, USA Perry S. Plexico, National Institutes of Health Maryland, USA B. G. TEUBNER Stuttgart JOHN WILEY & SONS Chichester New York • Brisbane • Toronto • Singapore

DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN C++ Keith E.Gorier«, National Institutes of Health Maryland, USA

Sanford M. Orlow, Systex, Inc. Maryland, USA

Perry S. Plexico, National Institutes of Health Maryland, USA

B. G. TEUBNER Stuttgart

JOHN WILEY & SONS Chichester • New York • Brisbane • Toronto • Singapore

Page 2: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

CONTENTS

List of Figures xiii

List of Tables xv

List of Examples xvii

Preface xix

1 Introduction 1 1.1 DATA ABSTRACTION 1 1.2 OBJECT-ORIENTED PROGRAMMING 2 1.3 THE C++LANGUAGE 3

1.3.1 How C++ differs from С 3 1.3.2 C++ and object-oriented programming 4 1.3.3 Ways of using C++ 5

1.4 THE NIH CLASS LIBRARY 6 1.5 EXAMPLE PROGRAMS 6

1.5.1 Source code distribution kit 7 1.5.2 Programming environment 7 1.5.3 Programming conventions 7

1 PROGRAMMING WITH ABSTRACT DATA TYPES IN C++ 9

2 Data Abstraction in C++ 11 2.1 INTRODUCTION 11 2.2 AN EXAMPLE OF PROCEDURAL PROGRAMMING IN С 12

2.2.1 Example problem 12 2.2.2 A solution using С and a procedure library 12

2.3 AN EXAMPLE OF DATA ABSTRACTION IN C++ 14 2.3.1 Specifications and implementations 16 2.3.2 Encapsulation 16

2.4 OVERVIEW OF C++ SUPPORT FOR DATA ABSTRACTION 17 2.5 CHAPTER SUMMARY 18

3 An Example Specification 19 3.1 INTRODUCTION 19 3.2 SPECIFICATION OF CLASS B i g l n t 19

Page 3: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

3.2.1 Classes 20 3.2.2 Encapsulation 20 3.2.3 Member functions 21 3.2.4 Function argument type checking 22 3.2.5 Function name overloading 23 3.2.6 Default function arguments 24 3.2.7 Calling member functions 24 3.2.8 Constructors 25 3.2.9 Default constructors 26 3.2.10 Constructors with more than one argument 26 3.2.11 Constructors for static class instances 26 3.2.12 Constructors and type conversion 27 3.2.13 Constructors and initialization 28 3.2.14 Operator overloading 29 3.2.15 Destructors 30

3.3 CHAPTER SUMMARY 31

An Example Implementation 33 4.1 INTRODUCTION 33 4.2 IMPLEMENTATION OF CLASS Вiglnt 33

4.2.1 The B i g l n t ( c o n s t c h a r * ) constructor 33 4.2.2 The scope resolution operator 34 4.2.3 Constant types 35 4.2.4 Constant member functions 36 4.2.5 Casting constants 37 4.2.6 Implicit member variable and function references 37 4.2.7 The new operator 38 4.2.8 Declarations in blocks 38 4.2.9 The B i g l n t (uns igned) constructor 39 4.2.10 The B i g l n t copy constructor 39 4.2.11 Reference types 39 4.2.12 The B i g l n t addition operator 41 4.2.13 The B i g l n t ( c h a r * , u n s i g n e d ) constructor 42 4.2.14 Class D i g i t S t r e a m 43 4.2.15 Class-based as opposed to object-based encapsulation 44 4.2.16 Friend functions 45 4.2.17 The keyword t h i s 45 4.2.18 The semantics of r e t u r n 46 4.2.19 The B i g l n t assignment operator 46 4.2.20 The member function B i g l n t : : p r i n t () 47 4.2.21 The B i g l n t destructor 47 4.2.22 Inline functions 48

4.3 CHAPTER SUMMARY 50

Applications for Abstract Data Types 51 5.1 INTRODUCTION 51 5.2 STREAM I/O 51

5.2.1 The stream package header file 52 5.2.2 Writing to the standard output stream 52 5.2.3 Reading from the standard input stream 54 5.2.4 Extending stream output operations to a new C++ class 54 5.2.5 Extending stream input operations to a new C++class 55 5.2.6 Associating an I/O stream with an open file 58 5.2.7 Summary for I/O streams 59

5.3 DYNAMIC CHARACTER STRINGS 59 5.3.1 A simple string application 60 5.3.2 S t r i n g operators 61

Page 4: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

CONTENTS vii

5.3.3 A S t r i n g abstract data type 62 5.3.4 Substrings 64 5.3.5 Private classes 65 5.3.6 Forward references to class declarations 66 5.3.7 Assignment to references 67 5.3.8 Overloading constant member functions 67 5.3.9 Casting away const-ness 68 5.3.10 Mixing S t r i n g s and S u b s t r i n g s with С strings 69 5.3.11 Implicit type conversion 69 5.3.12 ADT design issues 69 5.3.13 Summary for class S t r i n g 71

5.4 ABSTRACT DATA TYPES FOR DATES AND TIMES 72 5.4.1 An abstract data type for dates 72 5.4.2 A simple date application 74 5.4.3 An abstract data type for times 76 5.4.4 Summary for classes Date and Time 77

5.5 REGULAR EXPRESSIONS 77 5.5.1 Functionality of class Regex 77 5.5.2 Implementation of class Regex 79

5.6 NUMERIC DATA TYPES 81 5.6.1 Extensions of fundamental arithmetic 82 5.6.2 Complex numbers 82 5.6.3 Vectors 83 5.6.4 Matrices 88 5.6.5 The automatic derivative as an ADT 90 5.6.6 A practical application for automatic derivatives . . 93

5.7 CHAPTER SUMMARY 97

П OBJECT-ORIENTED PROGRAMMING IN C++ 99

6 Object-oriented Programming Concepts 101 6.1 INTRODUCTION 101

6.1.1 Derived classes 102 6.1.2 Virtual member functions . . . 102 6.1.3 The s w i t c h statement considered harmful 104

6.2 GEOMETRY EXAMPLE 104 6.2.1 Class P o i n t 104 6.2.2 Class L ine and class C i r c l e 105 6.2.3 Class instances as member variables 106 6.2.4 Class T r a n s f o r m S t a c k 107 6.2.5 The member function move () 108 6.2.6 The member function draw() 108 6.2.7 Class Shape 109 6.2.8 Class P i c t u r e 110 6.2.9 Type compatibility 112 6.2.10 Pure virtual functions 113 6.2.11 Subpictures 114

6.3 IMPROVED GEOMETRY EXAMPLE 114 6.3.1 Object initialization 116 6.3.2 Object finalization 119 6.3.3 Calling virtual functions from a base class constructor 119

6.4 OBJECT-ORIENTED PROGRAMMING TERMINOLOGY 120 6.4.1 Single and multiple inheritance 120 6.4.2 Abstract classes 120 6.4.3 Protected members 121

Page 5: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

viii CONTENTS

6.4.4 Polymorphism 122 6.5 CHAPTER SUMMARY 122

7 An Introduction to the NIH Class Library 123 7.1 INTRODUCTION 123 7.2 GEOMETRY EXAMPLE USING NIH LIBRARY CLASSES 123

7.2.1 NIH Library class P o i n t 124 7.2.2 NIH Library class S t ack 124 7.2.3 NIH Library class O r d e r e d C l t n 125 7.2.4 Deriving a class from class O b j e c t 125 7.2.5 Implementation of class P i c t u r e using class O r d e r e d C l t n 126

7.3 COPYING OBJECTS 127 7.3.1 Implementation of sha l lowCopy () 131 7.3.2 Implementation of deepCopy () 132 7.3.3 Summary of object copying 134

7.4 OBJECT I/O 134 7.4.1 Object output 134 7.4.2 Class OlOout 137 7.4.3 Object input 138 7.4.4 Class OlOin 140 7.4.5 Static member functions 140 7.4.6 Object input and polymorphism 140 7.4.7 Object I/O summary 141

7.5 CHAPTER SUMMARY 141

8 Programming with the NIH Container Classes 143 8.1 INTRODUCTION 143

8.1.1 Class O b j e c t 143 8.1.2 Identifying and testing an object's class 144 8.1.3 Comparing objects 146 8.1.4 Printing objects 149

8.2 CLASS P a t i e n t 149 8.3 THE NIH CLASS LIBRARY CONTAINER CLASSES 151

8.3.1 Class C o l l e c t i o n . . . 152 8.3.2 Class I t e r a t o r 152 8.3.3 Class S e q C l t n 156 8.3.4 Class O r d e r e d C l t n 157 8.3.5 Class S o r t e d C l t n 160 8.3.6 Classes K e y S o r t C l t n , LookupKey, and Assoc 161 8.3.7 Class L i n k e d L i s t 169 8.3.8 Class Se t 173 8.3.9 Class I d e n t S e t 176 8.3.10 Class D i c t i o n a r y 177 8.3.11 Class I d e n t D i c t 182 8.3.12 Class N i l and the N i l object 185

8.4 GUIDELINES FOR USING CONTAINER CLASSES 186 8.4.1 Specializing container classes 186 8.4.2 Managing memory correctly 187 8.4.3 Modifying objects while in containers 189

8.5 CHAPTER SUMMARY 190

9 Designing Library Classes 193 9.1 INTRODUCTION 193 9.2 OBJECT-ORIENTED DESIGN 194

9.2.1 When to use classes 195

Page 6: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

CONTENTS ix

9.2.2 Organizing classes 196 9.2.3 Member instances as opposed to member pointers to instances 204 9.2.4 Member functions 209 9.2.5 Member variables 213 9.2.6 Member accessibility 215

9.3 CHAPTER SUMMARY 218

10 An Example NIH Library Class 219 10.1 INTRODUCTION 219 10.2 WRITING THE SPECIFICATION 219

10.2.1 Including header files and preventing multiple definitions 221 10.2.2 Declaring NIH Class Library members 222 10.2.3 Member variables of class ArrayOb 222 10.2.4 Private member functions of class ArrayOb 222 10.2.5 Object I/O member functions of class ArrayOb 223 10.2.6 Public member functions of class ArrayOb 223

10.3 WRITING THE IMPLEMENTATION 225 10.3.1 Include files 225 10.3.2 THIS and BASE 226 10.3.3 BASE.CLASSES, MEMBER.CLASSES, and VIRTUAL_BASE_CLASSES 226 10.3.4 DEFINE.CLASS preprocessor macro 227 10.3.5 DEFINE_ABSTRACT_CLASS preprocessor macro 228 10.3.6 Symbolic error codes 228 10.3.7 ArrayOb constructors 228 10.3.8 ArrayOb assignment operator 229 10.3.9 ArrayOb: : o p e r a t o r = = ( ) 229 10.3.10 ArrayOb: : species () andisEqualO 230 10.3.11 ArrayOb: :hash () 234 10.3.12 ArrayOb: :deepenShallowCopy () 234 10.3.13 ArrayOb: : addContentsTo () 234 10.3.14 ArrayOb: : compare () 235 10.3.15 Interface to class I t e r a t o r 235 10.3.16 ArrayOb: : a t () 236 10.3.17 size() and capacity () 236 10.3.18 ArrayOb: : resize () 236 10.3.19 ArrayOb: :removeAll () 237 10.3.20 ArrayOb: :allocSizeErr () 237 10.3.21 ArrayOb: : IndexRangeErr () 237 10.3.22 Stream object I/O 238 10.3.23 File descriptor object I/O 238

10.4 CHAPTER SUMMARY 239

11 Lightweight Processes 241 11.1 INTRODUCTION 241

11.1.1 The lightweight process data type 241 11.1.2 Lightweight processes in the NIH Class Library 242

11.2 CLASSES P r o c e s s , S t a c k P r o c , AND HeapProc 244 11.3 CLASS S c h e d u l e r 247

11.4 CLASS S e m a p h o r e 250 11.5 CLASS S h a r e d Q u e u e 256 11.6 CHAPTER SUMMARY 258

12 An Object-Oriented Application 261 12.1 INTRODUCTION 261

12.1.1 Background and design goals 261

Page 7: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

x CONTENTS

12.1.2 A model of the backup system 261 12.2 THE OBJECT-ORIENTED DESIGN 262

12.2.1 Lightweight processes 265 12.2.2 Resource management 266 12.2.3 The backup database 266

12.3 AN IMPLEMENTATION BASED ON THE NIH CLASS LIBRARY 267 12.3.1 Data records 268 12.3.2 Data object containers 269 12.3.3 Data flow containers 271 12.3.4 Specialized objects 272 12.3.5 Lightweight process objects 273 12.3.6 The dynamics of the backup system 276

12.4 IMPLEMENTING A DATABASE WITH THE NIH CLASS LIBRARY 279 12.4.1 Backup State database 281 12.4.2 Residence Registry database 283 12.4.3 Tape Catalog database 283 12.4.4 Tape Registry database 285 12.4.5 Implementation of the backup database 285

12.5 CHAPTER SUMMARY 290

13 Multiple Inheritance 293 13.1 INTRODUCTION 293 13.2 MULTIPLE INHERITANCE AND MODULAR PROGRAMMING 293

13.2.1 What is modular programming? 293 13.2.2 Practicing "modular programming" in C++ 294 13.2.3 Class NIHCL as a "module" 295 13.2.4 Private base classes 297

13.3 MULTIPLE LINKED LIST EXAMPLE 297 13.3.1 Inheritance diagrams 299 13.3.2 Improved vehicle linked lists 300

13.4 RESOLVING AMBIGUITIES 302 13.5 VIRTUAL FUNCTIONS AND MULTIPLE INHERITANCE 304 13.6 VIRTUAL BASE CLASSES 305

13.6.1 Changes to class V e h i c l e 305 13.6.2 Classes LandVhcl and WaterVhcl 305 13.6.3 Classes S topL igh tQ and DrawBridgeQ 305 13.6.4 Class AmphibVhcl 306 13.6.5 Virtual base class constructors 307 13.6.6 Virtual base class example 308

13.7 VIRTUAL BASE CLASSES AND VIRTUAL FUNCTIONS 312 13.8 OBJECT INITIALIZATION AND MULTIPLE INHERITANCE 314

13.8.1 Object finalization 316 13.8.2 Calling virtual functions from a base class constructor 316

13.9 PROGRAMMING WITH VIRTUAL BASE CLASSES 318 13.9.1 Static method for eliminating multiple function calls 318 13.9.2 Dynamic method for eliminating multiple function calls 322 13.9.3 Summary of programming with virtual base classes 324

13.10 MULTIPLE INHERITANCE AND THE NIH CLASS LIBRARY 325 13.10.1 O b j e c t as a virtual base class 328 13.10.2 Downward casts from a virtual base class 328 13.10.3 Multiple inheritance and deepCopy () 334 13.10.4 Multiple inheritance and object I/O 337 13.10.5 Methods for resolving ambiguous downward casts 343 13.10.6 Summary of multiple inheritance and the NIH Class Library 345

13.11 CHAPTER SUMMARY 346

Page 8: DATA ABSTRACTION ш OBJECT-ORIENTED PROGRAMMING IN …

CONTENTS xi

14 Future Directions 349 14.1 INTRODUCTION 349 14.2 EXCEPTION HANDUNG 349

14.2.1 A hypothetical exception handling mechanism for C++ 350 14.2.2 Exception handling in the NIH Class Library 352

14.3 PARAMETERIZED TYPES 353 14.3.1 A hypothetical parameterized type facility for C++ 354 14.3.2 Parameterized types in the NIH Class Library 358 14.3.3 Summary of parameterized types 361

14.4 GARBAGE COLLECTION 361 14.4.1 Counted pointers 361 14.4.2 Garbage-collecting versions of malloc () 365

14.5 DYNAMIC LINKING 367

14.6 CHAPTER SUMMARY 369

Appendix A NIH Class Library Hierarchy 371

Appendix В NIH Class Library Template Files 373

Appendix С Tips for С Programmers 381

References 393

Index 395