C++ programming
 032135656X, 9780321356567

Table of contents :
Cover......Page 1
Contents......Page 6
Introduction......Page 10
What Is C++?......Page 11
How C++ Works......Page 12
What You’ll Need......Page 13
About This Book......Page 14
Getting Help......Page 16
Chapter 1: Creating a Basic Program......Page 18
Basic C++ Syntax......Page 19
Compiling a C++ Program......Page 23
Printing Text......Page 27
Running a Compiled Program......Page 31
Pausing Execution......Page 33
Understanding White Space......Page 35
Adding Comments to Your Source Code......Page 37
Using an IDE......Page 40
Chapter 2: Simple Variables and Data Types......Page 46
Declaring Variables......Page 47
Assigning Values to Variables......Page 53
Printing Variables......Page 55
Formatting Numbers......Page 58
Understanding Type Conversion......Page 61
Introduction to Characters......Page 65
Introduction to Strings......Page 68
Introduction to Constants......Page 71
Chapter 3: Operators and Control Structures......Page 74
Arithmetic Operators......Page 75
if Conditionals......Page 81
Using else and else if......Page 85
The Ternary Operator......Page 89
Logical and Comparison Operators......Page 93
Switch Conditionals......Page 99
Increment and Decrement Operators......Page 105
While Loop......Page 109
For Loop......Page 113
Chapter 4: Input, Output, and Files......Page 116
Taking Character Input......Page 117
Discarding Input......Page 122
Taking Numeric Input......Page 125
Taking String Input......Page 128
Taking Multiple Inputs......Page 131
Reading In a Whole Line......Page 135
Validating Input......Page 138
Creating File Output......Page 144
Using File Input......Page 149
Chapter 5: Defining Your Own Functions......Page 154
Creating Simple Functions......Page 155
Creating Functions That Take Arguments......Page 160
Setting Default Argument Values......Page 166
Creating Functions That Return a Value......Page 171
Overloading Functions......Page 177
Understanding Variable Scope......Page 181
Chapter 6: Complex Data Types......Page 186
Working with Arrays......Page 187
Working with Pointers......Page 193
Structures......Page 211
Revisiting User-Defined Functions......Page 216
Chapter 7: Introducing Objects......Page 224
Creating a Simple Class......Page 225
Adding Methods to a Class......Page 229
Creating and Using Objects......Page 234
Defining Constructors......Page 238
Defining Destructors......Page 243
The this Pointer......Page 249
Chapter 8: Class Inheritance......Page 254
Basic Inheritance......Page 255
Inheriting Constructors and Destructors......Page 261
Access Control......Page 266
Overriding Methods......Page 271
Overloading Methods......Page 275
Making Friends......Page 278
Chapter 9: Advanced OOP......Page 284
Static Attributes and Methods......Page 285
Virtual Methods......Page 292
Abstract Methods......Page 300
Operator Overloading......Page 305
The

Citation preview

VISUAL QUICKSTART GUIDE

C++ PROGRAMMING Larry Ullman Andreas Signer

Peachpit Press

Visual QuickStart Guide

C++ Programming Larry Ullman and Andreas Signer

Peachpit Press 1249 Eighth Street Berkeley, CA 94710 510/524-2178 800/283-9444 510/524-2221 (fax) Find us on the World Wide Web at: www.peachpit.com To report errors, please send a note to [email protected] Peachpit Press is a division of Pearson Education Copyright © 2006 by Larry Ullman and Andreas Signer Editor: Rebecca Gulick Production Coordinator: Pat Christenson Copy Editor: Bob Campbell Technical Reviewer: Brent Knigge Compositors: Owen Wolfson and Amy Hassos Indexer: Karin Arrigoni Cover Design: Peachpit Press Cover Production: George Mattingly

Notice of Rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact [email protected].

Notice of Liability The information in this book is distributed on an “As Is” basis without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it.

Trademarks Macintosh and Mac OS X are registered trademarks of Apple Computer, Inc. Microsoft and Windows are registered trademarks of Microsoft Corporation. Other product names used in this book may be trademarks of their own respective owners. Images of Web sites in this book are copyrighted by the original holders. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit Press was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book. ISBN 0-321-35656-X 987654321 Printed and bound in the United States of America

Dedications As sycophantic as it may seem, Larry dedicates this book to the wonderful people who work for Peachpit Press, especially editor extraordinaire Rebecca Gulick. Thank you all for your patience and support and for making sure that we put together the best books possible. Andi dedicates this book to Claudia, for her patience, support, and understanding, and to his Mom, for raising him the way she did.

Acknowledgments Larry sends warm fuzzies and a fruit basket each to… Andi Signer, co-author. Writing a book is a lot of work and I couldn’t have done this one without you. Thanks for all of your excellent work and input. Everyone at Peachpit Press, as always. Special mention goes out to: Nancy Ruenzel, Nancy Davis, and Marjorie Baer; the marketing team, for telling the booksellers about the book; Lisi Baldwin, for sending translated copies on to me; and the other dozen or so people at Peachpit and Pearson whose names and titles I don’t know but should. Rebecca Gulick, editor, cheerleader, mediator, and guidance counselor (in a good way). Bob Campbell, copy editor, for his excellent attention to detail, his suggestions, and his feedback. Thanks also to Karin Arrigoni for indexing and to Pat Christenson, Owen Wolfson, and Amy Hassos for managing the production and laying out the book, respectively. Brent Knigge, technical editor, for your honest opinions, strict adherence to the standards, and eye on the greater marketplace. Thanks as well, for your continued assistance in supporting readers through my book forums. Thanks, as always, to the readers, whose interest gives my job relevance. And last, but never least, Jessica. Just a simple thanks for everything.

Andi would also like to thank… Larry Ullman. Thanks for writing a book together with a newbie author, and for all the valuable hints about writing you gave me. My co-workers. The discussions about pros and cons of C++ were very interesting and gave me a lot of insight. My friends. Either for discussing code and providing valuable input, or for keeping me away from my computer.

Table of Contents Introduction

ix

What Is C++? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x How C++ Works. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Getting Help. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Creating a Basic Program

1

Basic C++ Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Compiling a C++ Program . . . . . . . . . . . . . . . . . . . . . 6 Printing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Running a Compiled Program . . . . . . . . . . . . . . . . . 14 Pausing Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Understanding White Space . . . . . . . . . . . . . . . . . . 18 Adding Comments to Your Source Code . . . . . . . 20 Using an IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Chapter 2:

Simple Variables and Data Types

29

Declaring Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Assigning Values to Variables . . . . . . . . . . . . . . . . . 36 Printing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 41 Understanding Type Conversion. . . . . . . . . . . . . . . 44 Introduction to Characters. . . . . . . . . . . . . . . . . . . . 48 Introduction to Strings . . . . . . . . . . . . . . . . . . . . . . . 51 Introduction to Constants . . . . . . . . . . . . . . . . . . . . 54

Chapter 3:

Operators and Control Structures

57

Arithmetic Operators. . . . . . . . . . . . . . . . . . . . . . . . . 58 if Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Using else and else if . . . . . . . . . . . . . . . . . . . . . . . . . 68 The Ternary Operator . . . . . . . . . . . . . . . . . . . . . . . . 72 Logical and Comparison Operators . . . . . . . . . . . . 76 Switch Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Increment and Decrement Operators . . . . . . . . . . 88

v

Table of Contents

Chapter 1:

Table of Contents While Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

Chapter 4:

Input, Output, and Files

99

Taking Character Input . . . . . . . . . . . . . . . . . . . . . . 100 Discarding Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Taking Numeric Input . . . . . . . . . . . . . . . . . . . . . . . 108 Taking String Input . . . . . . . . . . . . . . . . . . . . . . . . . 111 Taking Multiple Inputs . . . . . . . . . . . . . . . . . . . . . . 114 Reading In a Whole Line. . . . . . . . . . . . . . . . . . . . . 118 Validating Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Creating File Output . . . . . . . . . . . . . . . . . . . . . . . . 127 Using File Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Chapter 5:

Defining Your Own Functions

137

Table of Contents

Creating Simple Functions . . . . . . . . . . . . . . . . . . . 138 Creating Functions That Take Arguments . . . . . 143 Setting Default Argument Values . . . . . . . . . . . . . 149 Creating Functions That Return a Value . . . . . . 154 Overloading Functions . . . . . . . . . . . . . . . . . . . . . . 160 Understanding Variable Scope . . . . . . . . . . . . . . . 164

Chapter 6:

Complex Data Types

169

Working with Arrays . . . . . . . . . . . . . . . . . . . . . . . . 170 Working with Pointers. . . . . . . . . . . . . . . . . . . . . . . 176 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Revisiting User-Defined Functions. . . . . . . . . . . . 199

Chapter 7:

Introducing Objects

207

Creating a Simple Class. . . . . . . . . . . . . . . . . . . . . . 208 Adding Methods to a Class . . . . . . . . . . . . . . . . . . 212 Creating and Using Objects . . . . . . . . . . . . . . . . . . 217 Defining Constructors . . . . . . . . . . . . . . . . . . . . . . . 221 Defining Destructors . . . . . . . . . . . . . . . . . . . . . . . . 226 The this Pointer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

Chapter 8:

Class Inheritance

237

Basic Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Inheriting Constructors and Destructors . . . . . . 244 Access Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Overriding Methods. . . . . . . . . . . . . . . . . . . . . . . . . 254 Overloading Methods . . . . . . . . . . . . . . . . . . . . . . . 258 Making Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

vi

Table of Contents

Chapter 9:

Advanced OOP

267

Static Attributes and Methods . . . . . . . . . . . . . . . 268 Virtual Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Abstract Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . 288 The