Object-oriented programming with C

所需积分/C币:46 2012-09-15 07:09:49 1.2MB PDF
1
收藏 收藏
举报

Object-oriented programming with C Object-oriented programming with C
Contents Preface 1 Abstract Data Types- Information Hiding 1.1 Data Types 1.2 Abstract Data Types 3 An Example- Set 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 9 Summary 9 1.10 Exercises 2 Dynamic Linkage- Generic Functions 2.1 Constructors and Destructors 2.2 Methods, Messages, Classes and Objects 2.3 Selectors, Dynamic Linkage, and Polymorphisms 2.4 An Application 16 2.5 An Implementation- String 2.6 Another Implementation -Atom 18 2.7 Summary 20 2.8 Exercises 3 Programming Savvy- Arithmetic Expressions 21 3.1 The Main Loop 21 3.2 The scanner 3.3 The Recognizer 23 3.4 The processor 23 3.5 Information Hiding 24 3.6 Dynamic Linkage 25 7 A Postfix Writer 3. 8 Arithmetic 28 3.9 Infix Output 28 3.10 Summary 29 4 nheritance- Code reuse and refinement 4.1 A Superclass - Point 31 4.2 Superclass Implementation- Point 32 4.3 nheritance- Circie 33 4.4 Linkage and Inheritance 35 4.5 Static and Dynamic Linkage 4.6 Visibi lity and Access Functions 37 4.7 Subclass Implementation- Circle 39 Contents 4.8S umm 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 5.7 Another Subclass- Constants 52 5.8 Mathematical Functions -Math 52 5.9 Su 55 5. 10 Exercises 55 6 Class hierarchy -Maintainability 57 6.1 Requirements 57 6.2 etaclasses 58 6.3 Roots- Object and Class 59 6. 4 Subclassing-Any 6.5 Implementation-Object 62 6.6 Implementation--Class 6 6.7 nitialization 65 6.8 Selectors 65 6.9 Superclass Selectors 66 6.10 A New Metaclass- PointCclass 68 6.11 Summary 7 The ooc Preprocessor -Enforcing a Coding standard 7. 1 Point Revisited 7.2 Design 78 7.3 Preprocessing 79 7.4 Implementation Strategy 7.5 Object Revisited 82 7.6 Discussion 84 7.7 An Example- List, Queue, and Stack 7.8 Exercise 89 8 Dynamic Type Checking--Defensive Programming 8. 1 Technique 8.2 An Example -list 8.3 Implementation 94 8.4 Coding Standard 94 8.5 Avoiding recursion 98 8.6 Summary 100 8.7 Exercises 10 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 0 Delegates- Callback Functions 10.1 Callbacks 10.2 Abstract base Classes 10.3 Delegates 113 10.4 An Application Framework-Filter 114 10.5 The responds To 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 11.4 Programming Savvy -A Classy Calculator 128 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() 15 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 16 13.3 Examples 163 13.4 Summary 165 13.5 Exercises 4 Forwarding Messages-A GUI Calculator 167 4.1 The dea 167 14.2 Implementation 168 14.3 Object-Oriented Design by EXample 171 14.4 Implementation -Ic 174 Contents 14.5 A Character-Based Interface- curses 14.6 A Graphical Interface-Xt 182 4. 7 Summary 188 14.8 Exercises 189 A ANSI-C Programming Hints 191 A Names and scope 191 A2 Functions 191 A3 Generic Pointers 192 A 4 const 193 A.5 typedef and const 194 A 6 Structures 194 A 7 Pointers to Functions 195 A 8 Preprocessor 196 A 9 Veritication - assert. h 196 A 10 Global Jumps- set mp. h 196 A 11 Var iable Argument Lists- stdarg. h 197 A 12 Data Types- stddef. h 198 A 13 Memory Management- std/ib h 198 A 14 Memory Functions- string. h B The ooc Preprocessor-Hints on awk Programming 199 B. 1 Architecture 199 B 2 File Management -/0. awk 200 B3 Recognition- parse awk B. 4 The Database 201 B5 Report Generation- report awk 202 B6 Line Numbering 203 B.7 The Main Program -main awk 204 B 8 Report Files 204 B. 9 The ooc command 205 c Manual 207 C 1 Commands 207 C 2 Functions 214 C3 Root Classes 214 C 4 GUI Calculator Classes 218 ocra 223 Abstract Data Types Information Hiding 1.1 Data Typ Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content with what's available and a programming language normally provides facilities to build new data types trom those that are predefined. A simple approach is to form aggregates such as arrays, structures, or unions. Pointers, according to C. A R. Hoare a step from which we may never recover " permit us to represent and manipulate data of essentially un limited complexity What exactly is a data type? We can take several points of view. a data type is a set of values- char typically has 256 distinct values, int has many more; both are evenly spaced and behave more or less like the natural numbers or integers of mathematics. double once again has many more values, but they certainly do not behave like mathematics real numbers Alternatively, we can define a data type as a set of values plus operations to work with them. Typically, the values are what a computer can represent, and the operations more or less reflect the available hardware instructions. int in ANSI-C does not do too well in this respect: the set of values may vary between machines and operations like arithmetic right shift may behave differently More complicated examples do not tare much better. Typically we would define an element of a linear list as a structure typedc= struct nodc「 struct node next information and for the operations we specify function headers like node A head (node elt, const node A tail This approach, however, is quite sloppy. good programming principles dictate that we conceal the representation of a data item and declare only the possible manipulations 1.2 Abstract Data Types We call a data type abstract, if we do not reveal its representation to the user. at theoretical level this requires us to specify the properties of the data type by mathematical axioms involving the possible operations. For example, we cal and we retrieve the elements in the same order in which they were adder eviously remove an clement from a queue only as often as we have added one pi n Abstract Data Types- Information Hiding Abstract data types offer great flexibility to the programmer. Since the representation is not part of the definition we are free to choose whatever is easi est or most efficient to implement. If we manage to distribute the necessary infor- mation correctly, use of the data type and our choice of implementation are totally dependent Abstract data types satisfy the good programming principles of information hid- ing and divide and conquer. Information such as the representation of data items is given only to the one with a need to know: to the implementer and not to the user With an abstract data type we cleanly separate the programming tasks of imple mentation and usage: we are well on our way to decompose a large system into smaller modules 1.3 An Example- Set So how do we implement an abstract data type? As an example we consider a set of elements with the operations add, find, and drop. x They all apply to a set and an element and return the element added to found in or removed from a set find can be used to implement a condition contains which tells us whether an element is already contained in a set Viewed this way, set is an abstract data type. To declare what we can do with a set we start a header file set. h #inde= SE-H # define sf冖H tern const voi set: void x add id x set, const yoid element. t void ⅴO主d*dxop(void*set, const yolo*e1 erent) int contains (const void setr const void eleme: 1t)i fendi The preprocessor statements protect the declarations: no matter how many times we include Seth, the c compiler only sees the declarations once. This technique of protecting header files is so standard, that the gnu c preprocessor recognizes it and does not even access such a file when its protecting symbol is defined Set h is complete, but is it useful? We can hardly reveal or assume less: Set will have to somehow represent the fact, that we are working with sets: add( takes an element adds it to a set, and returns whatever was added or alread present in the set: find/) looks for an element in a set and returns whatever is present in the set or a null pointer drop( locates an element, removes it from a set, and returns whatever was removed contains converts the result of findo into a truth value 4 Unfortunately, remove is an ANSI-C library function to remove a file. If we used this name for a set function we could no longer include stdio. h 4 Memory Management The generic pointer void is used throughout. On the one hand it makes it impossible to discover what a set looks like, but on the other hand it permits us to pass virtually anything to addo and the other functions. Not everything will behave like a set or an element- we are sacrificing type security in the interest of informa- tion hiding. However, we wIll see in chapter 8 that this approach can be made completely secure 1.4 Memory Management We may have overlooked something: how does one obtain a set? Set is a pointer not a type defined by typedef; therefore, we cannot define local or global variables pe Set. Instead, we are only going to use pointers to refer to sets and ele ments and we declare source and sink of all data items in new h void new (const void ; Just like Set h this file is protected by a preprocess bol new H. the text only shows the interesting parts of each new file, the source diskette contains the com- plete code of all examples new( accepts a descriptor like Set and possibly more arguments for initialize tion and returns a pointer to a new data item with a representation conforming to the descriptor. delete ( accepts a pointer originally produced by new and recycles the associated resources new( and delete() presumably are a frontend to the ANSl-C functions calloc( and free(). If they are, the descriptor has to indicate at least how much memory is required 1.5 Object If we want to collect anything interesting in a set, we need another abstract data type object described by the header file object. h extern const. voic Object ,x new(Object) t differ (const void t void b differ() can compare objects: it returns true if they are not equal and false if they are. This description leave or the functionality of strcmp(): for some pa of objects we might choose to return a negative or positive value to specify an or- dering Real life objects re functionality to do something useful. For the moment, we restrict ourselves to the bare necessities for membership in a set. If we built a bigger class library, we would see that a set - and in tact everything else- is an object, too. At this point, a lot of functionality results more or less for free n Abstract Data Types- Information Hiding 1.6 An Application With the header files, i. e, the definitions of the abstract data types in place we can te an application m Include <stdio. h> include "ncw. h' include "Object. h include nt main o i void s= new(set) d x a= add( ○ bject)); b void x c- new(Chjcct)i if (contains(s, a)&& contains(s, b) puts (ok) if (contains(s, c) puts("contains?)i f (differ(a add(s, a))) puts ("differ? (contains(s, drop(s, a))) puts("drop? )i delete(drop(s, b)) return O We create a set and add two new objects to it. If all is well, we find the objects in the set and we should not find another new object. The program should simply print ok The call to differ()illustrates a semantic point: a mathematical set can only contain one copy of the object a; an attempt to add it again must return the original object and differ() ought to be false. Similarly, once we remove the object,it should no longer be in the set Removing an element not in a set will result in a null pointer being passed to deletel). For now, we stick with the semantics of freel) and require this to be acceptable 1.7 An Implementation--Set main. c will compile successfully but before we can link and execute the program We must implement the abstract data types and the memory manager. If an object stores no information and if every object belongs to at most one set, we can represent each object and each set as small, unique, positive integer values used as indices into an array heap] If an object is a member of a set, its array element con tains the integer value representing the set. Objects, therefore, point to the set containing them

...展开详情
试读 127P Object-oriented programming with C
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
上传资源赚积分or赚钱
    最新推荐
    Object-oriented programming with C 46积分/C币 立即下载
    1/127
    Object-oriented programming with C第1页
    Object-oriented programming with C第2页
    Object-oriented programming with C第3页
    Object-oriented programming with C第4页
    Object-oriented programming with C第5页
    Object-oriented programming with C第6页
    Object-oriented programming with C第7页
    Object-oriented programming with C第8页
    Object-oriented programming with C第9页
    Object-oriented programming with C第10页
    Object-oriented programming with C第11页
    Object-oriented programming with C第12页
    Object-oriented programming with C第13页
    Object-oriented programming with C第14页
    Object-oriented programming with C第15页
    Object-oriented programming with C第16页
    Object-oriented programming with C第17页
    Object-oriented programming with C第18页
    Object-oriented programming with C第19页
    Object-oriented programming with C第20页

    试读结束, 可继续阅读

    46积分/C币 立即下载 >