Why C++ Isn't Just an Object-Oriented Programming Language

Total Page:16

File Type:pdf, Size:1020Kb

Why C++ Isn't Just an Object-Oriented Programming Language 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.
Recommended publications
  • PHP Programming Cookbook I
    PHP Programming Cookbook i PHP Programming Cookbook PHP Programming Cookbook ii Contents 1 PHP Tutorial for Beginners 1 1.1 Introduction......................................................1 1.1.1 Where is PHP used?.............................................1 1.1.2 Why PHP?..................................................2 1.2 XAMPP Setup....................................................3 1.3 PHP Language Basics.................................................5 1.3.1 Escaping to PHP...............................................5 1.3.2 Commenting PHP..............................................5 1.3.3 Hello World..................................................6 1.3.4 Variables in PHP...............................................6 1.3.5 Conditional Statements in PHP........................................7 1.3.6 Loops in PHP.................................................8 1.4 PHP Arrays...................................................... 10 1.5 PHP Functions.................................................... 12 1.6 Connecting to a Database............................................... 14 1.6.1 Connecting to MySQL Databases...................................... 14 1.6.2 Connecting to MySQLi Databases (Procedurial).............................. 14 1.6.3 Connecting to MySQLi databases (Object-Oriented)............................ 15 1.6.4 Connecting to PDO Databases........................................ 15 1.7 PHP Form Handling................................................. 15 1.8 PHP Include & Require Statements.........................................
    [Show full text]
  • Thriving in a Crowded and Changing World: C++ 2006–2020
    Thriving in a Crowded and Changing World: C++ 2006–2020 BJARNE STROUSTRUP, Morgan Stanley and Columbia University, USA Shepherd: Yannis Smaragdakis, University of Athens, Greece By 2006, C++ had been in widespread industrial use for 20 years. It contained parts that had survived unchanged since introduced into C in the early 1970s as well as features that were novel in the early 2000s. From 2006 to 2020, the C++ developer community grew from about 3 million to about 4.5 million. It was a period where new programming models emerged, hardware architectures evolved, new application domains gained massive importance, and quite a few well-financed and professionally marketed languages fought for dominance. How did C++ ś an older language without serious commercial backing ś manage to thrive in the face of all that? This paper focuses on the major changes to the ISO C++ standard for the 2011, 2014, 2017, and 2020 revisions. The standard library is about 3/4 of the C++20 standard, but this paper’s primary focus is on language features and the programming techniques they support. The paper contains long lists of features documenting the growth of C++. Significant technical points are discussed and illustrated with short code fragments. In addition, it presents some failed proposals and the discussions that led to their failure. It offers a perspective on the bewildering flow of facts and features across the years. The emphasis is on the ideas, people, and processes that shaped the language. Themes include efforts to preserve the essence of C++ through evolutionary changes, to simplify itsuse,to improve support for generic programming, to better support compile-time programming, to extend support for concurrency and parallel programming, and to maintain stable support for decades’ old code.
    [Show full text]
  • Object-Oriented Programming Basics with Java
    Object-Oriented Programming Object-Oriented Programming Basics With Java In his keynote address to the 11th World Computer Congress in 1989, renowned computer scientist Donald Knuth said that one of the most important lessons he had learned from his years of experience is that software is hard to write! Computer scientists have struggled for decades to design new languages and techniques for writing software. Unfortunately, experience has shown that writing large systems is virtually impossible. Small programs seem to be no problem, but scaling to large systems with large programming teams can result in $100M projects that never work and are thrown out. The only solution seems to lie in writing small software units that communicate via well-defined interfaces and protocols like computer chips. The units must be small enough that one developer can understand them entirely and, perhaps most importantly, the units must be protected from interference by other units so that programmers can code the units in isolation. The object-oriented paradigm fits these guidelines as designers represent complete concepts or real world entities as objects with approved interfaces for use by other objects. Like the outer membrane of a biological cell, the interface hides the internal implementation of the object, thus, isolating the code from interference by other objects. For many tasks, object-oriented programming has proven to be a very successful paradigm. Interestingly, the first object-oriented language (called Simula, which had even more features than C++) was designed in the 1960's, but object-oriented programming has only come into fashion in the 1990's.
    [Show full text]
  • The Machine That Builds Itself: How the Strengths of Lisp Family
    Khomtchouk et al. OPINION NOTE The Machine that Builds Itself: How the Strengths of Lisp Family Languages Facilitate Building Complex and Flexible Bioinformatic Models Bohdan B. Khomtchouk1*, Edmund Weitz2 and Claes Wahlestedt1 *Correspondence: [email protected] Abstract 1Center for Therapeutic Innovation and Department of We address the need for expanding the presence of the Lisp family of Psychiatry and Behavioral programming languages in bioinformatics and computational biology research. Sciences, University of Miami Languages of this family, like Common Lisp, Scheme, or Clojure, facilitate the Miller School of Medicine, 1120 NW 14th ST, Miami, FL, USA creation of powerful and flexible software models that are required for complex 33136 and rapidly evolving domains like biology. We will point out several important key Full list of author information is features that distinguish languages of the Lisp family from other programming available at the end of the article languages and we will explain how these features can aid researchers in becoming more productive and creating better code. We will also show how these features make these languages ideal tools for artificial intelligence and machine learning applications. We will specifically stress the advantages of domain-specific languages (DSL): languages which are specialized to a particular area and thus not only facilitate easier research problem formulation, but also aid in the establishment of standards and best programming practices as applied to the specific research field at hand. DSLs are particularly easy to build in Common Lisp, the most comprehensive Lisp dialect, which is commonly referred to as the “programmable programming language.” We are convinced that Lisp grants programmers unprecedented power to build increasingly sophisticated artificial intelligence systems that may ultimately transform machine learning and AI research in bioinformatics and computational biology.
    [Show full text]
  • Preview Fortran Tutorial (PDF Version)
    Fortran i Fortran About the Tutorial Fortran was originally developed by a team at IBM in 1957 for scientific calculations. Later developments made it into a high level programming language. In this tutorial, we will learn the basic concepts of Fortran and its programming code. Audience This tutorial is designed for the readers who wish to learn the basics of Fortran. Prerequisites This tutorial is designed for beginners. A general awareness of computer programming languages is the only prerequisite to make the most of this tutorial. Copyright & Disclaimer Copyright 2017 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] i Fortran Table of Contents About the Tutorial ····································································································································i Audience ··················································································································································i
    [Show full text]
  • Ali Aydar Anita Borg Alfred Aho Bjarne Stroustrup Bill Gates
    Ali Aydar Ali Aydar is a computer scientist and Internet entrepreneur. He is the chief executive officer at Sporcle. He is best known as an early employee and key technical contributor at the original Napster. Aydar bought Fanning his first book on programming in C++, the language he would use two years later to build the Napster file-sharing software. Anita Borg Anita Borg (January 17, 1949 – April 6, 2003) was an American computer scientist. She founded the Institute for Women and Technology (now the Anita Borg Institute for Women and Technology). While at Digital Equipment, she developed and patented a method for generating complete address traces for analyzing and designing high-speed memory systems. Alfred Aho Alfred Aho (born August 9, 1941) is a Canadian computer scientist best known for his work on programming languages, compilers, and related algorithms, and his textbooks on the art and science of computer programming. Aho received a B.A.Sc. in Engineering Physics from the University of Toronto. Bjarne Stroustrup Bjarne Stroustrup (born 30 December 1950) is a Danish computer scientist, most notable for the creation and development of the widely used C++ programming language. He is a Distinguished Research Professor and holds the College of Engineering Chair in Computer Science. Bill Gates 2 of 10 Bill Gates (born October 28, 1955) is an American business magnate, philanthropist, investor, computer programmer, and inventor. Gates is the former chief executive and chairman of Microsoft, the world’s largest personal-computer software company, which he co-founded with Paul Allen. Bruce Arden Bruce Arden (born in 1927 in Minneapolis, Minnesota) is an American computer scientist.
    [Show full text]
  • Php Tutorial
    PHP About the Tutorial The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web-based software applications. This tutorial will help you understand the basics of PHP and how to put it in practice. Audience This tutorial has been designed to meet the requirements of all those readers who are keen to learn the basics of PHP. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of computer programming, Internet, Database, and MySQL. Copyright & Disclaimer © Copyright 2016 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected] i PHP Table of Contents About the Tutorial ...........................................................................................................................................
    [Show full text]
  • Teach Yourself Perl 5 in 21 Days
    Teach Yourself Perl 5 in 21 days David Till Table of Contents: Introduction ● Who Should Read This Book? ● Special Features of This Book ● Programming Examples ● End-of-Day Q& A and Workshop ● Conventions Used in This Book ● What You'll Learn in 21 Days Week 1 Week at a Glance ● Where You're Going Day 1 Getting Started ● What Is Perl? ● How Do I Find Perl? ❍ Where Do I Get Perl? ❍ Other Places to Get Perl ● A Sample Perl Program ● Running a Perl Program ❍ If Something Goes Wrong ● The First Line of Your Perl Program: How Comments Work ❍ Comments ● Line 2: Statements, Tokens, and <STDIN> ❍ Statements and Tokens ❍ Tokens and White Space ❍ What the Tokens Do: Reading from Standard Input ● Line 3: Writing to Standard Output ❍ Function Invocations and Arguments ● Error Messages ● Interpretive Languages Versus Compiled Languages ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 2 Basic Operators and Control Flow ● Storing in Scalar Variables Assignment ❍ The Definition of a Scalar Variable ❍ Scalar Variable Syntax ❍ Assigning a Value to a Scalar Variable ● Performing Arithmetic ❍ Example of Miles-to-Kilometers Conversion ❍ The chop Library Function ● Expressions ❍ Assignments and Expressions ● Other Perl Operators ● Introduction to Conditional Statements ● The if Statement ❍ The Conditional Expression ❍ The Statement Block ❍ Testing for Equality Using == ❍ Other Comparison Operators ● Two-Way Branching Using if and else ● Multi-Way Branching Using elsif ● Writing Loops Using the while Statement ● Nesting Conditional Statements ● Looping Using
    [Show full text]
  • Bjarne Stroustrup
    Bjarne Stroustrup 52 Riverside Dr. #6A +1 979 219 5004 NY, NY 10024 [email protected] USA www.stroustrup.com Education Ph.D. in Computer Science, University of Cambridge, England, 1979 Ph.D. Thesis: Communication and Control in Distributed Computer Systems Thesis advisor: David Wheeler Cand.Scient. in Mathematics with Computer Science, Aarhus University, Denmark, 1975 Thesis advisor: Brian H. Mayoh Research Interests Distributed Systems, Design, Programming techniques, Software development tools, and Programming Languages Professional Experience Technical Fellow, Morgan Stanley, New York, January 2019 – present Managing Director, Division of Technology and Data, Morgan Stanley, New York, January 2014 – present Visiting Professor, Columbia University, New York, January 2014 – present Visiting Professor in the Computer Lab and Fellow of Churchill College, Cambridge University, Spring 2012 Visiting Professor in the Computer Science Department, Princeton University, Fall 2011 The College of Engineering Chair Professor in Computer Science, Department of Computer Science, Texas A&M University, October 2002 – January 2014 Department Head, AT&T Laboratories – Research, Florham Park, New Jersey, July 1995 – October 2002 Distinguished Member of Technical Staff, AT&T Bell Laboratories, Murray Hill, NJ, June 1987 – July 1995 Member of Technical Staff, AT&T Bell Laboratories, Murray Hill, NJ, March 1979 – June 1987 Honors & Awards 2019: Honorary doctor of University Carlos III in Madrid, Spain. 1 2018: The John Scott Legacy Medal and Premium from The Franklin Institute and the City Council of Philadelphia to men and women whose inventions improved the comfort, welfare, and happiness of human kind in a significant way. 2018: The Computer Pioneer Award from The IEEE Computer Society For bringing object- oriented programming and generic programming to the mainstream with his design and implementation of the C++ programming language.
    [Show full text]
  • CSC 272 - Software II: Principles of Programming Languages
    CSC 272 - Software II: Principles of Programming Languages Lecture 1 - An Introduction What is a Programming Language? • A programming language is a notational system for describing computation in machine-readable and human-readable form. • Most of these forms are high-level languages , which is the subject of the course. • Assembly languages and other languages that are designed to more closely resemble the computer’s instruction set than anything that is human- readable are low-level languages . Why Study Programming Languages? • In 1969, Sammet listed 120 programming languages in common use – now there are many more! • Most programmers never use more than a few. – Some limit their career’s to just one or two. • The gain is in learning about their underlying design concepts and how this affects their implementation. The Six Primary Reasons • Increased ability to express ideas • Improved background for choosing appropriate languages • Increased ability to learn new languages • Better understanding of significance of implementation • Better use of languages that are already known • Overall advancement of computing Reason #1 - Increased ability to express ideas • The depth at which people can think is heavily influenced by the expressive power of their language. • It is difficult for people to conceptualize structures that they cannot describe, verbally or in writing. Expressing Ideas as Algorithms • This includes a programmer’s to develop effective algorithms • Many languages provide features that can waste computer time or lead programmers to logic errors if used improperly – E. g., recursion in Pascal, C, etc. – E. g., GoTos in FORTRAN, etc. Reason #2 - Improved background for choosing appropriate languages • Many professional programmers have a limited formal education in computer science, limited to a small number of programming languages.
    [Show full text]
  • Academic Program Review
    Academic Program Review April 16-18, 2012 Department of Computer Science and Engineering Room 301 Harvey R. Bright Building Texas A&M University College Station, Texas 1 Contents I Introduction ............................................................................................................................... 4 I.1 Charge to Review Committee ............................................................................................. 4 I.2 Schedule of Review/Itinerary ............................................................................................. 5 I.3 Administrative Structure .................................................................................................... 6 II Brief History ............................................................................................................................... 7 II.1 Founding of Department .................................................................................................... 7 II.2 Founding and Development of Related Centers ................................................................ 7 II.3 Review and Changes in Past Seven Years ........................................................................ 15 II.4 Date of Last Program Review ........................................................................................... 16 III Vision and Goals ................................................................................................................... 16 III.1 Strategic Plan ...................................................................................................................
    [Show full text]
  • A History of C++: 1979− 1991
    A History of C++: 1979−1991 Bjarne Stroustrup AT&T Bell Laboratories Murray Hill, New Jersey 07974 ABSTRACT This paper outlines the history of the C++ programming language. The emphasis is on the ideas, constraints, and people that shaped the language, rather than the minutiae of language features. Key design decisions relating to language features are discussed, but the focus is on the overall design goals and practical constraints. The evolution of C++ is traced from C with Classes to the current ANSI and ISO standards work and the explosion of use, interest, commercial activity, compilers, tools, environments, and libraries. 1 Introduction C++ was designed to provide Simula’s facilities for program organization together with C’s effi- ciency and flexibility for systems programming. It was intended to deliver that to real projects within half a year of the idea. It succeeded. At the time, I realized neither the modesty nor the preposterousness of that goal. The goal was modest in that it did not involve innovation, and preposterous in both its time scale and its Draco- nian demands on efficiency and flexibility. While a modest amount of innovation did emerge over the years, efficiency and flexibility have been maintained without compromise. While the goals for C++ have been refined, elaborated, and made more explicit over the years, C++ as used today directly reflects its original aims. This paper is organized in roughly chronological order: §2 C with Classes: 1979– 1983. This section describes the fundamental design decisions for C++ as they were made for C++’s immediate predecessor. §3 From C with Classes to C++: 1982– 1985.
    [Show full text]