Why C++ Isn't Just an Object-Oriented Programming Language
Total Page:16
File Type:pdf, Size:1020Kb
Why C++ is not just an Object-Oriented Programming Language Bjarne Stroustrup AT&T Bell Laboratories Murray Hill, New Jersey 07974 ABSTRACT C++ directly supports a variety of programming styles. In this, C++ deliber- ately differs from languages designed to support a single way of writing pro- grams. This paper briefly presents key programming styles directly supported by C++ and argues that the support for multiple styles is one of its major strengths. The styles presented include: traditional C-style, concrete classes, abstract classes, traditional class hierarchies, abstract classes and class hierarchies, and generic programming. To provide a context for this overview, I discuss criteria for a reasonable and useful definition of ‘‘object-oriented programming.’’ 1 Introduction ‘‘object-oriented programming,’’ ‘‘object-oriented analysis,’’ ‘‘object-oriented design,’’ ‘‘object- There are many tools and techniques that can help in oriented technology,’’ etc., is, however, a burning our effort to build useful, economical, and maintain- issue for people who want to turn the oft-repeated able systems. To complete ambitious and complex promises made for techniques and languages called projects, we rely on a wide variety of techniques and ‘‘object-oriented’’ into reality in everyday projects. tools that must work together. It has become a practical rather than academic topic The title of this paper singles out a programming of discussion. What is ‘‘object-oriented technol- language². However, the real topic is programming, ogy?,’’ what benefits can be expected from it? at or if you prefer a longer formulation, the design and what risks?, how do those techniques, benefits, and implementation of systems. A programming lan- risks compare with those associated with alterna- guage is just one of the means by which we try to tives? achieve our goals. A systems builder trying to explain to an accoun- The definition of ‘‘object-oriented program- tant why money should be spent for tools supporting ming’’ is no longer a popular topic of discussion at object-oriented techniques needs more than a state- major conferences. A practical definition of ment to the effect that ‘‘object-oriented is great’’ or that ‘‘really great techniques are really object- oriented.’’ You simply cannot ask someone to bet their company’s future on vague promises phased in ill-defined terms. Nor is a well-polished and logi- ________________ cally coherent semi-mathematical treatment of the ² This paper is primarily based on an invited talk with the same title given at OOPSLA’95 in Austin Texas. The style of this paper is clearly affected by its origins as a relatively short talk. I would have preferred this paper to be either much longer or much shorter, but I did not have the time to do either. subject of direct practical use. contradictory definitions of ‘‘object oriented’’ in We need to define ‘‘object-oriented’’ to be some- use. However, the mainstream usage stems directly thing specific so that we can point out specific bene- from the ideas pioneered by programming language fits and risks associated with its use. We must also Simula and the design techniques it was developed be specific about what is not object-oriented, and to support. The communities of programmers and what benefits and lack of benefits we can expect designers centered around languages such as C++, from various non-object-oriented techniques. CLOS, Eiffel, Object Pascal, and Smalltalk have Consequently, this paper starts out discussing contributed much to this tradition. what makes a good definition of ‘‘object-oriented.’’ A meaningful definition of any concept must Next, I present a range of useful techniques which exclude something. may or may not be object oriented and discuss their advantages and disadvantages. 3 A Broad Definition of ‘‘Object-oriented’’ Given these general criteria for a definition of 2 Defining ‘‘Object-oriented’’ ’’object-oriented’’ you can find several plausible To be useful and intellectually honest, a definition of candidates, and several communities have their own ‘‘object-oriented’’ must definitions. However, I suggest we stick to the tradi- [1] not be a mere synonym for ‘‘good,’’ tional definition of object-oriented used within broad [2] not exclude most accepted meanings, communities of programmers. A language or tech- [3] have a firm historical basis, nique is object-oriented if and only if it directly sup- [4] exclude something. ports: Not everything good is object- oriented, and not [1] Abstraction ± providing some form of classes everything object-oriented is good. I think I can and objects. support both claims from experience. I have seen [2] Inheritance ± providing the ability to build examples of the latter often enough: it is not uncom- new abstractions out of existing ones. mon to find programs that apply techniques usually [3] Run-time polymorphism ± providing some deemed object-oriented extensively or even exclu- form of run-time binding. sively, yet are hard to comprehend, hard to maintain, This definition includes all major languages com- and perform abysmally. Such examples occur in monly referred to as object-oriented: Ada95, Beta, every programming language. But then, of course, C++, CLOS, Eiffel, Simula, Smalltalk, and many some people respond ‘‘that just proves that the pro- other languages fit this definition. Classical pro- gram wasn’t truly object-oriented.’’ To which the gramming languages without classes, such as C, For- answer must be that either the term has become tran4, and Pascal, are excluded. Languages that lack meaningless or there must be something good direct support for inheritance or run-time binding, beyond what is called ‘‘object-oriented.’’ such as Ada88 and ML are also excluded. On the other hand, when we define ‘‘object- ML is a good example of something that is good oriented,’’ we must not be too exclusive. Object- but not object-oriented. I like ML; it is an interest- oriented programming is a broad intellectual disci- ing, innovative, and powerful language, but it is pline, not the mere use of specific language features. functional rather than object-oriented and its poly- Attempts to define ‘‘object-oriented’’ to mean morphism is resolved at compile time rather than at ‘‘what I’m selling’’ are not uncommon, but are fun- run-time. Thus, saying that ML isn’t object-oriented damentally sleazy. is not a criticism, it’s an observation about defini- Any definition of ‘‘object-oriented’’ should be tions and the nature of ML. historically reasonable. Words are only useful for Techniques and tools are object-oriented if and communication, really only mean something, if we only if they support the use of object-oriented pro- agree on a meaning for them. There are several gramming. For example, a design method is object- plausible, logically coherent, and mutually oriented if its regular and proper use leads to programs that exploit abstraction, inheritance, and provides features deemed non-object-oriented must polymorphism where appropriate. I strongly prefer be worse than a language that does not. People who design methods that directly and naturally support like a language to support classes as part of a hierar- the use of at least one of the major object-oriented chy only and functions/methods attached to one spe- languages supporting in ways that exploit its features cific class only often call such a language ‘‘a pure in an idiomatic way. object-oriented language.’’ If we don’t like the idea For example, it is often possible to simplify of restricting the definition of classes and functions application code by hiding objects with different rep- that way we can call such a language ‘‘just an resentations and different implementation details object-oriented programming language.’’ behind a common ‘‘abstract’’ interface (see §6.4 and I prefer to have more facilities available than can §6.6). Conversely, the implementation of related be provided by methods defined on classes within a concepts can often be greatly simplified by exploit- single hierarchy. A lot of good design goes beyond ing commonality through inheritance (see §6.5 and that relatively narrow domain. Incidentally, I have §6.6). A major purpose of design methods and the come to dislike the adjective ‘‘hybrid’’ as used to CASE tools that commonly support them is to make distinguish ‘‘pure’’ object-oriented systems from design simpler, more regular, and more predictable. others. Too often, ‘‘hybrid’’ is used in a prejudicial Thus, to earn the label ‘‘object-oriented,’’ a design manner. If I must apply a descriptive label, I use the method must regularly and predictably help the dis- phrase ‘‘multi-paradigm language’’ to describe C++. covery of commonality that can be exploited in these ways. Ideally, an object-oriented design method 4.1 Use of Language Features must strongly encourage the expression of this com- Even when all the features required to support monality using the most appropriate facilities in one object-oriented programming are available, you or more of the languages supporting object-oriented don’t need to use them all the time. Some classes programming. Minimally, the method and its sup- just don’t belong in a hierarchy and some functions porting tools must not be a hindrance to the use of don’t belong to any particular object. object-oriented facilities in the programming lan- The key to maintainable, efficient, and evolvable guage used to implement the design. Much confu- programs isn’t particular language features. It is the sion arise because not every design method that ability to develop concepts needed for a solution and claims to be object-oriented does that. to express them clearly in a program. Language fea- Please remember that I’m looking for a practical tures exist to make such expression simple and understanding of the notion of ‘‘object-oriented’’ direct. rather than a formal definition.