没有合适的资源?快使用搜索试试~ 我知道了~
首页Object-oriented programming with C.英文版
资源详情
资源评论
资源推荐

v
___________________________________________________________________________
Preface
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 been
around for much more then ten years. At the core, there is little more to it then
finally applying the good programming principles which we have been taught for
more then twenty years. C++ (Eiffel, Oberon-2, Smalltalk ... take your pick) is the
New Language because it is object-oriented — although you need not use it that
way if you do not want to (or know how to), and it turns out that you can do just as
well with plain
ANSI
-C. Only object-orientation permits code reuse between pro-
jects — although the idea of subroutines is as old as computers and good program-
mers always carried their toolkits and libraries with them.
This book is not going to praise object-oriented programming or condemn the
Old Way. We are simply going to use
ANSI
-C to discover how object-oriented pro-
gramming is done, what its techniques are, why they help us solve bigger prob-
lems, and how we harness generality and program to catch mistakes earlier. Along
the way we encounter all the jargon — classes, inheritance, instances, linkage,
methods, objects, polymorphisms, and more — but we take it out of the realm of
magic 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. To
share this fun you need to be reasonably fluent in
ANSI
-C to begin with — feeling
comfortable with structures, pointers, prototypes, and function pointers is a must.
Working through the book you will encounter all the newspeak — according to
Orwell and Webster a language ‘‘designed to diminish the range of thought’’ — and
I will try to demonstrate how it merely combines all the good programming princi-
ples that you always wanted to employ into a coherent approach. As a result, you
may well become a more proficient
ANSI
-C programmer.
The first six chapters develop the foundations of object-oriented programming
with
ANSI-C. We start with a careful information hiding technique for abstract data
types, add generic functions based on dynamic linkage and inherit code by judicious
lengthening of structures. Finally, we put it all together in a class hierarchy that
makes code much easier to maintain.
Programming takes discipline. Good programming takes a lot of discipline, a
large number of principles, and standard, defensive ways of doing things right. Pro-
grammers use tools. Good programmers make tools to dispose of routine tasks
once and for all. Object-oriented programming with
ANSI-C requires a fair amount
of immutable code — names may change but not the structures. Therefore, in
chapter seven we build a small preprocessor to create the boilerplate required. It
looks like yet another new object-oriented dialect language (yanoodl perhaps?) but
it should not be viewed as such — it gets the dull parts out of the way and lets us
concentrate on the creative aspects of problem solving with better techniques. ooc
//mr?? En with介词

vi
___________________________________________________________________________
Preface
(sorry) is pliable: we have made it, we understand it and can change it, and it
writes the
ANSI
-C code just like we would.
The following chapters refine our technology. In chapter eight we add dynamic
type checking to catch our mistakes earlier on. In chapter nine we arrange for
automatic initialization to prevent another class of bugs. Chapter ten introduces
delegates and shows how classes and callback functions cooperate to simplify, for
example, the constant chore of producing standard main programs. More chapters
are concerned with plugging memory leaks by using class methods, storing and
loading structured data with a coherent strategy, and disciplined error recovery
through a system of nested exception handlers.
Finally, in the last chapter we leave the confines of
ANSI-C and implement the
obligatory mouse-operated calculator, first for
curses and then for the X Window
System. This example neatly demonstrates how elegantly we can design and
implement using objects and classes, even if we have to cope with the idiosyn-
crasies of foreign libraries and class hierarchies.
Each chapter has a summary where I try to give the more cursory reader a run-
down on the happenings in the chapter and their importance for future work. Most
chapters suggest some exercises; however, they are not spelled out formally,
because I firmly believe that one should experiment on one’s own. Because we are
building the techniques from scratch, I have refrained from making and using a
massive class library, even though some examples could have benefited from it. If
you want to understand object-oriented programming, it is more important to first
master the techniques and consider your options in code design; dependence on
somebody else’s library for your developments should come a bit later.
An important part of this book is the enclosed source floppy — it has a
DOS file
system containing a single shell script to create all the sources arranged by chapter.
There is a ReadMe file — consult it before you say make. It is also quite instructive
to use a program like diff and trace the evolution of the root classes and ooc reports
through the later chapters.
The techniques described here grew out of my disenchantment with C++ when
I needed object-oriented techniques to implement an interactive programming
language and realized that I could not forge a portable implementation in C++. I
turned to what I knew,
ANSI-C, and I was perfectly able to do what I had to. I have
shown this to a number of people in courses and workshops and others have used
the methods to get their jobs done. It would have stopped there as my footnote to
a fad, if Brian Kernighan and my publishers, Hans-Joachim Niclas and John Wait,
had not encouraged me to publish the notes (and in due course to reinvent it all
once more). My thanks go to them and to all those who helped with and suffered
through the evolution of this book. Last not least I thank my family — and no,
object-orientation will not replace sliced bread.
Hollage, October 1993
Axel-Tobias Schreiner

vii
___________________________________________________________________________
Contents
Preface ...........................
5
1 Abstract Data Types — Information Hiding
........... 1
1.1 Data Types ......................
1
1.2 Abstract Data Types ..................
1
1.3 An Example — Set ...................
2
1.4 Memory Management ................. 3
1.5 Object .......................
3
1.6 An Application ....................
4
1.7 An Implementation —
Set ................ 4
1.8 Another Implementation —
Bag .............. 7
1.9 Summary ......................
9
1.10 Exercises ......................
9
2 Dynamic Linkage — Generic Functions
............. 11
2.1 Constructors and Destructors .............. 11
2.2 Methods, Messages, Classes and Objects ......... 12
2.3 Selectors, Dynamic Linkage, and Polymorphisms ....... 13
2.4 An Application .................... 16
2.5 An Implementation — String ............... 17
2.6 Another Implementation — Atom ............. 18
2.7 Summary ...................... 20
2.8 Exercises ...................... 20
3 Programming Savvy — Arithmetic Expressions ......... 21
3.1 The Main Loop .................... 21
3.2 The Scanner ..................... 22
3.3 The Recognizer .................... 23
3.4 The Processor .................... 23
3.5 Information Hiding ................... 24
3.6 Dynamic Linkage ................... 25
3.7 A Postfix Writer .................... 26
3.8 Arithmetic ...................... 28
3.9 Infix Output ..................... 28
3.10 Summary ...................... 29
4 Inheritance — Code Reuse and Refinement ........... 31
4.1 A Superclass — Point .................. 31
4.2 Superclass Implementation — Point ............ 32
4.3 Inheritance — Circle .................. 33
4.4 Linkage and Inheritance ................. 35
4.5 Static and Dynamic Linkage ............... 36
4.6 Visibility and Access Functions .............. 37
4.7 Subclass Implementation — Circle ............. 39

viii
___________________________________________________________________________
Contents
4.8 Summary ......................
40
4.9 Is It or Has It? — Inheritance vs. Aggregates ........ 42
4.10 Multiple Inheritance .................. 42
4.11 Exercises ......................
43
5 Programming Savvy — Symbol Table ............. 45
5.1 Scanning Identifiers ..................
45
5.2 Using Variables
....................
45
5.3 The Screener — Name ................. 47
5.4 Superclass Implementation
— Name ............ 48
5.5 Subclass Implementation
— Var .............. 50
5.6 Assignment .....................
51
5.7 Another Subclass — Constants .............. 52
5.8 Mathematical Functions
— Math ............. 52
5.9 Summary ......................
55
5.10 Exercises ......................
55
6 Class Hierarchy — Maintainability ............... 57
6.1 Requirements .....................
57
6.2 Metaclasses .....................
58
6.3 Roots — Object and Class ................ 59
6.4 Subclassing — Any .................. 60
6.5 Implementation — Object ................ 62
6.6 Implementation — Class ................ 63
6.7 Initialization ..................... 65
6.8 Selectors ...................... 65
6.9 Superclass Selectors .................. 66
6.10 A New Metaclass — PointClass .............. 68
6.11 Summary ...................... 70
7 The ooc Preprocessor — Enforcing a Coding Standard ...... 73
7.1 Point Revisited .................... 73
7.2 Design ....................... 78
7.3 Preprocessing .................... 79
7.4 Implementation Strategy ................ 80
7.5 Object Revisited .................... 82
7.6 Discussion ...................... 84
7.7 An Example — List, Queue, and Stack ........... 85
7.8 Exercises ...................... 89
8 Dynamic Type Checking — Defensive Programming ....... 91
8.1 Technique ...................... 91
8.2 An Example — list ................... 92
8.3 Implementation .................... 94
8.4 Coding Standard .................... 94
8.5 Avoiding Recursion .................. 98
8.6 Summary ...................... 100
8.7 Exercises ...................... 101

ix
___________________________________________________________________________
Contents
9 Static Construction — Self-Organization ............ 103
9.1 Initialization .....................
103
9.2 Initializer Lists — munch ................ 104
9.3 Functions for Objects .................. 106
9.4 Implementation ....................
107
9.5 Summary ......................
109
9.6 Exercises ......................
110
10 Delegates — Callback Functions ............... 111
10.1 Callbacks ......................
111
10.2 Abstract Base Classes .................
111
10.3 Delegates ......................
113
10.4 An Application Framework — Filter ............ 114
10.5 The respondsTo Method ................ 117
10.6 Implementation
....................
119
10.7 Another application — sort ................ 122
10.8 Summary ......................
123
10.9 Exercises ......................
124
11 Class Methods — Plugging Memory Leaks ........... 125
11.1 An Example ..................... 125
11.2 Class Methods .................... 127
11.3 Implementing Class Methods .............. 128
11.4 Programming Savvy — A Classy Calculator ......... 131
11.5 Summary ...................... 140
11.6 Exercises ...................... 141
12 Persistent Objects — Storing and Loading Data Structures .... 143
12.1 An Example ..................... 143
12.2 Storing Objects — puto() ................ 148
12.3 Filling Objects — geto() ................. 150
12.4 Loading Objects — retrieve() ............... 151
12.5 Attaching Objects — value Revisited ............ 153
12.6 Summary ...................... 156
12.7 Exercises ...................... 157
13 Exceptions — Disciplined Error Recovery ............ 159
13.1 Strategy ....................... 159
13.2 Implementation — Exception ............... 161
13.3 Examples ...................... 163
13.4 Summary ...................... 165
13.5 Exercises ...................... 166
14 Forwarding Messages — A
GUI Calculator ........... 167
14.1 The Idea ....................... 167
14.2 Implementation .................... 168
14.3 Object-Oriented Design by Example ............ 171
14.4 Implementation — Ic .................. 174
剩余220页未读,继续阅读













安全验证
文档复制为VIP权益,开通VIP直接复制

评论0