Sams Teach Yourself C++ in 21 Days, ASSOCIATE PUBLISHER Michael Stephens
Total Page:16
File Type:pdf, Size:1020Kb
00 0672327112_fm.qxd 11/19/04 12:52 PM Page i Jesse Liberty Bradley Jones Teach Yourself C++ in 21 Days FIFTH EDITION 800 East 96th Street, Indianapolis, Indiana, 46240 USA 00 0672327112_fm.qxd 11/19/04 12:52 PM Page ii Sams Teach Yourself C++ in 21 Days, ASSOCIATE PUBLISHER Michael Stephens Fifth Edition ACQUISITIONS EDITOR Copyright © 2005 by Sams Publishing Loretta Yates All rights reserved. No part of this book shall be reproduced, stored in a DEVELOPMENT EDITOR retrieval system, or transmitted by any means, electronic, mechanical, photo- Songlin Qiu copying, recording, or otherwise, without written permission from the pub- MANAGING EDITOR lisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation Charlotte Clapp of this book, the publisher and author assume no responsibility for errors or PROJECT EDITOR omissions. Nor is any liability assumed for damages resulting from the use of Seth Kerney the information contained herein. COPY EDITOR International Standard Book Number: 0-672-32711-2 Karen Annett Library of Congress Catalog Card Number: 2004096713 INDEXER Printed in the United States of America Erika Millen PROOFREADER First Printing: December 2004 Paula Lowell 07060504 4321 TECHNICAL EDITORS Mark Cashman Trademarks David V. Corbin All terms mentioned in this book that are known to be trademarks or service PUBLISHING COORDINATOR marks have been appropriately capitalized. Sams Publishing cannot attest to Cindy Teeters 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. MULTIMEDIA DEVELOPER Dan Scherf Warning and Disclaimer BOOK DESIGNER Every effort has been made to make this book as complete and as accurate as Gary Adair possible, but no warranty or fitness is implied. The information provided is on PAGE LAYOUT an “as is” basis. The authors and the publisher shall have neither liability nor Eric S. Miller responsibility to any person or entity with respect to any loss or damages Julie Parks arising from the information contained in this book. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S., please contact International Sales [email protected] 00 0672327112_fm.qxd 11/19/04 12:52 PM Page iii Contents at a Glance Introduction 1 Week 1 At a Glance 3 Day 1 Getting Started 5 2 The Anatomy of a C++ Program 25 3 Working with Variables and Constants 41 4 Creating Expressions and Statements 67 5 Organizing into Functions 99 6 Understanding Object-Oriented Programming 137 7 More on Program Flow 175 Week 1 In Review 209 Week 2 At a Glance 219 Day 8 Understanding Pointers 221 9 Exploiting References 255 10 Working with Advanced Functions 289 11 Object-Oriented Analysis and Design 329 12 Implementing Inheritance 371 13 Managing Arrays and Strings 407 14 Polymorphism 449 Week 2 In Review 491 Week 3 At a Glance 503 Day 15 Special Classes and Functions 505 16 Advanced Inheritance 537 17 Working with Streams 593 18 Creating and Using Namespaces 637 19 Templates 659 20 Handling Errors and Exceptions 715 21 What’s Next 751 00 0672327112_fm.qxd 11/19/04 12:52 PM Page iv Week 3 In Review 791 Appendixes Appendix A Working with Numbers: Binary and Hexadecimal 807 B C++ Keywords 817 C Operator Precedence 819 D Answers 821 E A Look at Linked Lists 875 Index 887 00 0672327112_fm.qxd 11/19/04 12:52 PM Page v Contents Introduction 1 Who Should Read This Book ..................................................................................1 Conventions Used in This Book ..............................................................................1 Sample Code for This Book ....................................................................................2 Week 1 At a Glance 3 A Note to C Programmers ......................................................................................3 Where You Are Going..............................................................................................3 1 Getting Started 5 A Brief History of C++ ..........................................................................................5 The Need for Solving Problems ........................................................................7 Procedural, Structured, and Object-Oriented Programming ..............................8 Object-Oriented Programming (OOP) ..............................................................9 C++ and Object-Oriented Programming............................................................9 How C++ Evolved ................................................................................................11 Should I Learn C First? ........................................................................................11 C++, Java, and C#..................................................................................................12 Microsoft’s Managed Extensions to C++..............................................................12 The ANSI Standard................................................................................................12 Preparing to Program ............................................................................................13 Your Development Environment ..........................................................................14 The Process of Creating the Program....................................................................15 Creating an Object File with the Compiler ......................................................15 Creating an Executable File with the Linker ..................................................15 The Development Cycle ........................................................................................16 HELLO.cpp—Your First C++ Program ..................................................................17 Getting Started with Your Compiler......................................................................19 Building the Hello World Project ....................................................................19 Compile Errors ......................................................................................................20 Summary................................................................................................................21 Q&A ......................................................................................................................21 Workshop ..............................................................................................................22 Quiz ..................................................................................................................22 Exercises ..........................................................................................................23 2 The Anatomy of a C++ Program 25 A Simple Program ................................................................................................25 A Brief Look at cout ............................................................................................28 00 0672327112_fm.qxd 11/19/04 12:52 PM Page vi vi Sams Teach Yourself C++ in 21 Days, Fifth Edition Using the Standard Namespace ............................................................................30 Commenting Your Programs..................................................................................32 Types of Comments..........................................................................................33 Using Comments ..............................................................................................33 A Final Word of Caution About Comments ....................................................34 Functions................................................................................................................35 Using Functions................................................................................................36 Methods Versus Functions................................................................................38 Summary................................................................................................................38 Q&A ......................................................................................................................38 Workshop ..............................................................................................................39 Quiz ..................................................................................................................39 Exercises ..........................................................................................................39 3 Working with Variables and Constants 41 What Is a Variable?................................................................................................41 Storing Data in Memory ..................................................................................42 Setting Aside Memory......................................................................................42 Size of Integers ................................................................................................43 signed and unsigned ........................................................................................45 Fundamental Variable Types ............................................................................45