What Is Object-Oriented Programming?
Total Page:16
File Type:pdf, Size:1020Kb
What is Object-Oriented Programming9 Bjmne StMStNp,AT&T Bell Laboratories Ohjedenkntedhas ot all programminglanguagescan duce C++and partly because C++is one of be object-oriented. Yet, claims the few languages that supports data ah become a buzzword have been made that APL, Ada, straction, object-oriented programming, that implies “good” Clu, C++,Iaops, and Smalltalk are object- and traditional programming techniques. proghmming But oriented languages. I have heard discus- I do not cover issues of concurrency and sions of object-oriented design in C, Pas- hardware support for specific,higher level when it comes to cal, Modula-2, and Chili. Could there language constructs. really supporting this somewhere be proponents of object- oriented programming in Fortran and Programming paradigms pardighm, not all Cobol? I think there must be. Object-oriented programming is a tech- lameesare equal. “Object-oriented” has become a high- nique - a paradigm for writing “good” tech synonym for “good.” Articles in the programs for a set of problems. If the term trade presscontain arguments that appear “object-oriented language” means any- to boil down to syllogisms like: thing, it must mean a language that has Ada is good; object-oriented is good; mechanisms that support the object- thert$oore, Ada is object+niated. oriented style of programming well. There is an important distinction here: This article presents my view ofwhat ob ject-oriented means in the context of a A language suppurtsa programming style if it provides facilities that make it con- general-purpose programming language. venient (reasonably easy, safe, and efi- I present examples in C++,partly to intre cient) to use that style. A language does not support a technique if it takes excep An earlier version of this article appeared in /kr.Fmt tional effort or skill to write such pro- Etrmpnii G7$ on O~Prl-(~i~/k~~rntnr,lR.Spnnger- Verlag. NewYork. 1987.p~.51-70. grams; in that case, the language merely 10 0740-7459/88/05OO/OO10/$01 .OO 01988 IEEE IEEE Software wrcilhc prograinmcrs to use the techniqiie. that has found itsway into the compiler or ' Procedul-al programming uses frrnc- For examplc,you can wire structured pro- the literature. tions tv ci-catc oi-dcr in a maze of alp grains in Fortr;in and type-sccurc pro- , rithms. grams in C, and yo~ican use data ahstrac- Procedural. The original - arid pi-ob 1 tioii in Moclul>tZ, but it is iinnecessarily ably still the most cvnirnoii - program- Data hiding. Over the years, the empha- hard to do so because those languages do rning paradigm is: sis in the progi-am design has shifted from not .support those techniques. , procedure design to data organization. Dmdr rohirh prowilurrs you want: ILW lhr ~ Among ot~ici-things, this reflects an in- Support for a pidigin conics not only Imt iilgonlhms ym cnn,/ind. in the obvious foi-in of language facilities crc'ae in prograni size. '4set ofrelated pre that let you use the paradigm directly, bur The foC1l.S is on proccdnrc design -the ~ cedures and the data they manipulate is also in the niorc subtle forms of compilc- algorichtn needed to pelform the desired often called ii ~notlule.The programming tiin<=aiid runtime checks foi- uniiiten- computation. Iaiguages support this par- pal-adigniis: adigm with facilities for passing arguments tional deviations f'rom the paradigm. Type Zlrridr ~i~hi~hmoduIr.rycnL w(m~;pn?lzlion to fnnctions and retLlrningvalues checking, ambiguity detection, and run- theprop-(on ,si) thnt dntn is hiddm in functions. The literature about this para- time checks are an examples of linguistic 1 moduh. support for paradigms. Extralinguistic digin is filled with discussions of how to This paradigm is known the data-hid- facilities such as standard librarics and as ing principle. U%en procedures do not pi-ograinrningeinironments can also prw need to be grouped with related data, the \idr significant support for paradigms. A languee does not procedural style suffices. In fact, the tech- One language is not nccessarily better sumta fechique if niques for designing good procedures are than another because it a feature the has still applied, now to each procedure in a other does not - therc are many ex- it takes exceptional module. amples to the contrary. The impormiit emrt or skill to write The most coninion example ofdata hid- issue is not how many features a language such proglams. ing is a definition of :I stack module. A has, but that the features it docs have are good solution requires sufficient to support the desired program- a user interface for the stack (for ex- ming styles in the desired application pass arguments, how to distinguish differ- ample, thefLinctionspustl() and pop()), areas. Specifically, it is important that: ent kinds ofargumcnts and different kinds that the stack representation (for ex- of functions (procedures, routines, mac- All features are cleanly and elegantly ample, a vector of elements) can be ros, etc.),and so on. integrated into the language. accessed only through this user interface, Fortran is the original procedut al lan- It is possible to cornhilie features to and guage; Algold0,Algol-68, C, and Pascal are achieve solutions that would have other- that the stack is initialized before its later inventions in the same tradition. wise required extra, separate features. first use. An example of good procedural style is a There arc as few spurious arid special- A plausibly external interface for astack square-root function. Given an argument, pirposc features as possible. module is the function neatly produces a result. To Implementing a feature does not im- do so, it performs a well-understood math- ,(I declai-ation of tlic inteifare of module pose significant overhead on programs ematical computation. // stackol characters that do not require it. chai-pop(): A user need only know about the lan- void push (char): guage subset used explicitly to write a pro- double sqrt(doublc arg) const sr-nck-cife = 100; gram. I // the rode for calcdating a squarv 1-oot Assuming this interface is found in a file The last two principles can be suni- I called stark.h, the intemalscan bedefined marized as "what you don't know won't like this: hurt you." If. there are any doubts about void some-function () I #include "5utk.h" the usefulness ofa feature, it is better left double root2 = sqrt (2); static charv[stack-siie]: // "static" means out. It is much easier to add a feature to a // ... // local to this language than to remove or modifv one I // file/rnodule May 1988 11 static char* p = v; // the stack is initially a type-manager module. Ifyouwanted two fined type.* The programming paradigm // empty stacks, you would define a stack-manager becomes: char pop() module with an interface like this: I Decide which types you want; plovide afull // check for underflow and pop // stack-id is a type; no details about set ofoperationsfor each type. I // stacks or stackjds are known here: void push (char c) class stack-id; When there isnoneedformore thatone I // make a stack and return its identifier: object of a type, the data-hiding program- // check for overflow and push stack-id create-stack(int size); ming style using modules suffices. Arith- I // call when stack is no longer needed: metic types such as rational and complex It is quite feasible to change this stack destroy-stack(stack-id); void push(stack-id, char); numbers are common examples of user- representation to a linked list. The user char pop(stack-id); defined types: does not have access to the representation This is certainly a great improvement class complex 1 anyway (because v and p were declared double re, im; static - that is, local to the file or module over the traditional unstructured mess, public: in which theyweredeclared). Such astack but "types" implemented this way are complex(doub1e r, double i) { re=r; im=i; 1 can be used like this: clearly very different from the types built // float->complex conversion: into a language. complex(doub1e r) { re=r; im=O; 1 #include "stack.h" In most important aspects, a type friend complex operator+ void some-function() (complex, complex); I created through a module mechanism is // binary minus: chart= pop(push('c')); different from a built-in type and enjoys in- friend complex operator- if (c != 'c') error("impossib1e"); ferior support: Each type-manager mod- (complex, complex); I ule must define a separate mechanism for // unary minus: friend complex operator-(complex) ; creatingvariables of its type, there is noes As originallydefined, Pascal doesn't pro- friend complex operatort vide satisfactory facilities for such group tablished norm forassigningobject identi- (complex, complex); ing: The only way to hide a name from the fiers, a variable of such a type has no name friend complex operator/ (complex, complex); rest of the program is to make it local to a known to the compiler or programming environment, and such variables do not // ... procedure. This leads to strange proce- I dure nestings and overreliance on global obey the usual scope and argument-pass data. ing rules. The declaration of the complex class Cfaressomewhatbetter.Asshownin the For example: (the userdefined type) specifiesthe repre- example, you can define a module by sentation ofacomplexnumberand theset void f() of operations on a complex number. The grouping related function and data defini- I tions in a single source file. The program- stack-idsl; representation is @'vu& that is, re and im mer can then control which names are stack-id s2; are accessible only to the functions seen by the rest of the program (a name specified in the declaration of class com- SI = create-stack(200); plex.