Contents i What’s New with This Edition Teach Yourself ANSI C++ in 21 Days, Premier Edition, is a new edition of the international bestseller, Teach Yourself C++ in 21 Days. This Premier Edition offers more than just 21 days of learning ANSI/ISO C++. Major changes throughout this edition have been made to reflect the latest ANSI/ ISO C++ draft standard. In addition, seven new lessons, referred to as “Bonus Days,” have been added to provide you with additional information for creating more sophisticated programs. The Bonus Day lessons are not required reading in order for you to learn to program in C++. As a matter of fact, on the very first day you will write a simple C++ program. On Bonus Day 22, “Advanced C++ Features,” you learn to use a collection of specialized ANSI C++ tools that many programmers don’t even realize exist. These tools are not essential for every C++ programmer to know but are indispensable in many circumstances. Bonus Day 23, “More About Streams,” covers some of the more advanced streams programming techniques. After completing this lesson, you will have all you need to create new manipulators for the stream classes you learn to create on Day 16. You will also learn a great deal about other streams programming techniques that space would not allow us to cover on Day 16. On Bonus Day 24, “Object-Oriented Design,” you learn more about this important subject. Without object-oriented design techniques, C++ is “just another C” with few advantages over its predecessor. This lesson helps you think in a way that builds on the object-oriented design paradigm. Bonus Days 25 and 26, “Data Structures” and “Simple Sorting and Searching Algorithms,” respectively, introduce you to the inner sanctum of computer science. On these two days you learn how to solve several common categories of problems. “Common Mistakes and Basic Debugging,” on Bonus Day 27, is designed to help you avoid some of the common mistakes that many beginning programmers make. Not all errors can be avoided, but with this lesson you should more quickly recognize many errors by their categories and more quickly find their solutions. The final day of the bonus week, “What’s Next?,” gives you some insight on where to turn when you run into a question that this book doesn’t seem to answer. It also provides the resources you need to further improve your C++ programming skills. P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 ii Teach Yourself ANSI C++ in 21 Days P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Contents iii teach yourself ANSI C++ in 21 days, Premier Edition http://avaxhome.ws/blogs/ChrisRedfield P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 iv Teach Yourself ANSI C++ in 21 Days P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Contents v teach yourself ANSI C++ in 21 days, Premier Edition Jesse Liberty J. Mark Hord S MS PUBLISHING 201 West 103rd Street Indianapolis, Indiana 46290 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 vi Teach Yourself ANSI C++ in 21 Days Copyright (cid:211) 1996 by Sams Publishing Acquisitions Editor Bradley L. Jones PREMIER EDITION Development Editor All rights reserved. No part of this book shall be reproduced, stored in a Anthony Amico retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the Production Editor publisher. No patent liability is assumed with respect to the use of the Ryan Rader information contained herein. Although every precaution has been taken in Copy Editors the preparation of this book, the publisher and author assume no responsi- Howard Jones bility for errors or omissions. Neither is any liability assumed for damages Marla Reece resulting from the use of the information contained herein. For informa- tion, address Sams Publishing, 201 W. 103rd St., Indianapolis, IN 46290. Technical Reviewer Justin Bell International Standard Book Number: 0-672-30887-6 John W. Charlesworth Library of Congress Catalog Card Number: 95-72920 Editorial Coordinator 99 98 97 96 4 3 2 1 Bill Whitmer Interpretation of the printing code: the rightmost double-digit number is Technical Edit Coordinator the year of the book’s printing; the rightmost single-digit, the number of the Lynette Quinn book’s printing. For example, a printing code of 96-1 shows that the first Formatter printing of the book occurred in 1996. Frank Sinclair Composed in Agaramond and MCPdigital by Macmillan Computer Publishing Editorial Assistants Printed in the United States of America Sharon Cox Andi Richter All terms mentioned in this book that are known to be trademarks or service Rhonda Tinch-Mize marks have been appropriately capitalized. Sams Publishing cannot attest to Cover Designer the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Tim Amrhein Book Designer Gary Adair Copy Writer Peter Fuller Production Team Supervisor Brad Chinn Production Mary Ann Abramson, Mona Brown, Georgiana Briggs, Michael Brummitt, Jeanne Clark, Terri Edwards, George Hamlin, Sonya Hart, Mike Henry, Publisher and President Richard K. Swadley Ayanna Lacey, Kevin Laseau, Acquisitions Manager Greg Wiegand Paula Lowell, Donna Martin, Casey Price, Nancy Price, Development Manager Dean Miller Brian-Kent Proffitt, Beth Rago, Managing Editor Cindy Morrow SA Springer, Tim Taylor, Marketing Manager Gregg Bushyeager Andrew Stone, Mark Walchle Todd Wente, Colleen Williams P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Contents vii Overview Week 1 At A Glance 1 1 Getting Started 3 2 The Parts of a Program 19 3 Variables and Constants 31 4 Expressions and Statements 55 5 Functions 87 6 Basic Classes 123 7 More Program Flow 155 Week 1 In Review 187 Week 2 At A Glance 193 8 Pointers 195 9 References 223 10 Advanced Functions 257 11 Arrays 293 12 Inheritance 329 13 Multiple Inheritance 361 14 Special Classes and Functions 395 Week 2 In Review 421 Week 3 At A Glance 431 15 Advanced Inheritance 433 16 Streams 469 17 The Preprocessor 509 18 Creating Reusable Code 537 19 Templates 551 20 Exceptions and Error Handling 581 21 Working with Multiple Files for Large Programs 609 Week 3 In Review 625 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 viii Teach Yourself ANSI C++ in 21 Days Bonus Days At A Glance 635 22 Coding Styles and Idioms 637 23 More About Streams 659 24 Object-Oriented Design 673 25 Data Structures 683 26 Simple Sorting and Searching Algorithms 701 27 Common Mistakes and Basic Debugging 721 28 What’s Next? 733 Appendixes A Operator Precedence 741 B C++ Keywords 745 C Binary and Hexadecimal 747 D Answers 757 Index 831 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 Contents ix Contents Week 1 At A Glance 1 A Note to C Programmers..........................................................................2 Where You Are Going................................................................................2 Day 1 Getting Started 3 A Brief History of C++...............................................................................3 ANSI/ISO C++ ..........................................................................................5 What Is a Program?....................................................................................5 Sophisticated Tools for Sophisticated Programming...................................6 Procedural, Structured, and Object-Oriented Programming..................6 C++ and Object-Oriented Programming...............................................8 How C++ Evolved.................................................................................9 Should I Learn C First?........................................................................10 Preparing to Program ...............................................................................10 Your Development Environment..............................................................11 Compiling the Source Code .....................................................................12 Creating an Executable File with the Linker.............................................12 The Development Cycle...........................................................................13 HELLO.CPP: Your First C++ Program....................................................14 Compile Errors.........................................................................................15 Summary..................................................................................................16 Q&A........................................................................................................16 Quiz.........................................................................................................17 Exercises...................................................................................................17 2 The Parts of a C++ Program 19 The Parts of a Simple Program.................................................................19 A Brief Look at cout.................................................................................21 Comments................................................................................................22 Types of Comments ............................................................................23 Using Comments.................................................................................23 Comments at the Top of Each File......................................................24 A Final Word of Caution About Comments........................................25 Functions .................................................................................................26 Using Functions ..................................................................................27 Summary..................................................................................................29 Q&A........................................................................................................29 Quiz.........................................................................................................29 Exercises...................................................................................................30 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3 x Teach Yourself ANSI C++ in 21 Days 3 Variables and Constants 31 What Is a Variable?...................................................................................31 Setting Aside Memory .........................................................................32 Size of Integers.....................................................................................33 Signed and Unsigned...........................................................................34 Fundamental Variable Types ...............................................................35 Defining a Variable...................................................................................36 Case Sensitivity....................................................................................37 Keywords.............................................................................................38 Creating More Than One Variable at a Time................................................................................................39 Assigning Values to Your Variables...........................................................39 typedef....................................................................................................40 Choosing Between short and long...........................................................41 Wrapping Around in Unsigned Integers..............................................42 Wrapping Around a Signed Integer .....................................................43 Characters ................................................................................................43 chars and Numbers.............................................................................44 Special Printing Characters..................................................................44 Constants .................................................................................................46 Literal Constants..................................................................................46 Symbolic Constants.............................................................................46 Enumerated Constants.............................................................................49 Summary..................................................................................................50 Q&A........................................................................................................51 Quiz.........................................................................................................52 Exercises...................................................................................................53 4 Expressions and Statements 55 Statements................................................................................................56 Whitespace..........................................................................................56 Blocks and Compound Statements......................................................57 Expressions...............................................................................................57 Operators .................................................................................................59 Assignment Operator...........................................................................59 Mathematical Operators......................................................................60 Integer Division and Modulus.............................................................60 Combining the Assignment and Mathematical Operators ........................61 Increment and Decrement........................................................................62 Prefix and Postfix.................................................................................62 Precedence................................................................................................64 Nesting Parentheses..................................................................................64 The Bitwise Operators..............................................................................65 Operator AND....................................................................................66 Operator OR.......................................................................................67 Operator Exclusive OR........................................................................67 P2/V3/sqc5 TY ANSI C++ in 21 30887-6 Casey 2.23.96 FM LP#3
Description: