teach yourself borland c++ 4 in 21 days

Upload: emrah-ertem

Post on 12-Oct-2015

44 views

Category:

Documents


0 download

TRANSCRIPT

  • Teach Yourself

    Borland C++ 4in 21 Days

    A Division of Macmillan Computer Publishing201 West 103rd Street, Indianapolis, Indiana 46290

    Namir Clement ShammasCraig ArnushEdward Mulroy

    About this electronic book...Click anywhere on this page to jump to the Table of Contents. See the Help menu if you don't know how to use Acrobat Reader.

  • vi

    Teach Yourself Borland C++ 4 in 21 Days21

    To my nephew, Julian AzizNamir Clement Shammas

    To Bzrblt, who kept me companyCraig Arnush

    To MarieEdward Mulroy

    Copyright 1994 by Sams PublishingFIRST EDITIONFIRST PRINTING1994

    All rights reserved. No part of this book shall be reproduced, stored in a retrievalsystem, or transmitted by any means, electronic, mechanical, photocopying,recording, or otherwise, without written permission from the publisher. No patentliability is assumed with respect to the use of the information contained herein.Although every precaution has been taken in the preparation of this book, thepublisher and author assume no responsibility for errors or omissions. Neither isany liability assumed for damages resulting from the use of the informationcontained herein. For information, address Sams Publishing, a division ofMacmillan Computer Publishing, 201 W. 103rd St., Indianapolis, IN 46290.

    International Standard Book Number: 0-672-30483-X

    Library of Congress Catalog Card Number: 94-65309

    97 96 95 94 4 3 2 1

    Interpretation of the printing code: the rightmost double-digit number is the yearof the books printing; the rightmost single-digit, the number of the booksprinting. For example, a printing code of 94-1 shows that the first printing of thebook occurred in 1994.

    Composed in AGaramond and MCPdigital by Macmillan Computer Publishing

    Printed in the United States of America

    TrademarksAll terms mentioned in this book that are known to be trademarks or servicemarks have been appropriately capitalized. Sams Publishing cannot attest to theaccuracy of this information. Use of a term in this book should not be regarded asaffecting the validity of any trademark or service mark. Borland C++ is a registeredtrademark of Borland International, Inc.

  • vii

    PublisherRichard K. Swadley

    Associate PublisherJordan Gold

    AcquisitionsManager

    Stacy Hiquet

    Managing EditorCindy Morrow

    Acquisitions EditorGrace Buechlein

    Development EditorDean Miller

    Production EditorDeborah Frisby

    Copy EditorJoe Williams

    Editorial andGraphicsCoordinator

    Bill Whitmer

    Editorial AssistantsCarol AckermanSharon CoxLynette Quinn

    Technical ReviewerBob Arnson

    Marketing ManagerGregg Bushyeager

    Cover DesignerDan Armstrong

    Book DesignerMichele Laseau

    Director ofProduction andManufacturing

    Jeff Valler

    Imprint ManagerJuli Cook

    ManufacturingCoordinator

    Paul Gilchrist

    Production AnalystsDennis Clay HagerMary Beth Wakefield

    Graphics ImageSpecialists

    Tim Montgomery

    Dennis SheehanSue VandeWalle

    ProductionSteve AdamsNick AndersonCarol BowersAyrika BryantKim CoferTerri EdwardsGreg KempBetty KishJamie MilazzoWendy OttShelly PalmaChad PooreBeth RagoMichelle SelfSusan ShepardS A SpringerRebecca Tapley

    Indexers

  • i

    DODONT

    About This BookThis book is designed to help you teach yourself the new and necessary aspects of programmingwith Borland C++ 4. In 21 days, youll learn about such fundamentals as the Borland C++ 4environment, managing I/O, loops and arrays, object-oriented programming, and creatingbasic OWL applicationsall in well-structured and easy-to-follow lessons. Each lessonprovides a sample listingcomplete with sample output and an analysis of the codetoillustrate the topics of the day. Syntax examples are clearly marked for handy reference.

    To help you become more proficient, each lesson ends with a set of common questions andanswers, exercises, and a quiz. You can check your progress by examining the quiz answersprovided in the books appendix.

    Who Should Read This BookYou dont need any previous experience in programming to learn Borland C++ with this book.Youll find the numerous examples of syntax and detailed analysis of code an excellent guideas you begin your journey into this rewarding environment. If you have programmed beforeor have some familiarity with C++, you will see that this book treats many important newfeatures of the Borland product. Whether you are just beginning or need only to learn the latestabout Borland C++ 4, this books clear organization makes doing so fast and easy.

    ConventionsNote: These boxes highlight information that can make your Borland C++ 4programming more efficient and effective.

    Warning: These boxes focus your attention on problems or side effects that canoccur in specific situations.

    New Term: These boxes provide clear definitions of essential terms.

    DO use the Do/Dont boxes to find a quick summary of a fundamentalprinciple in a lesson.

    DONT overlook the useful information offered in these boxes.

    This book uses various typefaces to help you distinguish C++ code from regular English.Actual C++ code is typeset is a special monospace font. Placeholderswords or charactersused to represent the real words or characters you would type in codeare typeset in italicmonospace. New or important terms are typeset in italic.

    In the listings in this book, each real code line is numbered. If you see an unnumbered linein a listing, youll know that the unnumbered line is really a continuation of the precedingnumbered code line (some code lines are too long for the width of the book).

  • viii

    Teach Yourself Borland C++ 4 in 21 Days21

    Charlotte ClappJennifer Eberhardt

    OverviewIntroduction xxv

    Week 1 at a Glance 1Day 1 Getting Started 3

    2 C++ Program Components 533 Operators and Expressions 834 Managing I/O 1175 The Decision-Making Constructs 1316 Loops 1537 Arrays 175

    Week 1 in Review 213

    Week 2 at a Glance 217Day 8 User-Defined Types and Pointers 219

    9 Strings 25310 Advanced Function Parameters 28511 Object-Oriented Programming and C++ Classes 31912 Basic Stream File I/O 35713 The C++ string Class 39114 Programming Windows with OWL 2.0 419

    Week 2 in Review 447

    Week 3 at a Glance 451Day 15 Basic Windows 453

    16 OWL Controls 47317 Grouped Controls 50518 List Box Controls 52119 Scroll Bars and Combo Boxes 55320 Dialog Boxes 58121 MDI Windows 601

    Week 3 in Review 631

  • ix

    Extra Credit Bonus SectionBonus 1 Debugging 633

    2 Visual Programming 6493 Using the Application Expert 7154 Generating MDI Applications with AppExpert 7695 Using the ClassExpert 8176 Common Dialog Boxes 865

    AppendixAppendix A Answers 913

    Index 949

  • xi

    ContentsIntroduction xxv

    Week 1 at a Glance 1

    Day 1 Getting Started 3The Basics of C++ Programs ....................................................................... 4Loading the Borland C++ IDE ................................................................... 5An Overview of the Borland C++ IDE ....................................................... 5

    The File Menu ....................................................................................... 7The Edit Menu .................................................................................... 11The Search Menu ................................................................................ 14The View Menu .................................................................................. 19The Project Menu ............................................................................... 28The Debug Menu ................................................................................ 34The Tool Menu ................................................................................... 35The Options Menu .............................................................................. 36The Window Menu ............................................................................. 41The Help Menu ................................................................................... 42

    The EasyWin Applications ....................................................................... 43Your First C++ Program ........................................................................... 44Exiting the IDE ........................................................................................ 48Summary .................................................................................................. 48Q&A ........................................................................................................ 49Workshop ................................................................................................ 50

    Quiz .................................................................................................... 50Exercise ................................................................................................ 51

    2 C++ Program Components 53Predefined Data Types in Borland C++ 4.0 .............................................. 54Naming Items in Borland C++ 4.0 ........................................................... 56The #include Directive .............................................................................. 56Declaring Variables ................................................................................... 57Declaring Constants ................................................................................. 59

    Using Macro-Based Constants ............................................................. 61Using Formal Constants ...................................................................... 62

    Declaring and Prototyping Functions ....................................................... 64Local Variables in Functions ..................................................................... 67Static Variables in Functions .................................................................... 68Inline Functions ....................................................................................... 70Exiting Functions ..................................................................................... 72Default Arguments ................................................................................... 72Function Overloading .............................................................................. 75Summary .................................................................................................. 78

  • xii

    Teach Yourself Borland C++ 4 in 21 Days21

    Q&A ........................................................................................................ 79Workshop ................................................................................................ 80

    Quiz .................................................................................................... 80Exercise ................................................................................................ 82

    3 Operators and Expressions 83Arithmetic Operators ................................................................................ 84Arithmetic Expressions ............................................................................. 87Increment Operators ................................................................................ 88Assignment Operators .............................................................................. 91The sizeof Operator .................................................................................. 94Typecasting .............................................................................................. 96Relational and Logical Operators ............................................................ 100Boolean Expressions ............................................................................... 102Bit-Manipulation Operators ................................................................... 106The Comma Operator ............................................................................ 108Operator Precedence and Evaluation Direction ...................................... 109Summary ................................................................................................ 112Q&A ...................................................................................................... 112Workshop .............................................................................................. 113

    Quiz .................................................................................................. 113Exercises ............................................................................................ 115

    4 Managing I/O 117Formatted Stream Output ...................................................................... 118Stream Input .......................................................................................... 120The printf Function ................................................................................ 123Summary ................................................................................................ 128Q&A ...................................................................................................... 129Workshop .............................................................................................. 129

    Quiz .................................................................................................. 129Exercises ............................................................................................ 130

    5 The Decision-Making Constructs 131The Single-Alternative if Statement ....................................................... 132The Dual-Alternative if-else Statement .................................................... 134

    Potential Problems with the if Statement ........................................... 136The Multiple-Alternative if-else Statement .............................................. 137The switch Statement .............................................................................. 141Nested Decision-Making Constructs ...................................................... 145Summary ................................................................................................ 147Q&A ...................................................................................................... 148Workshop .............................................................................................. 150

    Quiz .................................................................................................. 150Exercises ............................................................................................ 151

  • xiii

    6 Loops 153The for Loop .......................................................................................... 154

    Open Loops Using the for Loops ....................................................... 158The do-while Loop .................................................................................. 160The while Loop ...................................................................................... 163Skipping Loop Iterations ........................................................................ 164Exiting Loops ......................................................................................... 167Nested Loops .......................................................................................... 168Summary ................................................................................................ 170Q&A ...................................................................................................... 171Workshop .............................................................................................. 172

    Quiz .................................................................................................. 172Exercises ............................................................................................ 174

    7 Arrays 175Declaring Single-Dimensional Arrays ..................................................... 176Using Single-Dimensional Arrays ........................................................... 177Initializing Single-Dimensional Arrays .................................................... 180Array Parameters in Functions ................................................................ 184Sorting Arrays ......................................................................................... 187Searching Arrays ..................................................................................... 192Multidimensional Arrays ........................................................................ 199Initializing Multidimensional Arrays ...................................................... 203Multidimensional Array Parameters ........................................................ 205Summary ................................................................................................ 208Q&A ...................................................................................................... 210Workshop .............................................................................................. 211

    Quiz .................................................................................................. 211Exercise .............................................................................................. 212

    Week 1 in Review 213

    Week 2 at a Glance 217

    Day 8 User-Defined Types and Pointers 219Type Definition in C++ .......................................................................... 220Enumerated Data Types ......................................................................... 221Structures ............................................................................................... 225Unions ................................................................................................... 230Reference Variables ................................................................................. 230Overview of Pointers .............................................................................. 233Pointers to Existing Variables ................................................................. 233Pointers to Arrays ................................................................................... 236The Pointer Increment/Decrement Method ........................................... 239Pointers to Structures ............................................................................. 241Pointers and Dynamic Memory .............................................................. 244

  • xiv

    Teach Yourself Borland C++ 4 in 21 Days21

    Far Pointers ............................................................................................ 248Summary ................................................................................................ 249Q&A ...................................................................................................... 250Workshop .............................................................................................. 251

    Quiz .................................................................................................. 251Exercises ............................................................................................ 252

    9 Strings 253C++ Strings: An Overview ...................................................................... 254String Input ............................................................................................ 255Using the STRING.H Library ................................................................ 256Assigning Strings .................................................................................... 256The Length of a String ........................................................................... 259Concatenating Strings ............................................................................ 259String Comparison ................................................................................. 263Converting Strings .................................................................................. 268Reversing Strings .................................................................................... 270Locating Characters ................................................................................ 273Locating Strings ...................................................................................... 275Summary ................................................................................................ 280Q&A ...................................................................................................... 281Workshop .............................................................................................. 283

    Quiz .................................................................................................. 283Exercises ............................................................................................ 284

    10 Advanced Function Parameters 285Passing Arrays as Arguments ................................................................... 286Using Strings as Arguments .................................................................... 289Using Structures as Arguments ............................................................... 291Passing Arguments by Reference ............................................................. 293Passing Structures by Reference .............................................................. 293Passing Structures by Pointers ................................................................ 295Recursive Functions ................................................................................ 297Passing Pointers to Dynamic Structures .................................................. 300Pointers to Functions ............................................................................. 304Summary ................................................................................................ 314Q&A ...................................................................................................... 315Workshop .............................................................................................. 316

    Quiz .................................................................................................. 316Exercise .............................................................................................. 318

    11 Object-Oriented Programming and C++ Classes 319Basics of Object-Oriented Programming ................................................ 320

    Classes and Objects ............................................................................ 321Messages and Methods ...................................................................... 321Inheritance ........................................................................................ 321Polymorphism ................................................................................... 322

  • xv

    Declaring Base Classes ............................................................................ 322The Sections of a Class ...................................................................... 323

    Constructors ........................................................................................... 327Destructors ............................................................................................. 330

    Examples of Constructors and Destructors ........................................ 331Declaring a Class Hierarchy .................................................................... 334Virtual Functions ................................................................................... 339

    Rules for Virtual Functions ................................................................ 343Friend Functions .................................................................................... 345Operators and Friend Operators ............................................................. 349Summary ................................................................................................ 353Q&A ...................................................................................................... 354Workshop .............................................................................................. 355

    Quiz .................................................................................................. 355Exercise .............................................................................................. 356

    12 Basic Stream File I/O 357The C++ Stream Library ......................................................................... 358Common Stream I/O Functions ............................................................. 359Sequential Text Stream I/O .................................................................... 361Sequential Binary File Stream I/O .......................................................... 366Random Access File Stream I/O ............................................................. 373Exception Handling ............................................................................... 379Summary ................................................................................................ 388Q&A ...................................................................................................... 389Workshop .............................................................................................. 390

    Quiz .................................................................................................. 390Exercise .............................................................................................. 390

    13 The C++ string Class 391Benefits of the C++ string Class ............................................................... 392The string Class Header File CSTRING.H ............................................. 394Bug Busters ............................................................................................ 402Reading and Comparing Strings ............................................................. 402String Search, Substitution, and File I/O ................................................ 407Other C++ string Class Functions ........................................................... 413Summary ................................................................................................ 415Q&A ...................................................................................................... 415Workshop .............................................................................................. 416

    Quiz .................................................................................................. 416Exercises ............................................................................................ 417

    14 Programming Windows with OWL 2.0 419Templates ............................................................................................... 420OWL and Windows Issues ..................................................................... 425Hungarian Notation ............................................................................... 427

  • xvi

    Teach Yourself Borland C++ 4 in 21 Days21

    The Basic Structure of OWL .................................................................. 428Event Handling, TEventHandler ........................................................ 428Streamable or Persistent Objects, TStreamableBase ............................ 428

    Module ManagementTModule and TApplication ................................ 428Window Management, TWindow ...................................................... 428

    A Sample OWL Program ....................................................................... 429Windows Messages and OWL ................................................................ 431A Real OWL Program: Resources, Menus, Screen Writing ..................... 433Summary ................................................................................................ 442Q&A ...................................................................................................... 443Workshop .............................................................................................. 444

    Quiz .................................................................................................. 444Exercise .............................................................................................. 445

    Week 2 in Review 447

    Week 3 at a Glance 451

    Day 15 Basic Windows 453Creating a Read-Only Text Window ...................................................... 454Scrolling Through Text .......................................................................... 459A Scrolling Window ............................................................................... 462The SetupWindow Member Function ..................................................... 470Summary ................................................................................................ 471Q&A ...................................................................................................... 472Workshop .............................................................................................. 472

    Quiz .................................................................................................. 472Exercise .............................................................................................. 472

    16 OWL Controls 473The TControl Object .............................................................................. 474The Static Text Control ......................................................................... 475The Edit Control .................................................................................... 479

    The TEdit Class ................................................................................. 479Clipboard-Related Editing Functions ................................................ 482Query of Edit Controls ...................................................................... 482Altering the Edit Controls ................................................................. 485

    The Pushbutton Control ........................................................................ 486The TButton Class ............................................................................. 487Handling Button Messages ................................................................ 488Manipulating Buttons ........................................................................ 488

    Mr. Calculator ........................................................................................ 489Summary ................................................................................................ 501Q&A ...................................................................................................... 502Workshop .............................................................................................. 502

    Quiz .................................................................................................. 502Exercises ............................................................................................ 503

  • xvii

    17 Grouped Controls 505The Check Box Control ......................................................................... 506

    The TCheckBox Class ......................................................................... 507Responding to Check Box Messages .................................................. 508

    The Radio Button Control ..................................................................... 508The TRadioButton Class .................................................................... 508

    The Group Control ................................................................................ 509The TGroupBox Class ........................................................................ 510

    The Widget Selection Application .......................................................... 510Summary ................................................................................................ 518Q&A ...................................................................................................... 519Workshop .............................................................................................. 519

    Quiz .................................................................................................. 519Exercise .............................................................................................. 520

    18 List Box Controls 521The List Box Control ............................................................................. 522The TListBox Class ................................................................................. 522

    Responding to List Box Notification Messages .................................. 532The List Manipulation Tester ............................................................ 533

    Handling Multiple-Selection Lists .......................................................... 543The Multiple-Selection List Tester ..................................................... 544

    Summary ................................................................................................ 550Q&A ...................................................................................................... 550Workshop .............................................................................................. 551

    Quiz .................................................................................................. 551Exercise .............................................................................................. 551

    19 Scroll Bars and Combo Boxes 553The Scroll Bar Control ........................................................................... 554

    The TScrollBar Class .......................................................................... 554Responding to Scroll Bar Notification Messages ................................ 558

    The Countdown Timer .......................................................................... 559The Combo Box Control ....................................................................... 565

    Responding to Combo Box Notification Messages ............................ 568Combo Boxes as History List Boxes ................................................... 570

    The Son of Mister Calculator Application .............................................. 570Summary ................................................................................................ 578Q&A ...................................................................................................... 578Workshop .............................................................................................. 579

    Quiz .................................................................................................. 579Exercise .............................................................................................. 579

    20 Dialog Boxes 581Constructing Dialog Boxes ..................................................................... 582Creating Dialog Boxes ............................................................................ 584Connecting OWL Objects with Windows Controls ............................... 590

  • xviii

    Teach Yourself Borland C++ 4 in 21 Days21

    Transferring Control Data ...................................................................... 591Data Transfer for Modal Dialog Boxes ................................................... 593Transferring Data for Modeless Dialog Boxes ......................................... 598Summary ................................................................................................ 599Q&A ...................................................................................................... 600Workshop .............................................................................................. 600

    Quiz .................................................................................................. 600Exercises ............................................................................................ 600

    21 MDI Windows 601The MDI Application Features and Components ................................... 602Basics of Building an MDI Application .................................................. 603The TMDIFrame Class ........................................................................... 604Building MDI Frame Windows .............................................................. 605The TMDIClient Class ........................................................................... 606The MDI Child Window Class .............................................................. 608Building MDI Child Windows ............................................................... 609Managing MDI Messages ....................................................................... 610Simple Text Viewer ................................................................................ 610Revised Text Viewer ............................................................................... 618Summary ................................................................................................ 629Q&A ...................................................................................................... 629Workshop .............................................................................................. 629

    Quiz .................................................................................................. 630Exercises ............................................................................................ 630

    Week 3 in Review 631

    Bonus 1 Debugging 633The Integrated Debugger ........................................................................ 634

    The Debug Menu .............................................................................. 634The View Menu ................................................................................ 638

    Debugging a Program ............................................................................. 639Other Debugging Tools ......................................................................... 646Summary ................................................................................................ 647Q&A ...................................................................................................... 647

    2 Visual Programming 649Resource Workshop Overview ................................................................ 650Types of Resources ................................................................................. 651

    Accelerators ....................................................................................... 651Bitmaps ............................................................................................. 652Cursors .............................................................................................. 652Dialog Boxes ...................................................................................... 652Fonts ................................................................................................. 652Icons .................................................................................................. 652Menus ............................................................................................... 652

  • xix

    String Tables ...................................................................................... 653User-Defined and rcdata Resources .................................................... 653VERSIONINFO ............................................................................... 653

    Resource Files ......................................................................................... 653Creating Menu Resources ....................................................................... 654Creating Accelerator Resources ............................................................... 668Creating Icon Resources ......................................................................... 677Creating Dialog Box Resources ............................................................... 680

    The Tools Palette ............................................................................... 681The Alignment Palette ....................................................................... 682

    Creating a Bare-Bones Dialog Box Resource ........................................... 683Creating Dialog Box Resources with Basic Controls ............................... 689Creating Dialog Box Resources with Grouped Controls ......................... 695Creating a Fully Operational Dialog Box ................................................ 705

    The TMainWindow Class .................................................................. 711The TCalcDialog Class ....................................................................... 712

    Summary ................................................................................................ 713Q&A ...................................................................................................... 714

    Exercises ............................................................................................ 714

    3 Using the Application Expert 715Using the AppExpert Utility ................................................................... 716

    The Application Topic ...................................................................... 717The Main Window Topic .................................................................. 721The MDI Child/View Topic ............................................................. 724

    Studying the AppExpert Output ............................................................. 727The XPED1 Project ............................................................................... 728The XPED2 Project ............................................................................... 756Summary ................................................................................................ 766Q&A ...................................................................................................... 766

    Exercises ............................................................................................ 767

    4 Generating MDI Applications with AppExpert 769The XPED3 Project ............................................................................... 770The XPED4 Project ............................................................................... 786The XPED5 Project ............................................................................... 807Summary ................................................................................................ 814Q&A ...................................................................................................... 814

    Exercises ............................................................................................ 815

    5 Using the ClassExpert 817Invoking ClassExpert .............................................................................. 818Adding New Member Functions ............................................................ 819Adding a Class ........................................................................................ 840Summary ................................................................................................ 862Q&A ...................................................................................................... 863

    Exercises ............................................................................................ 863

  • xx

    Teach Yourself Borland C++ 4 in 21 Days21

    6 Common Dialog Boxes 865Software Requirements ........................................................................... 866The TInputDialog Class .......................................................................... 867The TCommonDialog Class .................................................................... 873The File Dialog Classes .......................................................................... 874

    The Supporting Classes and Structures .............................................. 875Invoking the File Dialog Box ............................................................. 880The File Statistics Program ................................................................ 880

    The TChooseColorDialog Class ................................................................ 885Supporting Classes and Structures ..................................................... 885A Sample Program ............................................................................. 888

    The Find and Replace Dialog Classes ..................................................... 892Supporting Classes and Structures ..................................................... 894The TFindReplaceDialog Class ........................................................... 894The TFindDialog Class ...................................................................... 896The TReplaceDialog Class .................................................................. 897Notifying the Parent Window ........................................................... 897A Sample Program ............................................................................. 901

    Summary ................................................................................................ 910Q&A ...................................................................................................... 911

    Exercises ............................................................................................ 911

    Appendix A Answers 913Answers to Day 1, Getting Started .................................................. 914Answers to Day 2, C++ Program Components ............................... 914Answers to Day 3, Operators and Expressions ................................ 917Answers to Day 4, Managing I/O ................................................... 918Answers to Day 5, The Decision-Making Constructs ..................... 920Answers to Day 6, Loops ................................................................ 924Answers to Day 7, Arrays ................................................................ 926Answers to Day 8, User-Defined Types and Pointers ...................... 928Answers to Day 9, Strings ............................................................... 931Answers to Day 10, Advanced Function Parameters ....................... 934Answers to Day 11, Object-Oriented

    Programming and C++ Classes ...................................................... 939Answers to Day 12, Basic Stream File I/O ...................................... 940Answers to Day 13, The C++ string Class ....................................... 942Answers to Day 14, Programming Windows with OWL 2.0 .......... 944Answers to Day 15, Basic Windows ................................................ 945Answers to Day 16, OWL Controls ................................................ 945Answers to Day 17, Grouped Controls ........................................... 946Answers to Day 18, List Box Controls ............................................ 946Answers to Day 19, Scroll Bars and Combo Boxes ......................... 946Answers to Day 20, Dialog Boxes ................................................... 947Answers to Day 21, MDI Windows ............................................... 947

    Index 949

  • xxi

    AcknowledgmentsI would like to thank Grace Buechlein at Sams Publishing for having the patience todeal with the likes of me. Also, thanks to Dean Miller, who listened to my suggestionsand even liked my bugs. My thanks go out to all those at Sams who participated inputting this book together and eventually into your hands. Craig Arnush

    I would like to thank Grace Buechlein, who provided kind, frank guidance; and PeterAitken, whose responses convinced me to do this. Special thanks to my wife Marie,who has been supportive throughout. Edward Mulroy

    I wish to thank the many people at Sams for encouraging me and working with meon this project. First, I would like to thank Publisher Richard Swadley and AssociatePublisher Jordan Gold for their support. Many thanks also to Grace Buechlein, DeanMiller, Joe Williams, and Deborah Frisby for their first-class work. Thanks to all whoparticipated in producing this book. Namir Clement Shammas

    Finally, all of the authors wish to thank Robert Arnson, technical editor for this book.Arnson, who used to work in Borlands Technical Publications department on C++projects, is now a free-lance writer, editor, and consultant. Hes also a member ofTeam Borland, Borlands group of volunteers who help support Borland products onCompuServe, GEnie, and BIX. His speciality is application frameworks, and he hasworked with OWL for more than three years. You can contact him on CompuServe(72662,1376) and GEnie (ARNSON). You can also contact him via Internet mailat [email protected].

  • xxiii

    About the AuthorsNamir Clement Shammas is a full-time author of programming books and an expertin object-oriented programming. He has written and coauthored more than 40 bookson programming languages such as C++, C, Pascal, and Visual Basic. Among his manybooks are Advanced C++, Teach Yourself Visual C++ in 21 Days, and What EveryBorland C++ 4 Programmer Should Know.

    Craig Arnush is an independent software consultant in San Diego and is an experton Windows. He volunteers his time answering technical questions on the BorlandCompuServe forums as a member of Team Borland. Craig can be reached via hisCompuServe account at 71333,3052 or via the Internet at [email protected].

    Edward Mulroy is Chief Engineer for RF Data Corporation, a company specializingin communications and embedded work. Since 1990, he has assisted fellow users ofC++, C, and Assembler on the Borland CompuServe forums as a member of TeamBorland.

  • xxv

    IntroductionThis book has three major goals: teaching you to program in C++, teaching you tocreate Windows applications using Borland C++, and teaching you the new featuresof Borland C++ 4. No prior programming experience is required. However, knowinghow to program in other languages, such as BASIC or Pascal, certainly helps. Thisbook is not for the faint-hearted, because becoming familiar with the new features ofa new compiler is hard enough, but also learning to program in C++ and learning towrite Windows applications in C++ are two nontrivial tasks!

    The book contains 21 chapters, one for each study day. The material is somewhat fast-paced in order to meet the goals of the book. Each chapter contains a Q&A section,a quiz section, and an exercise section. In the back of the book youll find the answersto the quizzes and to many of the exercises.

    Day 1 gives you a brief tour of the Borland C++ IDE, the Windows environment thatyou use to develop C++ programs. The chapter also presents your first C++ programto demonstrate the basic components of a non-Windows C++ program.

    Day 2 looks at C++ program components in more detail. The chapter discussesnaming and declaring variables, constants, and functions. The book also provides anearly focus on C++ functions because they are important program building blocks.

    Day 3 presents the various C++ operators and expressions. Operators enable you tomanipulate data and form expressions that support more complex data manipulation.

    Day 4 discusses formatted stream input and output, as well the famous printffunction. The latter function supports versatile formatted output.

    Day 5 covers C++ decision-making constructs. These constructs include the variouskinds of if statements as well as the switch statement.

    Day 6 discusses C++ loops, including the for, do-while, and while loops. Thechapter demonstrates how to use the for loop as an open loop. In addition, thechapter discusses skipping loop iterations, exiting loops, and nesting loops.

    Day 7 presents arrays in C++. The chapter covers both single-dimensional andmultidimensional arrays and discusses how to declare them and initialize them. Inaddition, the chapter discusses sorting and searching single-dimensional arrays.

    Day 8 covers user-defined types and pointers. The chapter discusses enumerated datatypes, structures, unions, reference variables, and pointers. The text demonstrateshow to declare and use pointers with simple variables, arrays, structures, and dynamicmemory.

  • xxvi

    Teach Yourself Borland C++ 4 in 21 Days21

    Day 9 focuses on strings and the STRING.H library, which is inherited from C. Thechapter covers topics like assigning, concatenating, comparing, converting, andreversing strings. In addition, the chapter discusses searching for characters andsubstrings in strings.

    Day 10 discusses advanced function parameters and mainly covers parameters that arearrays, strings, structures, and pointers to functions. The chapter also discusses thevarious ways to pass structures as parameters and presents recursive functions.

    Day 11 introduces you to the world of object-oriented programming (OOP). Thechapter covers the basics of OOP and presents C++ classes. The text discusses thebasic components of a C++ class and the rules related to using these components.

    Day 12 discusses the basic stream file I/O, which is supported by the C++ streamlibrary. The chapter covers common stream functions, sequential text stream I/O,sequential binary stream I/O, and random-access stream I/O.

    Day 13 covers the string class, an alternative to strings, and the functions inSTRING.H that work with them. This class conforms to the preliminary strings classfrom the ANSI C++ committee and is prototyped in the header file CSTRING.H.

    Day 14 presents very simple OWL-based Windows applications. Object WindowsLibrary version 2.0, or OWL2, is included with BC++ 4. It is a C++ library for use inWindows programming, and using it shortens the time and effort in developing aWindows program. OWL is written in C++ and uses a feature of that language calledtemplates, which you also learn on Day 14.

    Day 15 focuses on drawing text in a window. The chapter presents both nonscrollingand scrolling windows and illustrates how to draw text (as graphics) in these windows.

    Day 16 presents the OWL library classes, which model static text controls, editcontrols, and pushbutton controls. The chapter also presents a nontrivial command-oriented line calculator as an example that uses these controls.

    Day 17 presents the OWL library classes that model the check box control, the radiobutton control, and the group control.

    Day 18 covers the OWL library class that models list box controls. The chapterdiscusses both single-selection and multiple-selection list boxes. The programs in thechapter illustrate both kinds of list boxes.

  • xxvii

    Day 19 presents the OWL library classes, which model the scroll bar control and thecombo box control. The chapter also discusses how to create history boxes usingcombo boxes. In addition, the chapter presents a version of the calculator programthat uses the combo boxes.

    Day 20 focuses on creating and using dialog boxes. The chapter shows you how to useresource files to define modal and modeless dialog boxes. In addition, the chapterdiscusses data transfer between a dialog box and its parent window.

    Day 21 looks at Multiple Document Interface (MDI) windows. The chapter presentsthe classes that support MDI-compliant applications and illustrates how to manageMDI-child windows.

    The bonus chapters present the new features of Borland C++ 4, including AppExpert,ClassExpert, the Resource Workshop, and the debugging tools. In addition, you geta bonus chapter on common dialog boxes.

    The book contains Windows programs that illustrate aspects of programming that gobeyond the trivial aspects of using various visual controls. Study these programs, asthey contain techniques and tricks that can enrich your Windows programming. Weall learn to program by looking at examples (including nontrivial ones) and by askingfriends questions.

    In the back of the book is an explanation of how to obtain a companion disk thatincludes the source code and the project files presented in this book. You can alsodownload the files from CompuServe; type: GO SAMS. The Files can be found in theSams Programming Library 9.

    Happy programming!

  • Sam

    sLe

    arn

    ing

    Cent

    er

    abcd

    1

    A/ns6 TY Borland C++ in 21 Days #30483 Lisa D 4-14-94 AAG1 LP#2(sp 4/12 folio)

    MT

    WR

    FS

    S1

    2

    3

    4

    5

    6

    7

    1WEEK

    AT A

    GLA

    NC

    E

    The first week of your journey into learning to writeWindows applications starts with an introduction to theBorland C++ 4.0 environmentthe IDE (integrateddevelopment environment). The remaining days in thisweek present the basics of the C++ language itself. Youlearn about predefined data types; naming constants,variables, and functions; C++ operators and expressions;managing basic input and output; making decisions;writing loops; and declaring and using arrays. Thus, thisweek covers the basic components of the C++ language.

  • 3Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    MT

    WR

    FS

    S

    WEEK

    11Getting Started

    11

  • 4Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Welcome to the world of C++ and Windows programming. Your journey into thisexciting world begins today. Most of the information in todays lesson familiarizes youwith the Borland C++ Integrated Development Environment (IDE). You will learnabout the following topics:

    nn The basics and history of C++ programs

    nn Loading and using the Borland C++ IDE

    nn The EasyWin applications

    nn Typing and running your first C++ program

    The Basics of C++ ProgramsYou dont need any previous experience in programming to learn Borland C++ withthis book; but if you have programmed before, things will be easier. As with otherlanguages, C++ is made up of declarations and statements that specify exact instruc-tions to be executed when the program runs.

    C++ was developed by Bjarn Stroustrup at Bell Labs. The language is meant tosupersede and build on the popular C language, mainly by adding object-orientedlanguage extensions.

    *

    New Term: An object-oriented language represents the attributes andoperations of objects.

    In addition, C++ offers a number of enhancements to C that are not object-oriented.Thus, learning C++ gives you the bonus of becoming very familiar with C. However,unlike C, which has been standardized, C++ is still undergoing the standardizationprocess.

    Programming in C++ requires that you become aware of the supporting libraries,which perform various tasks such as input/output, text manipulation, math opera-tions, file I/O (input/output), and so on. In languages such as BASIC, support for suchoperations is transparent to programs, meaning that it is automatically available tothese programs. As a result, many programs come across as single components that areindependent of any other programming components. By contrast, programming inC++ makes you more aware of a programs dependency on various libraries. The

  • 5Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    advantage of this language feature is that you are able to select between similarlibraries, including ones that you develop. Thus, C++ programs are modular. C++compilers, including Borland C++, use project files and program files. The BorlandC++ IDE uses project files to manage the creation and updating of a program.

    *

    New Term: Project files specify the library. Program files create an applica-tion.

    Loading the Borland C++ IDEThe Borland C++ IDE is the visual interface for the C++ compiler, linker, debugger,and other tools that are used to create, manage, and maintain C++ programs. You canload the IDE by simply clicking the Borland C++ icon or by double-clicking theBCW.EXE program from the File Manager. (The file BCW.EXE is located in thedirectory \BC4\BIN.)

    An Overview of theBorland C++ IDE

    The Borland C++ IDE is an MDI-compliant application with the following maincomponents:

    nn The frame window with the menu system, minimize, and maximize icons.You can resize, move, maximize, and minimize the Borland C++ IDEwindow. This window has a title that reflects the name of the active window.

    nn The system menu, which offers numerous options.

    nn The speed bar, which contains special bitmapped buttons that offer short-cuts to specific commands. The IDE enables you to customize thebitmapped buttons in the speed bar. In addition, these buttons are contextsensitive. Their number and type can change, depending on the current taskor active window. The IDE supports a nice feature that displays what abitmapped button does (the text appears in the status line) when you movethe mouse over that button.

  • 6Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    nn The client area, which contains various windows, such as the source-codeediting window, the message window, the variable watch window, and soon.

    nn The status line located at the bottom of the IDE window. This line displaysbrief online help as you move the mouse over the buttons in the speed bar,offers a brief explanation for the various menu items, displays the cursorlocation, and shows the status of the insert/overwrite mode.

    Figure 1.1 shows a sample session with the Borland C++ IDE.

    Figure 1.1. The Borland C++ IDE.

    Note: Because the IDE is meant to accommodate software developers,many of the options will seem advanced to you if you are a noviceprogrammer. However, you only need to be familiar with the options andtheir related terms. As you become more experienced, these options andterms will become part of your knowledge as a Borland C++ programmer.

  • 7Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    The File MenuThe File menu provides options to manage files, to print text, and to exit the IDE.Table 1.1 summarizes the options in the File menu. The File menu also includes adynamic list of the most recently opened source-code files.

    Table 1.1. Summary of the options in the File menu.

    Command Shortcut Keys Function

    New Opens a new edit window.

    Open Loads an existing source-code file into anew edit window.

    Save Ctrl+K S Saves the contents of the active editwindow.

    Save as Saves the contents of the active editwindow using a new filename.

    Save all Saves all of the opened source-codewindows in their respective files.

    Print Prints the contents of a source codewindow.

    Print setup Sets up the printer.

    Exit Exits the IDE.

    The New CommandThe New option opens a new edit window (also known as a source-code window) andassigns it a default associated filename. The default filename of the first new windowyou open is NONAME00.CPP. Likewise, the default filename of the second newwindow is NONAME01.CPP, and so on. The newly opened window is initiallyempty and has the same window size and location of the last active window. In otherwords, if the last active window was maximized, the new window will also bemaximized.

  • 8Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    The Open OptionThe Open option enables you to load the contents of an existing source code fileinto a new edit window. In fact, the IDE is able to load multiple files. The optioninvokes the Open a File dialog box, shown in Figure 1.2. The dialog box has severallist box and combo box controls that enable you to locate the source-code file and thenselect it. These controls permit you to choose the drive, directory, and filenamewildcards that help you to locate the source-code file you seek.

    Figure 1.2. The Open a File dialog box.

    The Save OptionThe Save option assists you in saving the contents of the active edit window to itsassociated file. If you invoke this option with a new edit window, the Save optioninvokes the Save File As dialog box, shown in Figure 1.3. This dialog box enables youto optionally specify the nondefault filename, as well as the destination drive anddirectory. The shortcut keys for the Save option are Ctrl+K S.

  • 9Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Figure 1.3. The Save File As dialog box.

    The Save As OptionThe Save As option enables you to save the contents of the active edit window ina file that is different from the currently associated file. In fact, the new filenamebecomes the new associated file for the active edit window. The Save As optioninvokes the Save File As dialog box, shown in Figure 1.3. If you select an existing file,the option brings up a message dialog box to ask you if you wish to overwrite thecontents of the existing file with those of the active edit window.

    The Save All OptionThe Save All option writes the contents of all the modified edit windows to theirassociated files. If the IDE contains new edit windows, this option invokes the SaveFile As dialog box to save these new windows.

    The Print OptionThe Print option enables you to print the contents of the active edit window. Theoption brings up the Print Options dialog box, shown in Figure 1.4. This dialog boxhas check boxes for the following options:

  • 10

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    nn Print a header and page numbers

    nn Print line numbers

    nn Highlight syntax keywords by printing them in bold characters

    nn Use color (if your printer supports colors)

    nn Wrap lines

    nn Left margin edit box option

    Figure 1.4. The Print Options dialog box.

    The Print Setup OptionThe Print Setup option enables you to set up your printer using the Print optionbefore you print. The printer setup option brings up the Setup dialog box, shown inFigure 1.5. (The dialog box in this figure is based on a system that has an HP LaserJetIII.) This dialog box contains controls that enable you to specify the following items:

    nn The paper size.

    nn The paper source.

    nn The number of copies to print.

  • 11

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    nn The amount of printer memory.

    nn The orientation of the printout.

    nn The selected font cartridges and fonts.

    nn Page protection to reserve additional memory for printing a page. Thisoption is available only when you have more than one 1 MB of printermemory.

    Figure 1.5. The Setup dialog box.

    The Exit OptionThe Exit option enables you to exit the Borland C++ IDE altogether. The IDEprompts you for any modified edit window that has not been saved.

    The Edit MenuThe Edit menu contains options that enable you to edit the text in the edit windows.Table 1.2 summarizes the options in the Edit menu.

  • 12

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Table 1.2. Summary of the options in the Edit menu.

    Command Shortcut Keys Function

    Undo Ctrl+Z Undoes the last editing action.

    Redo Shift+Ctrl+Z Reverses the action of the last Undooption.

    Cut Ctrl+X Deletes the selected text and copies itto the Clipboard. The previouscontents of the Clipboard are lost.

    Copy Ctrl+C Copies the selected text to the Clip-board. The previous contents of theClipboard are lost.

    Paste Ctrl+V Inserts the contents of the Clipboardat the current cursor location.

    Clear Ctrl+Delete Deletes selected text but does notwrite it to the Clipboard.

    Select all Selects all of the text in the active editwindow.

    Buffer list Displays the Buffer List dialog box.

    The Undo OptionThe Undo option enables you to reverse the effect of the last editing task and restorethe contents of the active edit window. The shortcut keys for this option are Ctrl+Z.This option enables you to quickly and efficiently deal with editing errorsespeciallyafter working long hours.

    The Redo OptionThe Redo option enables you to reverse the action of the Undo option. The shortcutkeys for the Redo option are Shift+Ctrl+Z. The Redo option enables you to switchbetween two versions of edited source code. This option is beneficial to the trulyexhausted programmer who cannot make up his mind about how the source codeshould look!

  • 13

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    The Cut OptionThe Cut option deletes selected text and places it in the Clipboard. The previouscontents of the Clipboard are lost. The shortcut keys for the Cut option are Ctrl+X.

    The Copy OptionThe Copy option copies the selected text into the Clipboard. The previous contentsof the Clipboard are lost. The shortcut keys for the Copy option are Ctrl+C.

    The Paste OptionThe Paste option inserts the contents of the Clipboard at the current insertion point.The contents of the Clipboard remain unaffected. Thus you can use the Cut and Pasteoptions to move text in the same edit window or across different edit windows. Youcan also use the Copy and Paste options to duplicate blocks of text in the same editwindow or across different edit windows. The shortcut keys for the Paste option areCtrl+V.

    The Clear OptionThe Clear option clears the selected text without copying it to the Clipboard. Thisdoes not mean that the deleted text is irreversibly lost, because you can use the Undooption to undelete that text. The shortcut keys for the Clear option are Ctrl+Delete.

    The Select All OptionThe Select All option selects all of the text in the active edit window. You can copythis text to the Clipboard by using the Copy option. Then you can write the contentsof the Clipboard to another edit window using the Paste option.

    The Buffer List OptionThe Buffer List option enables you to examine the list of buffers used with thevarious edit windows. This option brings up the Buffer List dialog box, shown inFigure 1.6. The dialog box enables you to load a buffer into an edit window. Thedialog box contains the list of buffers; those that have changed since they were lastloaded have the word MODIFIED (placed in parentheses) after them. The dialog boxenables you to replace the contents of an edit window without closing the associatedfile. If the replaced file is not loaded into another edit window, it is hidden. You mayuse the buffer list later in order to load the hidden buffer into an edit window.

  • 14

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    You can use the Save pushbutton of the Buffer List dialog box to update the fileassociated with the selected buffer. This action causes the word MODIFIED to disappearfrom the selected buffer entry. You may also use the Delete pushbutton to remove theselected buffer from memory, if that buffer is not in an Edit window.

    Figure 1.6. The Buffer List dialog box.

    The Search MenuThe Search menu contains options that enable you to locate various kinds ofinformation, such as text, symbol definitions, function declarations, and program-building errors. Table 1.3 summarizes the options in the Search menu.

    Table 1.3. Summary of the options in the Search menu.

    Command Shortcut Keys Function

    Find Ctrl+Q F Searches for text in the active editwindow.

    Replace Ctrl+Q A Replaces text in the active source-code window.

    Search again F3 Repeats the last Find or Replaceoperation.

  • 15

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Command Shortcut Keys Function

    Browse symbol Locates a symbol in any source codethat is part of the current project.

    Locate function Locates a function.

    Previous message Alt+F7 Selects the previous program-building message and places thecursor at the offending line in an editwindow.

    Next message Alt+F8 Selects the next program-buildingmessage and places the cursor at theoffending line in an edit window.

    The Find OptionThe Find option supports searches for text in the active edit window. This option,which has the shortcut keys Ctrl+Q F, brings up the Find Text dialog box, shown inFigure 1.7. This dialog box has the following controls:

    nn The Text to find combo box control, which enables you either to type inthe search text or to recall recently searched text.

    nn The Options check boxes, which include

    nn The Case sensitive check box, which enables you to select case-sensitive or case-insensitive text search.

    nn The Whole words only check box, which enables you to choosebetween matching entire words or matching any text.

    nn The Regular expression check box, which turns on or off the use ofthe BRIEF editors regular expressions feature. Such expressions resultin using the text in the Text to find control as the text pattern.

    nn The Direction diamond-shaped radio button controls. These controlsenable you to choose between forward and backward search.

    nn The Scope diamond-shaped radio button controls. These controlsenable you to choose between searching the entire text and limiting thesearch to the selected text.

  • 16

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    nn The Origin diamond-shaped radio button controls. These controlsenable you to choose between searching the entire edit window andsearching from the cursor position.

    nn The OK, Cancel, and Help buttons.

    Figure 1.7. The Find Text dialog box.

    The Replace OptionThe Replace option supports replacing text in the active edit window. This option,which has the shortcut keys Ctrl+Q A, brings up the Replace Text dialog box, shownin Figure 1.8. This dialog box has the following controls:

    nn The Text to find combo box control, which enables you either to type inthe search text or to recall recently searched text.

    nn The New text combo box, which enables you either to enter the replacementtext or to select recently used replacement text.

    nn The Options check boxes, which include

    nn The Case sensitive check box, which enables you to select case-sensitive or case-insensitive text search.

  • 17

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    nn The Whole words only check box, which enables you to choosebetween matching entire words or matching any text.

    nn The Regular expression check box, which turns on or off the use ofthe BRIEF editors regular expressions. Such expressions result in usingthe text in the Text to find control as a text pattern.

    nn The Prompt on replace check box, which enables you to selectwhether or not text shall be replaced without your confirmation.

    nn The Direction diamond-shaped radio button controls. These controlspermit you to choose between forward and backward search.

    nn The Scope diamond-shaped radio button controls. These controlsenable you to choose between searching the entire text and limiting thesearch to the selected text.

    nn The Origin diamond-shaped radio button controls. These controlsenable you to choose between searching the entire edit window andsearching from the cursor position.

    nn The Change All pushbutton, which enables you to replace all of thematching text. By contrast, if you click the OK button you will onlyreplace the next matching text.

    nn The OK, Cancel, and Help buttons.

    Figure 1.8. The Replace Text dialog box.

  • 18

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    The Search Again OptionThe Search Again option enables you to repeat the last Find or Replace option.The shortcut key for this option is the F3 function key.

    The Browse Symbol OptionThe Browse Symbol option enables you to browse the makeup of a symbol,including classes, functions, and variables. These symbols need not be defined in theactive edit window, as long as they are defined in one of the current projects source-code files (your files or the librarys included files). Figure 1.9 shows a sample symbol-browsing dialog box.

    Figure 1.9. A sample symbol-browsing dialog box.

    The Locate Function OptionThe Locate Function option enables you to find the definition of a function. Thisoption brings up the Locate Function dialog box, shown in Figure 1.10, whichprompts you to enter the name of the function you wish to find. The IDE respondsby moving to it in an existing edit window or by displaying the function definitionin a new edit window, if need be.

  • 19

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Figure 1.10. The Locate Function dialog box.

    The Previous Message OptionThe Previous Message option enables you to zoom in on the offending source-codeline that is associated with the previous message in the Message window. The IDEresponds to this option by displaying the edit window that contains the offendingsource-code line. The shortcut keys for this option are Alt+F7.

    The Next Message OptionThe Next Message option enables you to zoom in on the offending source-code linethat is associated with the next message in the Message window. The IDE respondsto this option by displaying the edit window which contains the offending source-code line. The shortcut keys for this option are Alt+F8.

    The View MenuThe View menu contains options that enable you to view and browse through a widevariety of information. This information goes beyond the declarations in the source-code files of your own project. Table 1.4 summarizes the options in the View menu.

  • 20

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Table 1.4. Summary of the options in the View menu.

    Command Shortcut Keys Function

    ClassExpert Invokes the ClassExpert utility, whichworks with project files generated byAppExpert.

    Project Displays the Project window.

    Message Displays the Message window.

    Classes Browses through the classes.

    Globals Browses through global data types,constants, and variables.

    Watch Selects or opens the Watch window.

    Breakpoint Selects or opens the Breakpoints window.

    Call stack Selects or opens the Call Stack window.

    Register Selects or opens the Registers window.

    Event log Selects or opens the Event Log window.

    Information Displays system or status information.

    The ClassExpert OptionThe ClassExpert option invokes the ClassExpert utility, which works only withproject files created by the AppExpert (which we introduce in the next section). Thisoption invokes the ClassExpert window, which has three panes, as follows:

    nn The Classes pane, which lists the classes involved in the project created usingAppExpert. The information in the other two panes is related to the cur-rently selected class in this pane.

    nn The Events pane, which lists the command notification, control notifica-tions, virtual functions, Windows messages, and other events that are relatedto the class selected in the Classes pane.

    nn The source-code window, in which the selected class is defined.

    Figure 1.11 shows a sample ClassExpert window.

  • 21

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Figure 1.11. A sample ClassExpert window.

    The Project OptionThe Project option selects or opens the Project window, which lists the targets in thenodes in the current .IDE file. The Project window displays the files of a target in theform of a tree-like outline. The outline is made up of nodes that you can expand andcollapse (if they have child nodes). Figure 1.12 shows a sample Project window. Eachnode has a bitmap to its left. If the bitmap graphic has a + sign, then the node has childnodes that are currently hidden. If you click the + sign, you expand that node, and theIDE replaces the + sign with a sign. The child nodes without + or signs have nochild nodes of their own.

    If you click the right mouse button on a node in the Project window, the IDE displaysa floating menu that enables you to view various components of the project, managenodes, and edit project-related components.

    The Message OptionThe Message option displays, selects, or opens the Message window, which containsthe source-code compiler, resource compiler, and linker messages. These messagesinform you of the progress of building the .EXE program file. In addition, the Messagewindow contains any warning or error messages generated by the compilers or by thelinker.

  • 22

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Figure 1.12. A sample Project window.

    The Classes OptionThe Classes option displays the Browsing Objects window, showing a graph of thevarious classes in the current project and how they are interlinked. Typically, theBrowsing Objects window has a vertical and horizontal scroll bar to enable you toscroll through the various classes involved in the current project. Figure 1.13 showsa sample Browsing Objects window that displays the custom application classTWinApp and the applications frame window class, TMainWindow.

    The Globals OptionThe Globals option displays the Browsing Globals window, which shows the globaldata types, constants, variables, and functions. Figure 1.14 shows a sample BrowsingGlobals window. The window identifies each item by using the following specialbitmaps:

    nn The bitmap T indicates that the symbol is a data type.

    nn The bitmap C signals that the symbol is a constant.

    nn The bitmap F signifies that the symbol is a function.

    nn The bitmap V indicates that the symbol is a variable.

  • 23

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Figure 1.13. A sample Browsing Objects window.

    The Browsing Globals window contains switches that enable you to filter the viewingof certain global symbols. The window also contains an edit box control that enablesyou to type in the name of the symbol you want to find. The edit box control filtersthe symbols with every keystroke you enter.

    Figure 1.14. A sample Browsing Globals window.

  • 24

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    The Watch OptionThe Watch option selects or opens the Watch window. This window lists thecurrently watched variables in your program. Figure 1.15 shows a sample Watchwindow. The window displays a check box to the left of each variable. The check boxis checked by default to display and update the value in the associated variable. Youcan uncheck the control to temporarily disable displaying the value of a variable. Thistask is especially meaningful when the watched variable is not defined in the currentlytraced function.

    Figure 1.15. A sample Watch window.

    The Breakpoint OptionThe Breakpoint option displays the Breakpoints window, which lists the location andtype of breakpoints. A breakpoint is a program statement at which the program stopsto enable you to inspect its variables. Figure 1.16 shows a sample Breakpoints window.The Breakpoints window displays the following information:

    nn The filename that contains the breakpoint.

    nn The line number where the breakpoint is located.

    nn The state of the breakpoint.

  • 25

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    nn The number of passes (that is, the number of times the statement is executedbefore the program stops at the breakpoint).

    If you double-click any entry in the Breakpoints window, the IDE displays theBreakpoints Properties dialog box. This dialog box enables you to edit the breakpointsdata. Well cover this dialog box when we discuss managing breakpoints in the Debugmenu section.

    Figure 1.16. The Breakpoints window.

    The Call Stack OptionThe Call Stack option displays the Call Stack window, which lists the pendingprogram and the DLL functions that were called (and not yet returned) when theprogram reached the current breakpoint or the current single-stepped line. Figure1.17 shows a sample Call Stack window. The DLL functions are referenced by thename of the DLL library, followed by the address of the function.

    The Register OptionThe Register option displays the Registers window, which reveals the current valuesin CPU registers. The information in this window helps you perform a low-leveldebug and trace of a program.

  • 26

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Figure 1.17. A sample Call Stack window.

    The Event Log OptionThe Event Log option displays the Event Log window, which lists the sequence ofbreakpoint events. Each log entry includes the breakpoint address, followed by textthat identifies the related Windows messages, output messages, or exceptions. Figure1.18 shows a sample Event Log window.

    The Information OptionThe Information option displays the Information dialog box. This dialog boxcontains the following information:

    nn The current directory

    nn The Windows version and mode

    nn The MS-DOS version

    nn The total free memory space

    nn The largest free memory block

    nn The percent of USER, GDI, and total free heap space

    Figure 1.19 shows a sample Information dialog box.

  • 27

    Sam

    sLe

    arni

    ngCe

    nter PUBLISHING

    S MS

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    Figure 1.18. A sample Event Log window.

    Figure 1.19. A sample Information dialog box.

  • 28

    Getting StartedM

    TW

    RF

    SS

    DAYDAY

    1

    two/bns8 TYS Borland C++ 21 #30483 kim 4-13-94 CH01 LP#3(sp 4/12 folio)

    The Project MenuThe Project menu offers options that manage a project to build an executable programor a library. Table 1.5 summarizes the options in the Project menu.

    Table 1.5. Summary of the opti