C++ Programming
Visual QuickStart Guide
Larry Ullman
Andreas Signer
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
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 .
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
9 8 7 6 5 4 3
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 couldnt 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 dont 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
We imagine that most people come to C++ for one of two reasons. In the first group are those that have C++ thrust upon them, in school or at work. In the second group are those with an interest in writing software who think that C++ may just be the right tool for the job. In either case, you wont be disappointedin the language or in this book.
C++ has been around for years and, despite the arrival of newcomers like Java and C#, is still one of the top dogs when it comes to software development. The heaviest of the heavyweightsMicrosoft, Adobe, Sun, Intel, Amazon.com, Google, Apple, Nokiaall use C++ to some extent. The language is relatively easy to use (especially when learning it using an introductory text like this one) and is amazingly powerful. Youll be able to create basic applications today and really far-out ones in months.
Although C++ is a serious programming language, even those without formal training or only modest computer skills can pick up the subject. This book was written as the no-frills, heres-what-you-really-need-to-know beginners guide to the C++ programming language. Absolutely no programming experience is expected of you (including no C experience), but by following the examples and explanations we present here, youll have some real-world know-how in no time.
What Is C++?
To understand C++, you must begin with C. The C programming language was created in the 1970s and provided a new, valuable tool for programmers (C arose from the B programming language, but we dont need to go that far back). The two main benefits of C are performance and portability. C programs tend to be compact and speedy, and most C code can easily be used on many different operating systems.
C is a procedural language , which means that the focus is on an ordered sequence of commands. This is fine, but as you program more and as your applications become larger, reliance upon procedures is less and less efficient.
The C++ programming language was created in the early 1980s by Bjarne Stroustrup, an employee at Bell Labs. C++ was intended as an improved version of C. C++ retains all of the benefits of Cits efficiency, portability, and ability to work with computers on a low levelwhile adding:
Support for objects and OOP (see the sidebar)
Much improved productivity for the programmer
Fixes for common C problems
This is not to say that there isnt a need or room for C anymore, just that C++ is quite the upgrade. Which leads us to our next point...
Although C++ is an outgrowth of C, you do not need to know C in order to use this book. If you already know C, that wont hurt, but youll see early on that we always discard problematic and antiquated C techniques if C++ has created a better solution (like using C++ strings instead of C-style strings). C is still a subset of C++, meaning that most C code is valid in C++, but our focus is on programming in proper C++.
Next page