Object oriented programming with ANSI-C
暂无分享,去创建一个
data types offer great flexibility to the programmer. Since the representation is not part of the definition, we are free to choose whatever is easiest or most efficient to implement. If we manage to distribute the necessary information correctly, use of the data type and our choice of implementation are totally independent. Abstract data types satisfy the good programming principles of information hiding 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 implementation 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.* 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: #ifndef SET_H #define SET_H extern const void * Set; void * add (void * set, const void * element); void * find (const void * set, const void * element); void * drop (void * set, const void * element); int contains (const void * set, const void * element);
[1] Timothy A. Budd,et al. An introduction to object-oriented programming , 1991 .
[2] Brian W. Kernighan,et al. PIC — A language for typesetting graphics , 1982, Softw. Pract. Exp..
[3] Steve Hilditch. Using C with Curses, Lex and YACC , 1991 .