Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword
Like this
4Activity
×
0 of .
Results for:
No results containing your search query
P. 1
Oops Using c

Oops Using c

Ratings: (0)|Views: 151|Likes:
Published by Karthik Juvvala

More info:

Categories:Topics, Art & Design
Published by: Karthik Juvvala on Jul 21, 2010
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

07/21/2010

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 — 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 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 theOld 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. 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 princi-ples 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. Pro-grammers 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.
ooc 
 
vi___________________________________________________________________________
Preface
(sorry) is pliable: we have made it, we understand it and can change it, and itwrites the
ANSI
-C code just like we would.The following chapters refine our technology. In chapter eight we add dynamictype checking to catch our mistakes earlier on. In chapter nine we arrange forautomatic initialization to prevent another class of bugs. Chapter ten introducesdelegates and shows how classes and callback functions cooperate to simplify, forexample, the constant chore of producing standard main programs. More chaptersare concerned with plugging memory leaks by using class methods, storing andloading structured data with a coherent strategy, and disciplined error recoverythrough a system of nested exception handlers.Finally, in the last chapter we leave the confines of
ANSI
-C and implement theobligatory mouse-operated calculator, first for
curses 
and then for the X WindowSystem. This example neatly demonstrates how elegantly we can design andimplement 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. Mostchapters suggest some exercises; however, they are not spelled out formally,because I firmly believe that one should experiment on one
s own. Because we arebuilding the techniques from scratch, I have refrained from making and using amassive class library, even though some examples could have benefited from it. Ifyou want to understand object-oriented programming, it is more important to firstmaster the techniques and consider your options in code design; dependence onsomebody 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
filesystem 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 instructiveto use a program like
diff 
and trace the evolution of the root classes and
ooc 
reportsthrough the later chapters.The techniques described here grew out of my disenchantment with C++ whenI needed object-oriented techniques to implement an interactive programminglanguage and realized that I could not forge a portable implementation in C++. Iturned to what I knew,
ANSI
-C, and I was perfectly able to do what I had to. I haveshown this to a number of people in courses and workshops and others have usedthe methods to get their jobs done. It would have stopped there as my footnote toa 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 allonce more). My thanks go to them and to all those who helped with and sufferedthrough the evolution of this book. Last not least I thank my family
and no,object-orientation will not replace sliced bread.Hollage, October 1993Axel-Tobias Schreiner
 
vii___________________________________________________________________________
Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . .
5
1 Abstract Data Types — InformationHiding
. . . . . . . . . . .
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
. . . . . . . . . . . . . . . . . . . . . .
9
2 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
. . . . . . . . . . . . . . . . . . . . . .
20
3 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
. . . . . . . . . . . . . . . . . . . . . .
29
4 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 
. . . . . . . . . . . . .
39

You're Reading a Free Preview

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