ebook img

Object Oriented Programming in C++ PDF

566 Pages·7.601 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Object Oriented Programming in C++

RICHARD JOHNSONBAUGH MARTIN KALIN Object-OnammiPnrgo gir riented C++ Object-Oriented Programming in C + + Richard Johnsonbaugh Martin Kalin DEPAUL UNIVERSITY PRENTICE HALL, Englewood Cliffs, New Jersey 07632 Cover photograph: Autograph score of Schubert’s Great C major Symphony, Gesellschaft der Musikfreunde. Used by permission Copyright © 1995 by Prentice-Hall, Inc. A Paramount Communications Company Englewood Cliffs, New Jersey 07632 All rights reserved. No part of this book may be reproduced in any form or by any means, without permission in writing from the publisher. Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 ISBN 0-05-3bDbfl2-7 Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty. Limited, Sydney Prentice-Hall Canada Inc., Toronto Prentice-Hall Hispanoamericana, S.A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Simon & Schuster Asia Pte. Ltd., Singapore Editora Prentice-Hall do brasil, Ltda., Rio de Janiero Trademark Notices IBM is a registered trademark of International Business Machines Corpora­ tion. UNIX is a registered trademark of AT&T Bell Laboratories. Borland C++, Turbo C, and Turbo C++ are registered trademarks of Bor­ land International, Inc. ,v'J. I { Ik Preface This book is based on C++ courses given by the authors at DePaul Univer­ sity for the last several years. The book can be used for self-study or a course on object-oriented programming in C++. We assume no prior knowledge of C++, but we do assume knowledge of C. This book is truly about C++, and not simply about the C subset of C++. Coverage of C at the level provided in R. Johnsonbaugh and M. Kalin, Applications Programming in ANSI C, 3rd ed. (New York: Macmillan, 1995) provides sufficient background for this book. In this book, as in our C books, we make extensive use of examples, figures, self-study exercises, sample applications, lists of common program­ ming errors, and programming exercises. We strive for clarity throughout the book and also illustrate a variety of good programming style possible in C++. Our aim is to make C++ the language of choice for those who need the power of object-oriented programming. This is the first introductory book on C++ that treats object-oriented design, emphasizes sound programming practices (see, e.g., Assertions and Program Correctness, Section 3.8), presents the C++ input/output class hierarchy (Chapter 7) in sufficient detail so that it can be used, and features major, useful examples (e.g., Measuring Computer Performance, Section 5.3, and A Random Access File Class, Section 7.6). C++ is an evolving language; hence, no final standard has yet been es­ tablished. The C++ presented in this book is based on the latest Working Paper for Draft Proposed International Standard for Information Systems— Programming Language C++ developed by the X3J16 standards committee of The American National Standards Institute (ANSI). The X3J16 commit­ tee is developing a definition of C++, which eventually will evolve into a proposed standard. The Working Paper is available from Standards Secretariat CBEMA 1250 Eye Street, NW Suite 200 Washington, DC 20005 viii PREFACE Overview During the 1980s and 1990s, C became the language of choice for many applications and systems programmers. Most major software available for personal computers is written in C: spreadsheets, word processors, databases, communications packages, statistical software, graphics packages, and so on. Virtually all software written for the UNIX environment is likewise written in C, and many mainframe systems meant to be ported from one platform to another are also coded in C. In the early 1980s, Bjarne Stroustrup of AT&T Bell Labs developed C++ as an extension of C that supports object- oriented programming, a type of programming that is well suited to the large, complex software systems now written for all platforms, from the cheapest personal computers to the most expensive mainframes. C++ also corrects some shortcomings in C, which C++ includes as a subset, and it supports abstract data types. C++’s popularity has grown in tandem with that of object-oriented design in software. Accordingly, we cover object-oriented design and C++ programming. C++ is a complex language. Fortunately, most C++ programmers can benefit from its power without mastering each and every one of its features. Because ours is an introductory book, we focus on the most useful aspects of the language; but we also cover all of the language’s constructs. We emphasize using C++ to write practical applications based on sound object- oriented design. The book includes the following features: • Examples and exercises that cover a wide range of applications. • Sample applications. • A broad variety of programming exercises. The book contains 59 pro­ gramming exercises. • End-of-chapter lists of common programming errors. • Discussion of the standard C++ input/output class library (Chapter 7). • Self-study exercises to enable readers to check their mastery of a section. The book contains nearly 500 such exercises. Answers to the odd- numbered exercises are in the back of the book. • Figures to facilitate the learning process. • Major data structures, including stacks (Section 3.2), strings (Section 4.1), binary search trees (Section 4.4), and files (Section 7.6), imple­ mented in C++. • A number of appendixes. • Understandable code. We opt for clarity over subtlety throughout the book. PREFACE ix • The latest C++ features including templates (Section 3.9), exception handling (Section 8.1), run-time type identification (Section 8.2), and namespaces (Section 8.3). • An introduction to the object-oriented languages Smalltalk, Eiffel, and Objective C (Section 8.4). Organization of the Book Chapter 1 introduces key concepts associated with object-oriented design and programming: classes, abstract data types, objects, inheritance, encap­ sulation, polymorphism, and others. The chapter contrasts object-oriented design with top-down functional decomposition and offers examples to illus­ trate the difference between the approaches. Chapter 2 discusses some important changes from C to C++. It also introduces C++ input/output so that the reader can begin using this class library right away. Chapter 7 goes into the details of C++ input/output. Chapters 3 and 4 are devoted to classes. Chapter 3 covers the basics so that the reader can begin using classes at once. The chapter explains how to declare classes; how to write constructors, destructors, and other methods; how to overload operators; and how to write friend functions. Chapter 3 introduces assertions as a tool to promote correctness in object-oriented programs. Chapter 3 also introduces templates and encourages their use for constructing generic classes. We believe that assertions and templates have not received adequate coverage in other C++ books, particularly given their power to promote the goals of object-oriented design. Chapter 4 expands on Chapter 3 and introduces new material as well. Constructors are studied closely, friend classes are presented, and static data members and meth­ ods are introduced. Chapters 3 and 4 rely heavily on examples to explain how classes may be used to implement abstract data types and to meet the object-oriented goal of encapsulation. Chapter 5 explains inheritance (including multiple inheritance) and poly­ morphism in depth. Through examples and sample applications (e.g., mea­ suring computer performance, polymorphic tree traversal), the chapter il­ lustrates basic programming techniques and also illustrates different styles of C++ programming. The chapter carefully explains the distinction be­ tween run-time and compile-time binding so that the reader is clear about the limits of polymorphism in C++. Chapter 6 is devoted to operator overloading. The chapter first reviews earlier treatments of the topic and then offers many examples and several sample applications to highlight the power of operator overloading. The chapter shows how to overload the subscript, function, memory management, preincrement, and postincrement operators among others. Chapter 7 serves two purposes. First, the chapter examines in detail the C++ input/output library so that the interested reader can exploit the powerful classes contained therein. This treatment culminates in a sample X PREFACE application that builds a random access file class through inheritance from a library file class. Second, the chapter uses the input/output library as a major, sophisticated example of object-oriented design realized in C++. Chapter 7 pays close attention to manipulators, which are powerful ways to do sophisticated input/output in C++. We believe Chapter 7 offers an unrivaled examination of C++’s input/output. Chapter 8 covers advanced topics such as exception handling, run-time type identification, and namespaces. These topics are recent additions to C++. The chapter also contrasts C++ with other object-oriented languages such as Eiffel, Objective C, and Smalltalk. The chapter concludes with a discussion of the future of object-oriented design and programming. This book is devoted to C++ independent of any particular operating system. However, the basic commands needed to run C++ programs are given in Appendixes C and D for the UNIX g++ and Borland C++ compilers. In addition, Appendix C contains an extended discussion of UNIX. We rely heavily on short examples, pictures, tables, and other figures to illustrate specific points about the syntax and semantics of C++. From our experience in teaching C++ and other languages, we are convinced that no single method is appropriate for clarifying every aspect about a language. Most of our students agree with us that learning and using C++ is fun. We have tried to incorporate this view by using interesting examples, sample problems, programming exercises, and short slices of code. Chapter Structure The chapters are organized as follows: Contents Overview Section Section Exercises Section Section Exercises Common Programming Errors Programming Exercises In Chapters 3 through 7, several sections are devoted to sample applica­ tions. Each of these sections contains a statement of a problem, a solution to the problem, and an implementation of a solution to the problem in C++. Many of these sections conclude with an extended discussion. The sample applications include the following: • A stack class (Section 3.2) • A zip code class (Section 3.4) • A complex number class (Section 3.6) PREFACE xi • A string class (Section 4.1) • A binary search tree class (Section 4.4) • Iterators (Section 4.5) • Measuring computer performance (Section 5.3) • An associative array (Section 6.4) • Random access files (Section 7.6) The Common Programming Errors sections highlight those aspects of the language that are easily misunderstood. The book contains 59 programming exercises drawn from a wide variety of applications. Exercises The book contains nearly 600 section review exercises, the answers to which are short answers, code fragments, and, in a few cases, entire programs. These exercises are suitable as homework problems or as self-tests. The answers to the odd-numbered exercises are in the back of the book, and the answers to the even-numbered exercises are in the Instructor’s Guide. Our experience teaching C++ has convinced us of the importance of these exercises. The applications covered in the programming exercises at the ends of the chapters include the following: • Semaphores (Programming Exercise 3.7) • Local area networks (Programming Exercises 4.3 and 5.2) • Scheduling (Programming Exercise 4.7) • Graphs (Programming Exercises 4.9 and 5.6) • Spreadsheets (Programming Exercises 4.8 and 6.9) • Indexed files (Programming Exercise 7.5) Not every reader will be interested in all of these applications; however, we think that it is important to show the variety of problems that C++ can address. Appendixes Four appendixes are provided for reference. Appendix A contains the ASCII table. Appendix B contains a list of some of the most useful C++ functions and class methods. We describe the parameters and return values for each, the header file to include (when specified), and what the function or method does. For those readers using C++ under UNIX, we have included Appendix C. We describe the GNU C++ compiler command g++ in some detail. In ad­ dition to the g++ command, we also discuss the make utility, the file system, commands for handling files (e.g., Is, cp), directories and several commands

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.