Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Ivor Horton’s Beginning Visual C++ ® 2005 Ivor Horton 01_571974 ffirs.qxp 1/20/06 11:22 PM Page iii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 01_571974 ffirs.qxp 1/20/06 11:22 PM Page ii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Ivor Horton’s Beginning Visual C++ ® 2005 01_571974 ffirs.qxp 1/20/06 11:22 PM Page i Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 01_571974 ffirs.qxp 1/20/06 11:22 PM Page ii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Ivor Horton’s Beginning Visual C++ ® 2005 Ivor Horton 01_571974 ffirs.qxp 1/20/06 11:22 PM Page iii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Ivor Horton’s Beginning Visual C++ ® 2005 Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2006 by Ivor Horton Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN-13: 978-0-7645-7197-8 ISBN-10: 0-7645-7197-4 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/QY/QS/QW/IN Library of Congress Cataloging-in-Publication Data: Horton, Ivor. Ivor Horton’s Beginning Visual C++ 2005 / Ivor Horton. p. cm. Includes index. ISBN-13: 978-0-7645-7197-8 (paper/website) ISBN-10: 0-7645-7197-4 (paper/website) 1. C++ (Computer program language) 2. Microsoft Visual C++. I. Title: Beginning Visual C++ 2005. II. Title. QA76.73.C15I6694 2006 005.13’3—dc22 2005032051 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE- ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Visual C++ is a registered trademark of Microsoft Cor- poration in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. 01_571974 ffirs.qxp 1/20/06 11:22 PM Page iv Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Credits Executive Editor Bob Elliott Senior Development Editor Kevin Kent Development Editor Howard Jones Technical Editor John Mueller Production Editor Pamela Hanley Copy Editor Susan Hobbs Editorial Manager Mary Beth Wakefield Production Manager Tim Tate Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert Project Coordinator Ryan Steffen Graphics and Production Specialists Brian Drumm Lauren Goddard Brooke Graczyk Denny Hager Joyce Haughey Jennifer Heleine Stephanie D. Jumper Barry Offringa Alicia South Erin Zeltner Quality Control Technicians Laura Albert John Greenough Leeann Harney Proofreading TECHBOOKS Production Services Indexing Infodex Indexing Services Inc. 01_571974 ffirs.qxp 1/20/06 11:22 PM Page v Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 01_571974 ffirs.qxp 1/20/06 11:22 PM Page vi Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com About the Author Ivor Horton graduated as a mathematician and was lured into information technology by promises of great rewards for very little work. In spite of the reality being usually a great deal of work for relatively modest rewards, he has continued to work with computers to the present day. He has been engaged at various times in programming, systems design, consultancy, and the management of the implementa- tion of projects of considerable complexity. Horton has many years of experience in the design and implementation of computer systems applied to engineering design and to manufacturing operations in a variety of industries. He also has considerable experience in developing occasionally useful applications in a wide variety of programming languages, and of teaching primarily scientists and engineers to do likewise. He has written books on programming for more than 10 years; his currently published works include tutorials on C, C++, and Java. At the pre- sent time, when he is not writing programming books or providing advice to others, he spends his time fishing, travelling, and trying to speak better French. 01_571974 ffirs.qxp 1/20/06 11:22 PM Page ix Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... 86 86 88 88 91 91 94 95 96 98 99 C+ +/CLI Specific: Fundamental Data Types C+ +/CLI Output to the Command Line C+ +/CLI Specific — Formatting the Output C+ +/CLI Input from the Keyboard Using safe_cast C+ +/CLI Enumerations 99 10 4 10 4 10 7 10 8 10 9 Specifying a Type for Enumeration Constants Specifying Values for Enumeration Constants 11 1 11 1 Summary Exercises Chapter 3: Decisions and Loops Comparing Values... New Order ID Initiating ID Creation Storing the Order Data Setting Dates Selecting Products for an Order Adding a New Order Summary Exercises xxxii 9 91 992 993 994 996 997 999 10 00 10 01 1002 10 02 10 06 10 10 10 14 10 14 10 15 10 17 10 19 10 20 10 21 1023 10 28 10 29 Contents Chapter 21: Applications Using Windows Forms 10 31 Understanding Windows Forms Understanding Windows Forms Applications Simpo PDF Merge and... Loops C+ +/CLI Programming The for each Loop Summary Exercises Chapter 4: Arrays, Strings, and Pointers Handling Multiple Data Values of the Same Type Arrays Declaring Arrays Initializing Arrays Character Arrays and String Handling String Input Multidimensional Arrays Initializing Multidimensional Arrays 12 4 12 5 12 5 12 6 12 7 12 9 13 2 13 2 13 2 13 5 13 9 14 3 14 3 14 6 14 7 15 0 15 3 15 6 15 7 15 9 16 0 16 0 16 1 16 4 16 6 16 7... Limits Menu Creating a Dialog Box Adding a List to a ListBox Handling the Dialog Button Events Controlling the State of the ListBox Objects Creating the Dialog Object Using the Dialog Box Validating the Input Handler the Reset Menu Item Event Adding the Second Dialog 10 34 10 35 10 35 10 36 10 37 10 38 10 40 10 42 10 44 10 47 10 48 10 49 10 49 10 50 10 50 10 52 10 56 10 56 10 58 10 60 10 61 10 61 1062 10 63 10 67 10 68 Getting... 908 910 911 912 913 914 915 916 917 917 918 920 920 Contents Chapter 19 : Connecting to Data Sources Database Basics A and SQL Simpo PDF MergeLittle Split Unregistered Version - http://www.simpopdf.com Retrieving Data Using SQL Choosing Records 9 21 9 21 924 924 925 Joining Tables Using SQL Sorting Records 926 929 Database Support in MFC 929 MFC Classes Supporting ODBC 930 Creating a Database Application... Outside the Class Arrays of Objects of a Class Static Members of a Class Static Data Members of a Class Static Function Members of a Class Pointers and References to Class Objects Pointers to Class Objects References to Class Objects Implementing a Copy Constructor C+ +/CLI Programming Defining Value Class Types The ToString() Function in a Class Literal Fields Defining Reference Class Types Class Properties... The struct in C+ + What Is a struct? Defining a struct Initializing a struct Accessing the Members of a struct Intellisense Assistance with Structures The struct RECT Using Pointers with a struct Accessing Structure Members through a Pointer The Indirect Member Selection Operator Data Types, Objects, Classes and Instances 298 299 302 304 305 307 308 309 309 310 315 316 316 318 318 319 320 3 21 323 324... Collections of Objects Simpo PDF Merge 7 61 The CArray Template Class Helper Functions and Split Template Class The CList Unregistered The CMap Template Class 7 61 763 763 768 Version - http://www.simpopdf.com The Typed Pointer Collections 7 71 The CTypedPtrList Template Class CTypePtrList Operations 7 71 7 71 Using the CList Template Class 773 Drawing a Curve Defining the CCurve Class Implementing the CCurve... Nested Classes C+ +/CLI Programming Inheritance in C+ +/CLI Classes Interface Classes Defining Interface Classes Classes and Assemblies Visibility Specifiers for Classes and Interfaces Access Specifiers for Class and Interface Members Functions Specified as new Delegates and Events Declaring Delegates Creating Delegates Unbound Delegates Creating Events Destructors and Finalizers in Reference Classes... Graphics Drawing in Color 708 709 709 709 711 711 711 712 713 717 Drawing Graphics in Practice Programming the Mouse 7 21 725 Messages from the Mouse 725 WM_LBUTTONDOWN WM_MOUSEMOVE WM_LBUTTONUP Mouse Message Handlers Drawing Using the Mouse Getting the Client Area Redrawn Defining Classes for Elements The CElement Class The CLine Class Calculating the Enclosing Rectangle for a Line The CRectangle Class . Enumerations 10 9 Specifying a Type for Enumeration Constants 11 1 Specifying Values for Enumeration Constants 11 1 Summary 11 2 Exercises 11 3 Chapter 3: Decisions and Loops 11 5 Comparing Values 11 5 The. 99 C+ +/CLI Specific: Fundamental Data Types 99 C+ +/CLI Output to the Command Line 10 4 C+ +/CLI Specific — Formatting the Output 10 4 C+ +/CLI Input from the Keyboard 10 7 Using safe_cast 10 8 C+ +/CLI. http://www.simpopdf.com xvi Contents Logical Operators and Expressions 12 4 Logical AND 12 5 Logical OR 12 5 Logical NOT 12 6 The Conditional Operator 12 7 The switch Statement 12 9 Unconditional Branching 13 2 Repeating