ebook img

Mastering C++: An Introduction to C++ and Object-Oriented Programming for C and Pascal Programmers PDF

292 Pages·39.427 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 Mastering C++: An Introduction to C++ and Object-Oriented Programming for C and Pascal Programmers

EH a** C++ fete?-. ':*- Sfe CAY HIIHSTMANN S. r C++ MASTERING C++ MASTERING An Introduction to C++ and Object-oriented Programming for C and Pascal Programmers CAY HORSTMANN S. San Jose State University JOHN WILEY & SONS New York Chichester Brisbane Toronto Singapore Copyright © 1991 byJohn Wiley & Sons, Inc. All rights reserved. Published simultaneously in Canada. Reproduction or translation ofany part of this work beyond that permitted by Sections 107and 108 of the 1976 United States Copyright Actwithout the permission ofthe copyright owner is unlawful. Requests for permission or further information should be addressed to the Permissions Department, John Wiley & Sons. Library ofCongress Cataloging-in-Publication Data: Horstmann, Cay S., 1959- Mastering C++ an introduction to C+ + and object-oriented : programming for C and Pascal programmers / Cay S. Horstmann. Includes bibliographical references. ISBN 0-471-52257-0 1. C+ + (Computerprogramlanguage) 2. Object-orientedprogramming (Computer science) I. Title. QA76.73.C153H67 1991 005.26'2-dc20 90-37691 CIP Printed in the United States ofAmerica 10987654321 . PREFACE C++ is a superset of the hugely successful C programming language. (The + + in its name originates from the C increment operator + +.) C++ of- fers enhancements of C in four directions: Operator and function overloading Information hiding Inheritance Virtual functions (polymorphism) Thisbookcontainsa thoroughintroductionto C++ withparticularemphasis , on techniques used in real-life programs. This book does not require knowledge of the C programming language. The reader familiar with Pascal or Modula-2 will find numerous "Pascal Notes" to aid in the transition to the C/C++ syntax. The reader who al- ready knows C can skim through Chapters 2, 3, 4, and 6 and watch for "C Notes", which point out differences between C and C++ In this book, I attempt to show C++ in the context of realistic program- ming situations. I purposely stay away from contrived examples. There are no data types for small appliances, furry animals, or edible objects. No ob- jects are called my.stack, no data types derived_l when examples involving useful data structures can be easily given. It is easy for students (as well as for some authors) to think they know a programming language when all they actually know is its syntax. I place little emphasis on syntax and mechanics; instead, I try to develop the ideas underlying the language concepts and show them in the context of realistic examples. The policy of supplying realistic examples starts with Chapter 1, which gives a quick tour of two exciting capabilities of C++ operator overloading : and virtual functions. (The introduction of classes is deferred until Chapter 5 because of the required syntactical overhead.) I didn't want to disappoint the reader with pointless toy examples; therefore some of the programs in Chapter 1 are quite long and, I hope, interesting. The reader should not be . VI Preface discouraged by their complexity. It is not necessary to grasp all details in Chapter 1. Later chapters provide in-depth coverage of all concepts. I try to put the methodology of object-oriented programming in its proper perspective. Not all of C++ is concerned with object-oriented pro- gramming. And object orientation, while extremely useful for many applica- tions, is not the appropriate paradigm for all programming tasks. The book shows the use of C++ in the context of both traditional and object-oriented programming. Most differences between Pascal and C++ are cosmetic and easily mas- tered. However, C++ has a few irksome syntactical quirks that tend to con- fuse newcomers. I point this out when this is the case, so students can then place the blame for the confusion squarely where it belongs—on the lan- guage syntax, rather than themselves. C++ is an excellent language, but it is a human creation and as such not perfect or above criticism. I hope the reader does not construe such occasional comments as an indication that some other language is "better" just because it happens to employ a clearer syntax for some of its constructs. There are many exercises. The exercises are part of the text, rather than at the end of the chapter. The reader is encouraged to skim through them as they appear. Some develop an interesting sideline, others simply give feedback on how much knowledge should have been acquired at that point. Many chapters contain sections on "pitfalls," which illustrate common programming problems. These sections serve two purposes: (1) They can be read as textual material to illustrate complicated aspects of the language; and (2) they can be used as a reference to find a way out of problems arising during programming. The idea for these sections is stolen1 from [Koenig]. This book covers both versions 1 and 2 of C++ The main enhancement . of version 2 is the implementation of multiple inheritance, but it also differs in numerous minor aspects from previous versions. These differences are pointed out throughout the book. At this time, version 2 has just become available, and it is expected that systems implementing version 1 will be in use for some time. Occasionally, versions before 2.0 are referred to as "older versions" of C++ The reader should be familiar with standard data structures: arrays, stacks, queues, sets, linked lists, trees, hash tables, and their implemen- tations. Some mild understanding of college mathematics is also assumed, but certainly no more than necessary for a computer graphics course. Lines, vectors, matrices, polynomials, and complex numbers are used in some of the examples. My thanks go to Earl Tondreau of John Wiley & Sons for prodding me to write this book. I also thank my reviewer—John Vlissides of Stanford University, whose critical and insightful comments prodded me to rewrite the book and Michael Tripoli, who checked the code for correctness. Naturally, any remaining flaws are entirely their fault. Cay S. Horstmann 'Lesser artists borrow, great artists steal." (Igor Stravinsky) CONTENTS CHAPTER 1 A QUICK TOUR THROUGH C++ 1 1.1. "Hello, World" 1 1.2. A Cheap Calculator 3 A 1.3. Stack-Based Calculator 6 A 1.4. Fraction Calculator 10 A 1.5. Flowchart Plotter 16 1.5.1. Screen Display Routines 17 1.5.2. Flowchart Objects 19 1.5.3. Joining Flowchart Objects 21 1.5.4. Putting It Together 24 CHAPTER 2 DATA 31 2.1. Basic Types 31 2.2. Variable and Constant Declarations 33 2.3. Arithmetic 35 2.4. Assignment 37 2.5. Relational and Boolean Operations 38 2.6. Operator Precedence and Order of Evaluation 40 2.7. Pitfalls 42 2.7.1. = vs. == 43 2.7.2. Operator Precedence 43 2.7.3. Overrunning Array Boundaries 44 2.7.4. Order of Evaluation 44 2.7.5. char Can Be signed 44 2.7.6. int Is Promoted to unsigned 45 CHAPTER 3 FUNCTIONS 46 3.1. Function Declarations 46 3.2. Control Flow 52 3.3. Variables 56 3.4. Pitfalls 59 vii ) VIII Contents 3.4.1. return Returns Immediately 59 3.4.2. Functions Cannot Modify Arguments 59 3.4.3. Omitting 60 ( 3.4.4. Missing return Values 60 3.4.5. Mismatched Arguments 60 3.4.6. Misplacing 61 ; 3.4.7. Omitting break in a switch 61 CHAPTER 4 POINTERS, ARRAYS, AND REFERENCES 62 4.1. Pointers are Addresses 62 4.2. Arrays and Pointers 64 4.3. String Functions 67 4.4. References 71 4.5. Pitfalls 76 4.5.1. Copying Without Allocating Storage 76 4.5.2. Different Levels of Indirection 76 4.5.3. Pointing to Data that No Longer Exist 76 4.5.4. Forgetting a String Terminator 77 4.5.5. Using Omniscient sizeof 77 CHAPTER 5 STRUCTURED TYPES 79 5.1. Structures 79 5.2. Member Functions 82 5.3. Classes 86 5.4. Constructors 90 5.5. Friends 92 5.6. Class Interfaces 95 5.7. Unions 98 5.8. Pitfalls 101 5.8.1. Forgetting the 101 ,• 5.8.2. Confusing . and -> 102 CHAPTERS ADVANCED POINTER TOPICS 103 6.1. Two-Dimensional Arrays 103 6.2. Arrays of Pointers 106 6.3. The Command Line 107 6.4. Pointers to Functions 111 6.5. Type Definitions 115 6.6. Error Handling 117 6.7. Pitfalls 121 6.7.1. Using Commas When Accessing Two-Dimensional Arrays 121 6.7.2. Forgetting Parentheses When Evaluating a Pointer to a Function 122

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.