No programming technique solves all problems.No programming language produces only correct results.No programmer should start each project from scratch.Object-oriented programming is the current cure-all — although it has beenaround for much more then ten years. At the core, there is little more to it thenfinally applying the good programming principles which we have been taught formore then twenty years. C (Eiffel, Oberon-2, Smalltalk ... take your pick) is theNew Language because it is object-oriented — although you need not use it thatway if you do not want to (or know how to), and it turns out that you can do just aswell with plain ANSI-C. Only object-orientation permits code reuse between projects — although the idea of subroutines is as old as computers and good programmers always carried their toolkits and libraries with them.This book is not going to praise object-oriented programming or condemn theOld Way. We are simply going to use ANSI-C to discover how object-oriented programming is done, what its techniques are, why they help us solve bigger problems, and how we harness generality and program to catch mistakes earlier. Alongthe way we encounter all the jargon — classes, inheritance, instances, linkage,methods, objects, polymorphisms, and more — but we take it out of the realm ofmagic and see how it translates into the things we have known and done all along.I had fun discovering that ANSI-C is a full-scale object-oriented language. Toshare this fun you need to be reasonably fluent in ANSI-C to begin with — feelingcomfortable with structures, pointers, prototypes, and function pointers is a must.Working through the book you will encounter all the newspeak — according toOrwell and Webster a language ‘‘designed to diminish the range of thought’’ — andI will try to demonstrate how it merely combines all the good programming principles that you always wanted to employ into a coherent approach. As a result, youmay well become a more proficient ANSI-C programmer.The first six chapters develop the foundations of object-oriented programmingwith ANSI-C. We start with a careful information hiding technique for abstract datatypes, add generic functions based on dynamic linkage and inherit code by judiciouslengthening of structures. Finally, we put it all together in a class hierarchy thatmakes code much easier to maintain.Programming takes discipline. Good programming takes a lot of discipline, alarge number of principles, and standard, defensive ways of doing things right. Programmers use tools. Good programmers make tools to dispose of routine tasksonce and for all. Object-oriented programming with ANSI-C requires a fair amountof immutable code — names may change but not the structures. Therefore, inchapter seven we build a small preprocessor to create the boilerplate required. Itlooks like yet another new object-oriented dialect language (yanoodl perhaps?) butit should not be viewed as such — it gets the dull parts out of the way and lets usconcentrate on the creative aspects of problem solving with better techniques.
ContentsPreface ........................... 51 Abstract Data Types — Information Hiding ........... 11.1 Data Types ...................... 11.2 Abstract Data Types .................. 11.3 An Example — Set ................... 21.4 Memory Management ................. 31.5 Object ....................... 31.6 An Application .................... 41.7 An Implementation — Set ................ 41.8 Another Implementation — Bag .............. 71.9 Summary ...................... 91.10 Exercises ...................... 92 Dynamic Linkage — Generic Functions ............. 112.1 Constructors and Destructors .............. 112.2 Methods, Messages, Classes and Objects ......... 122.3 Selectors, Dynamic Linkage, and Polymorphisms ....... 132.4 An Application .................... 162.5 An Implementation — String ............... 172.6 Another Implementation — Atom ............. 182.7 Summary ...................... 202.8 Exercises ...................... 203 Programming Savvy — Arithmetic Expressions ......... 213.1 The Main Loop .................... 213.2 The Scanner ..................... 223.3 The Recognizer .................... 233.4 The Processor .................... 233.5 Information Hiding ................... 243.6 Dynamic Linkage ................... 253.7 A Postfix Writer .................... 263.8 Arithmetic ...................... 283.9 Infix Output ..................... 283.10 Summary ...................... 294 Inheritance — Code Reuse and Refinement ........... 314.1 A Superclass — Point .................. 314.2 Superclass Implementation — Point ............ 324.3 Inheritance — Circle .................. 334.4 Linkage and Inheritance ................. 354.5 Static and Dynamic Linkage ............... 364.6 Visibility and Access Functions .............. 374.7 Subclass Implementation — Circle ............. 39viii___________________________________________________________________________ Contents4.8 Summary ...................... 404.9 Is It or Has It? — Inheritance vs. Aggregates ........ 424.10 Multiple Inheritance .................. 424.11 Exercises ...................... 435 Programming Savvy — Symbol Table ............. 455.1 Scanning Identifiers .................. 455.2 Using Variables .................... 455.3 The Screener — Name ................. 475.4 Superclass Implementation — Name ............ 485.5 Subclass Implementation — Var .............. 505.6 Assignment ..................... 515.7 Another Subclass — Constants .............. 525.8 Mathematical Functions — Math ............. 525.9 Summary ...................... 555.10 Exercises ...................... 556 Class Hierarchy — Maintainability ............... 576.1 Requirements ..................... 576.2 Metaclasses ..................... 586.3 Roots — Object and Class ................ 596.4 Subclassing — Any .................. 606.5 Implementation — Object ................ 626.6 Implementation — Class ................ 636.7 Initialization ..................... 656.8 Selectors ...................... 656.9 Superclass Selectors .................. 666.10 A New Metaclass — PointClass .............. 686.11 Summary ...................... 707 The ooc Preprocessor — Enforcing a Coding Standard ...... 737.1 Point Revisited .................... 737.2 Design ....................... 787.3 Preprocessing .................... 797.4 Implementation Strategy ................ 807.5 Object Revisited .................... 827.6 Discussion ...................... 847.7 An Example — List, Queue, and Stack ........... 857.8 Exercises ...................... 898 Dynamic Type Checking — Defensive Programming ....... 918.1 Technique ...................... 918.2 An Example — list ................... 928.3 Implementation .................... 948.4 Coding Standard .................... 948.5 Avoiding Recursion .................. 988.6 Summary ...................... 1008.7 Exercises ...................... 101_Contents __________________________________________________________________________ix9 Static Construction — Self-Organization ............ 1039.1 Initialization ..................... 1039.2 Initializer Lists — munch ................ 1049.3 Functions for Objects .................. 1069.4 Implementation .................... 1079.5 Summary ...................... 1099.6 Exercises ...................... 11010 Delegates — Callback Functions ............... 11110.1 Callbacks ...................... 11110.2 Abstract Base Classes ................. 11110.3 Delegates ...................... 11310.4 An Application Framework — Filter ............ 11410.5 The respondsTo Method ................ 11710.6 Implementation .................... 11910.7 Another application — sort ................ 12210.8 Summary ...................... 12310.9 Exercises ...................... 12411 Class Methods — Plugging Memory Leaks ........... 12511.1 An Example ..................... 12511.2 Class Methods .................... 12711.3 Implementing Class Methods .............. 12811.4 Programming Savvy — A Classy Calculator ......... 13111.5 Summary ...................... 14011.6 Exercises ...................... 14112 Persistent Objects — Storing and Loading Data Structures .... 14312.1 An Example ..................... 14312.2 Storing Objects — puto() ................ 14812.3 Filling Objects — geto() ................. 15012.4 Loading Objects — retrieve() ............... 15112.5 Attaching Objects — value Revisited ............ 15312.6 Summary ...................... 15612.7 Exercises ...................... 15713 Exceptions — Disciplined Error Recovery ............ 15913.1 Strategy ....................... 15913.2 Implementation — Exception ............... 16113.3 Examples ...................... 16313.4 Summary ...................... 16513.5 Exercises ...................... 16614 Forwarding Messages — A GUI Calculator ........... 16714.1 The Idea ....................... 16714.2 Implementation .................... 16814.3 Object-Oriented Design by Example ............ 17114.4 Implementation — Ic .................. 174x___________________________________________________________________________ Contents14.5 A Character-Based Interface — curses ........... 17914.6 A Graphical Interface — Xt ................ 18214.7 Summary ...................... 18814.8 Exercises ...................... 189A ANSI-C Programming Hints ................. 191A.1 Names and Scope ................... 191A.2 Functions ...................... 191A.3 Generic Pointers — void * ................ 192A.4 const ........................ 193A.5 typedef and const ................... 194A.6 Structures ...................... 194A.7 Pointers to Functions .................. 195A.8 Preprocessor ..................... 196A.9 Verification — assert.h ................. 196A.10 Global Jumps — setjmp.h ................ 196A.11 Variable Argument Lists — stdarg.h ............ 197A.12 Data Types — stddef.h ................. 198A.13 Memory Management — stdlib.h ............. 198A.14 Memory Functions — string.h .............. 198B The ooc Preprocessor — Hints on awk Programming ....... 199B.1 Architecture ..................... 199B.2 File Management — io.awk ............... 200B.3 Recognition — parse.awk ................ 200B.4 The Database ..................... 201B.5 Report Generation — report.awk ............. 202B.6 Line Numbering .................... 203B.7 The Main Program — main.awk .............. 204B.8 Report Files ..................... 204B.9 The ooc Command ................... 205C Manual .......................... 207C.1 Commands ...................... 207C.2 Functions ...................... 214C.3 Root Classes ..................... 214C.4 GUI Calculator Classes ................. 218Bibliography ......................... 223
评论