Borland C++ Power Programming
Total Page:16
File Type:pdf, Size:1020Kb
Borland C++ Power Programming Borland C++ Power Programming Clayton Walnum PROGRAMMING SERIES i SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming Borland C++ Power Programming 1993 by Que Corporation All rights reserved. Printed in the United States of America. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system, without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 11711 N. College Ave., Carmel, IN 46032. Library of Congress Catalog No.: 93-83382 ISBN: 1-56529-172-7 This book is sold as is, without warranty of any kind, either express or implied, respecting the contents of this book, including but not limited to implied warranties for the book’s quality, performance, merchantability, or fitness for any particular purpose. Neither Que Corporation nor its dealers or distributors shall be liable to the purchaser or any other person or entity with respect to any liability, loss, or damage caused or alleged to be caused directly or indirectly by this book. 96 95 94 93 8 7 6 5 4 3 2 1 Interpretation of the printing code: the rightmost double-digit number is the year of the book’s printing; the rightmost single-digit number, the number of the book’s printing. For example, a printing code of 93-1 shows that the first printing of the book occurred in 1993. Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Que cannot attest to 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. Publisher: Lloyd J. Short Associate Publisher: Rick Ranucci Operations Manager: Sheila Cunningham Acquisitions Editor: Joseph B. Wikert ii SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming Dedication To my wife, Lynn, for her love and understanding. iii SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming Credits Title Manager Book Designer Joseph B. Wikert Scott Cook Product Director Cover Designer Jay Munro Jay Corpus Production Editor Graphic Image Specialists Kezia Endsley Jerry Ellis Dennis Sheehan Copy Editors Susan VandeWalle Susan Pink Judy Brunetti Production Team Lori Cates Katy Bodenmiller Julie Brown Technical Editor Laurie Casey Jeffrey D. Clark Brook Farling Editorial Assistants Dennis Clay Hager Elizabeth D. Brown Heather Kaufman Stacey Beheler Bob LaRoche Jay Lesandrini Production Manager Caroline Roop Corinne Walls Linda Seifert Proofreading/Indexing Susan Shepard Coordinator Sandra Shay Joelynn Gifford Phil Worthington Production Analyst Indexer Mary Beth Wakefield Johnna VanHoose Composed in ITC Century Light and MCPdigital by Prentice Hall Computer Publishing. iv SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming About The Author Clayton Walnum has been writing about computers for a decade and has published over 300 articles in major computer publications. He is also the author of 10 books, which cover such diverse topics as programming, computer gaming, and application programs. He lives in Connecticut with his wife and their three children, Christopher, Justin, and Stephen. Acknowledgments I would like to thank the following people for their contribution to this book: Joe Wikert for giving me the project; Jay Munro for his encouragement and invaluable guidance; Kezia Endsley, Susan Pink, Judy Brunetti, and Lori Cates for making the words right; and Jeff Clark for checking the facts. Thanks to Bryan Schappel and Computer Components Inc. of Middleton, Wisconsin for bailing me out when the equipment went bad. Finally, a special thank you is due to some special people—my wife, Lynn, and my three great kids, Christopher, Justin, and Stephen—who make many sacrifices so Dad can do his writing thing. v SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming Overview Introduction . 1 Part I DOS Topics Chapter 1 C++ Style Considerations . 11 Chapter 2 Developing a String Class . 41 Chapter 3 Event-Driven Programming in DOS . 63 Chapter 4 Graphical Controls and Windows for DOS . 91 Chapter 5 Playing with Life . 143 Chapter 6 An Introduction to Recursion . 191 Chapter 7 Using Recursion to Parse Formulas . 229 Chapter 8 Writing Interrupt Handlers and TSR Programs . 259 Part II Windows Topics Chapter 9 Creating Status Bars and Toolbars . 291 Chapter 10 Designing Custom Controls . 345 Chapter 11 Scaling Printer Output . 407 Chapter 12 The Windows Clipboard . 443 Chapter 13 Writing Dynamic Link Libraries . 481 Chapter 14 Using Multimedia Sound with Windows . 501 Chapter 15 Writing Screen Savers for Windows 3.1 . 527 Part III References Appendix A DOS Window Library Quick Reference . 557 Appendix B Detecting Whether a TSR Is Loaded . 569 Index . 579 vi SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming Table of Contents Introduction 1 The New Age . .1 Who This Book Is For . .2 Hardware and Software Requirements . .2 An Overview of the Book . .3 Compiling the Programs in This Book . .5 Conventions Used in This Book . .6 A Word to the Wise . .7 I DOS Topics 1 C++ Style Considerations 11 A Programming History . .12 From Switches to Objects . .12 An Obvious, Yet Brilliant, Solution . .13 A Review of Object-Oriented Programming . .13 Encapsulation . .14 Classes as Data Types . .20 Header Files and Implementation Files . .21 Inheritance . .23 Polymorphism . .26 Classes: From General to Specific . .29 Single-Instance Classes . .34 Responsible Overloading . .36 Overloading Versus Default Arguments . .36 Using Operator Overloading Logically . .37 When to Use Virtual Functions . .39 Conclusion . .40 vii SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming 2 Developing a String Class 41 Designing a String Class . .42 The Components of a String Class . .43 String Construction and Destruction . .47 String Assignments . .48 String Concatenation . .49 String Comparison . .50 String Searches . .51 String Insertion . .52 String Deletion . .53 String Extraction . .54 String Retrieval . .54 Conclusion . .55 3 Event-Driven Programming in DOS 63 What Is an Event-Driven Program? . .64 Developing an Event Handler . .65 Polling for Events . .65 Keyboard Events . .68 Mouse Events . .71 The Complete Mouse Class . .80 The Event Handler . .88 Conclusion . .90 4 Graphical Controls and Windows for DOS 91 Designing the Basic Window . .92 Your Basic Window . .93 Constructing Windw . .94 Destructing Windw . .95 Drawing Windw . .95 Running Windw . .97 Programming the Basic Window . .98 The Captioned Window . .101 Constructing CapWindw . .102 Drawing CapWindw . .103 The Captioned Text Window . .104 Constructing CapTWindw . .105 Drawing CapTWindw . .107 viii SAMS/q3 Borland C++ Power Prog Paula 2-17-93 FM lp7 Borland C++ Power Programming Getting Button Presses . .107 The Button Window . .108 Constructing Button . .109 Drawing Button . .109 Clicking Button . .111 The OK Window . .115 Constructing OKWindw . .116 Destructing OKWindw . .116 Drawing OKWindw . .117 Running OKWindw . .117 The Yes/No and Yes/No/Cancel Windows . .118 The Input Window . .120.