C++ Programming for the Absolute Beginner, Second Edition Mark Lee Course Technology PTR A part of Cengage Learning Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States C++ Programming for the Absolute © 2009 Course Technology, a part of Cengage Learning. Beginner, Second Edition: Mark Lee ALL RIGHTS RESERVED. No part of this work covered by the copyright Publisher and General Manager, Course herein may be reproduced, transmitted, stored, or used in any form or by Technology PTR: Stacy L. Hiquet any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, Associate Director of Marketing: information networks, or information storage and retrieval systems, except Sarah Panella as permitted under Section 107 or 108 of the 1976 United States Copyright Manager of Editorial Services: Act, without the prior written permission of the publisher. Heather Talbot Marketing Manager: Mark Hughes For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 Acquisitions Editor: Mitzi Koontz For permission to use material from this text or product, submit all Project Editor: Jenny Davidson requests online at cengage.com/permissions Further permissions questions can be emailed to [email protected] Technical Reviewer: Keith Davenport PTR Editorial Services Coordinator: All trademarks are the property of their respective owners. Jen Blaney Library of Congress Control Number: 2008939939 Interior Layout Tech: Value Chain International ISBN-13: 978-1-59863-875-2 ISBN-10: 1-59863-875-0 Cover Designer: Mike Tanamachi eISBN-10: 1-59863-941-2 Indexer: Sharon Shock Course Technology, a part of Cengage Learning 20 Channel Center Street Proofreader: Sara Gullion Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in the United States of America 1 2 3 4 5 6 7 11 10 09 To Dirk Henkemans, my best friend. I am lost without you. F OREWORD T he video game industry is unique in that it regularly incorporates every major discipline of computer science. From 3D graphics and artificial in- telligence to operating system theory and database design, if you are designing a commercial video game, you will eventually run into problems from each of these fields. Some of these fields mean working with specialized languages, but ultimately the two languages that are as common to the game industry as crunch time, caffeinated beverages, and pizza are C and C++. Despite a few com- mercial games written in Java (which is very similar to C++), almost every game that you play is written in either C or C++. It doesn’t matter whether the game runs on a PC, a game console, or even an arcade machine, chances are that C or C++ routines are at its heart. Even in cases when performance dictates that a routine needs to be written in assembly language to squeeze out more speed, it is common practice to first write the routine in C or C++. During my years in the industry, I have interviewed over one hundred applicants for programming positions and have read resumes from thousands more. Through all of this, I continually look for three things in a strong candidate. The first is strong problem-solving skills. With constantly changing technologies and fierce competition, game programming is always throwing new problems at us. Conse- quently, excellent problem-solving skills are not only a luxury, but they are also a requirement. Second, a good candidate has been exposed to the entire spectrum of computer science disciplines. Even when programmers have specialized in one area, the solution to a problem often lies in a field outside their area of expertise. Finally, I look for strong C/C++ skills. C/C++ skills are to a game programmer what paint and brushes are to a painter. They are the tools of the trade and, as such, they need to be finely honed. Although C++ is widely used as a teaching language, this wasn’t always the case. I can still recall my first exposure to C programming. Until that time, all of my programming had been in Basic (my first video game was written in it), Pascal, and Fortran. I had heard of C; according to rumor, it was going to be the language to know. I was looking forward to my next computer science course: “Introduction to Programming Languages.” I assumed that the course would teach me how to program in C. I was wrong. The only reference to C in the entire course was, “Here is your assignment. Write it in C. Hand it in on Wednesday.” “Okay,” I thought. “At least one of the course textbooks is about C.” As it turned out, that textbook was about accessing UNIX operating system information from the C language. That was useful if I was interested in accessing process IDs or using shell commands, but not a great help if I wanted to know how to read a file or write a function. Somehow I managed to struggle through the assignment and to actually learn something while I did it. It wasn’t the best way to learn a new language, but it was better than my first exposure to C++. That was during my first job after graduation. I was working for the univer- sity’s athletic department writing software for various research projects. One of the projects that I inherited from the previous programmer was only half complete and was written in C++. Once again, I had before me a sink-or-swim proposition. This time, I had access to a function reference that explained only the syntax of the language, not how to use it. I would have killed for the book that you are currently holding in your hands. Well, maybe not killed, but I certainly can’t overestimate the importance of learning C++ in such an organized and straightforward manner. As you read this book, please have some sympathy for those of us who didn’t have the fine learning tool you have. Scott Greig Director of Programming BioWare Corp. A CKNOWLEDGMENTS T he amount of time and effort involved in the process of publishing a book is considerable, and this book is no exception. As only one cog in the vast machine of effort involved, it is difficult for me to fully appreciate all of the hands that have touched this edition. However, without the ambition and drive of my original co-author, Dirk Henkemans, this book would have remained an unrequited dream. His drive and perseverance taught me that seemingly impos- sible things, like writing a book, were not only possible, but readily attainable. I thank Course Technology PTR for making this book possible and Mitzi Koontz, my acquisitions editor, for all of her efforts. I am deeply indebted to Keith Davenport, the technical editor, whose careful eye ensured all of the code in this book works as it should. Jenny Davidson, the project and copy editor, was an exceptional resource through- out the project and her careful editing improved this work by orders of magnitude. I extend a special thank you to everyone else who played a role in preparing this book for publication, including Value Chain International, layout; Sara Gullion, proofreader; and Sharon Shock, indexer. All of you played a big role in making this book what it is. We give praise to Scott Greig, the lead programmer at BioWare Corp. and the author of this book’s foreword. Scott, you are our idol. Without you, who could we aspire to be? I am deeply thankful for the scrutinizing eye of Jen Janzen whose talent for editing is unrivaled. Every line she touches is improved by orders of magnitude. Finally, I’d like to give thanks to my parents, for putting up with and supporting me all of these years. A A BOUT THE UTHOR M ark Lee is a professional web developer and programmer. He has a degree in Computer Science from the University of Alberta and is proficient in the use of C, C++, Java, Ruby, PHP, AJAX, MySQL, and JavaScript. This page intentionally left blank Table of Contents Chapter 1 Starting the Journey................................................ 1 Computer Basics..................................................................................................................... 2 Defining Source Code........................................................................................................... 2 Examining Hello World............................................................................................... 3 Using Visual Studio............................................................................................................... 6 Creating a New Project......................................................................................................... 6 The Software Development Cycle.................................................................................... 11 Working with Text............................................................................................................... 12 Assembling Strings.............................................................................................................. 13 Displaying Strings with cout............................................................................................ 13 Displaying Multiple Strings with cout.................................................................. 14 Working with Escape Characters.................................................................................... 14 Storing Strings...................................................................................................................... 15 Displaying Stored Strings.................................................................................................. 16 The Town Crier Program.................................................................................................... 17 Receiving Input.................................................................................................................... 18 Storing Strings with cin..................................................................................................... 18 Working with Numbers..................................................................................................... 20 Introducing Integers........................................................................................................... 20 Taking Action with Operators.......................................................................................... 20 The Modulus Operator........................................................................................................ 21 Creating the Pirate Musketeer Game............................................................................. 21 Summary................................................................................................................................ 23 Chapter 2 Descending Deeper into Variables........................ 25 Understanding Variables................................................................................................... 25 Sorting Out the Relationship between Variables and Memory............................... 26 Describing Variable Identifiers........................................................................................ 29 Declaring and Assigning Variables................................................................................. 30 Introducing the Fundamental Variable Types............................................................. 31 The Boolean Type................................................................................................................. 31 The Character Types............................................................................................................ 32 The Integer Types................................................................................................................. 32
Description: