Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
C OOProgramming Using Ansi C

C OOProgramming Using Ansi C

Ratings: (0)|Views: 24 |Likes:
Published by api-26972476

More info:

Published by: api-26972476 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

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 \u2014 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 \u2014 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 plainANSI-C. Only object-orientation permits code reuse between pro- jects \u2014 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 useANSI-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 \u2014 classes, inheritance, instances, linkage, methods, objects, polymorphisms, and more \u2014 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 thatANSI-C is a full-scale object-oriented language. To share this fun you need to be reasonably fluent inANSI-C to begin with \u2014 feeling comfortable with structures, pointers, prototypes, and function pointers is a must. Working through the book you will encounter all the newspeak \u2014 according to Orwell and Webster a language \u2018\u2018designed to diminish the range of thought\u2019\u2019 \u2014 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 proficientANSI-C programmer.

The first six chapters develop the foundations of object-oriented programming withANSI-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 withANSI-C requires a fair amount of immutable code \u2014 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 \u2014 it gets the dull parts out of the way and lets us concentrate on the creative aspects of problem solving with better techniques.ooc

vi
___________________________________________________________________________
Preface
(sorry) is pliable: we have made it, we understand it and can change it, and it
writes theANSI-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 ofANSI-C and implement the obligatory mouse-operated calculator, first forcursesand 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\u2019s 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\u2019s library for your developments should come a bit later.

An important part of this book is the enclosed source floppy\u2014 it has aDOSfile system containing a single shell script to create all the sources arranged by chapter. There is aReadMe file\u2014 consult it before you saymake. It is also quite instructive to use a program likediff and trace the evolution of the root classes andooc 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\u2014 and no, object-orientation will not replace sliced bread.

Hollage, October 1993
Axel-Tobias Schreiner
vii
___________________________________________________________________________
Contents
Preface
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5
1 Abstract Data Types \u2014 Information Hiding
.
.
.
.
.
.
.
.
.
.
.
1
1.1
Data Types
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1.2
Abstract Data Types
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1.3
An Example \u2014Set
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
1.4
Memory Management
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
1.5
Object
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
1.6
An Application
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4
1.7
An Implementation \u2014Set
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4
1.8
Another Implementation \u2014Bag
.
.
.
.
.
.
.
.
.
.
.
.
.
.
7
1.9
Summary
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
9
1.10 Exercises
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
9
2 Dynamic Linkage \u2014 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 \u2014String
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17
2.6
Another Implementation \u2014Atom
.
.
.
.
.
.
.
.
.
.
.
.
.
18
2.7
Summary
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
20
2.8
Exercises
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
20
3 Programming Savvy \u2014 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 \u2014 Code Reuse and Refinement
.
.
.
.
.
.
.
.
.
.
.
31
4.1
A Superclass \u2014Point
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
31
4.2
Superclass Implementation \u2014Point
.
.
.
.
.
.
.
.
.
.
.
.
32
4.3
Inheritance \u2014Circle
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
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 \u2014Circle
.
.
.
.
.
.
.
.
.
.
.
.
.
39

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->