Using Borland9 C+ + 3
Transcript of Using Borland9 C+ + 3
Using Borland9 C+ + 3
2nd Edition
Lee Atkinson and Mark Atkinson
PROGRAMMINGS E R I E S
oueCORPORATION
LEADING COMPUTER KNOWLEDGE
TABLE OF CONTENTSv
Introduction 1Who Should Read This Book? 2What This Book Is About 2How This Book Is Organized 2How To Use This Book 3Conventions Used in This Book 4ANote on Practicing C and C++ 7Selecting a Borland Compiler 7
I Using Borland C ++'s Basic Features
1 Getting Started with Borland C+ + 13Running the Installation Utility 13
Installing Borland C++ 13Using the Integrated Development Environment (IDE) 16
Starting the IDE 17Using the IDE's Menus and Windows 19
The Menu System 21The Window System 22
Configuring Borland C++ 26Setting the IDE Options 26
Writing Your First C Program 29Understanding C Program Structure 29
Preprocessor Directives 31Global Declarations 33The main( ) Function 35User-Defined Functions 35
Using the Editor To Write a Program 36Introducing the Library Functions 41
Using Some Basic Input Functions 41The get... () Functions 42The scanf( ) Function 43
Using Some Basic Output Functions 45The put... ( ) Functions 45The printf( ) Function 46
Using Some Format-Conversion Functions 47The atoi( ) Function 48The toupper( ) Function 49
Exercises 50Summary 51
IX
Understanding the Foundations of C 53Laying the Foundations 53
Understanding Source, Object, and Load Modules 53Understanding Program Logic and Execution Flow 56
Using Conditional Instructions '. 56Using Loop Instructions 60
Using the Basic Data Types 63Understanding C's Basic Data Types 63
Integers 64Floating-Point Numbers 65Character Data 66
Knowing Where To Define Data Objects 67Writing C Expressions and Statements 71
Understanding Expressions and Statements 71Introducing the C Operator Set ; 73
Controlling Type Conversions 77Understanding Implicit Type Conversions 77Using Explicit Type Conversions 79
Using C Macros ; 80Defining Object-like Macros 80Defining Function-like Macros 85
Exercises 89Summary 90
JUsing C Functions 93
Understanding the main( ) Function and Library Functions 93Writing the main( ) Function 94Using Library Functions 97
Knowing What Library Functions Are Available 97Including Library Functions in Your Program 99
Writing Your Own Functions 100Writing Prototypes for Your Functions 101Passing Arguments to Your Functions :.. 104
Returning Values from Functions 108Defining and Using Function Types 108Using Functions Like Objects I l l
Understanding Storage Classes 112Determining a Variable's Scope 112Determining a Variable's Duration : 114Determining a Variable's Linkage 115
Using Advanced Program Control Logic 116Writing Loop-Control Statements 117
Usinggoto 118Using break 118Using continue 119
Changing the Flow of Program Execution 119Using exit( ) and abort( ) 120Using the system( ) , exec...( ) , and spawn...( ) Functions 122
Using Variable Argument Lists 123Designing Variable Argument Lists 124Using the va_...( ) Functions 125
Exercises 130Summary : 130
4 Using Pointers and Derived Types ; 133Understanding Standard C Derived Types 133
Understanding C's Typing Scheme 134Creating New Types from Old 134
Understanding C Pointers ." 138Understanding Indirect Addressing 139Using C's Indirection and Address-of Operators 145
Using Arrays and Strings 148Declaring and Using Arrays of Objects 149Understanding C Strings 152
Using Structures and Unions '.. 157 ^Building Structures from Different Types 157Unions Are Alternate Views of Structures and Objects 161
Using Pointers to Functions 164Declaring and Initializing Pointers to Functions 164Calling Functions/with a Pointer Reference 165
Using Pointers with Dynamic Memory 169Your C Program and Dynamic Memory 170Using Dynamic Memory 172
Exercises 179Summary : 180
5 Building, Compiling, and Testing Borland C+ +Programs : .....183
Using Several Source Files for One Program 184Deciding What To Put in a Source File '. 184Setting Up a Borland C++ Project File 200
Understanding External References 203Using the extern Keyword 203Using External Functions 205
Writing Header Files for External Modules 206Deciding What Goes in the Header File 207Including User-Supplied Header Files 208
Using Conditional Compiler Directives in Header Files 209Compiling and Running Programs with the IDE 212
Compiling and Running Simple Programs 212Compiling and Running Complex Programs 214
XI
Introducing the Integrated Debugger 216Using the Step over Command To Narrow the Search 216Using the Trace into Command To Pinpoint the Problem 218Setting and Removing Breakpoints 219
Exercises 222Summary 222
Using the Borland C+ + I/O Function library 225Understanding I/O Concepts 225
Understanding Files and Devices .....226Understanding Files and Streams 228Understanding Text and Binary Streams 229
Using the Standard Streams for I/O 231Using Formatted I/O Functions 231
Understanding scanf( ) 235Understanding/>rm#f ) 240
Using Character I/O Functions 243Using the File Control Functions '. 249
Opening, Closing, and Controlling Files 249Controlling File Buffers 254
Using the Direct File I/O Functions 256Understanding Direct I/O Concepts 257Reading and Writing Direct Files 259
Using the File Positioning Functions 265Obtaining the Current File-Position 265Setting a New File Position 267
Handling File I/O Errors '. 269Detecting File I/O Errors 269Displaying and Clearing File I/O Errors : 270
Exercises 271Summary 272
Using Memory Models and VROOMMTechnology 275
Introducing 80x86 Architecture 276Understanding Segments, Paragraphs, and Offsets 276Understanding CPU Addressing Registers 279
Understanding near, far, and huge Pointers : 281Choosing the Pointer Size You Want 281Using the near, far, andhuge Specifiers 284
Using the Six Borland C++ Memory Models 286Deciding Which Memory Model To Use 287Programming with Mixed Models 288
Creating COM Executable Program Files 291Using COM Files 291
Understanding the Overlay Manager 294Knowing What an Overlay Manager Does 294Understanding Borland's VROOMM 295
Xll
Designing and Creating Overlay Programs 297Deciding What Modules To Overlay 298Compiling and Linking an Overlay Program '. 299
Exercises 302Summary : 302
8 Using the Borland C++ Video Functions 305Understanding the IBM/PC Text Modes 305
Surveying PC Video Adapters and Screens 306Understanding Memory-Mapped Screen I/O 307
Controlling the Text Screen 309Using the Text-Mode Control Functions 309Using Direct Console I/O for High Performance 314
Using the Window Functions 315Understanding the IBM/PC Graphics Mode 319
Understanding Pixels and Palettes 319Controlling the Graphics Screen 324
Introducing the BGI Graphics Library 326Using the Drawing and Filling Functions 327Controlling the Screen and Viewport 332
Using Text in Graphics Mode 334Understanding BGI Fonts 334
. Using the Graphics-Mode Text Functions 335Exercises ,. '. 338Summary f!\. 338
9 Using Turbo Debugger and Turbo Profiler 341Setting Up for Debugging 342
Understanding Bugs and Debuggers 342Preparing To Run Turbo Debugger from the IDE 344
Using Turbo Debugger ..- 345Running a Program with Turbo Debugger 345Viewing Data..... 350
Using Advanced Debugging Features. 353Changing Program Values 353Setting Breakpoints 355
Using Turbo Profiler ...: 357Knowing What Profiling Is 357Starting Turbo Profiler 359Knowing Basic Profiling Information 360
Using Advanced Profiling Features 366Selecting Areas To Profile ; 366Displaying Profiler Statistics 369Choosing Active or Passive Profiling 372
Exercises 373Summary 374
xm
10 Using Borland C++'s Extended Features 377Using Inline Assembly Language 378
Understanding the Inline Assembly Environment 379Using the asm Keyword 382
Interfacing Assembly Language Routines 386Calling Assembly Routines from C 387Calling C Functions from Assembly Routines 400Using Interrupt Functions 409Understanding 80x86 Interrupt Architecture 409Using Borland's Interrupt Interfaces 411
Using Interrupt Handlers 415Declaring Interrupt Handler Functions 415Implementing a Timer Tick Interrupt Handler 418
Exercises .-. 422Summary 424
\ /
II Using Borland C++'s v
Object-Oriented Features
11 Using C++ Classes 427Comparing Derived Types in C and C++ 428
Redefining "Derived" in C+;+ 428Understanding C++ Encapsulation 430Declaring Classes with struct 432
Declaring C++ Classes 437Understanding the class Declaration 440
Using Class Declaration Syntax 441Declaring Class Members 446Building a LIFO Stack Class 446Using the this Pointer 455
Using the public, private, and protected Keywords ....462Writing Member Functions for a Class 463
Associating Member Functions with a Class 464Compiling Member Functions Separately 464Declaring Inline Member Functions 465Specifying Default Arguments for Member Functions 467
Providing Constructor and Destructor Functions 469Using Friend Functions 473
Including Friend Functions in a Class 474Deciding When To Use a Friend Function 475
Exercises 476Summary 477
XIV
12 Creating C++ Objects 481Defining C++ Objects 481
Assigning Storage Classes to Class Objects 482Defining Arbitrary-Duration Class Objects 484Defining Local (auto) Class Objects 501Defining Global (static) Class Objects 511
Initializing Class Objects 521Using Constructors To Initialize Class Objects 521Using Initializer Lists '. 524
Exercises 525Summary 526
13 Accessing C++ Objects 527Using the Scope Resolution Operator 527
Using Scope Resolution in General 528Using Scope Resolution for Syntax Control 530Controlling Ambiguities with Scope Resolution 532
Understanding the C++ Scope Rules 537Understanding the Differences between C and C++ Scope 537Examining the C++ Scope Rules 539
Communicating with C++ Objects 543Sending Messages to Objects 544Understanding *this ' ; 570
Using the Reference Operator ! 571Evolving the Reference Operator from the
Address-of Operator 571Understanding the Reference Operator 572
Using Objects as Function Parameters 573Passing Objects by Value and by Reference 573Accessing Other Objects from a Member Function 575
Using Pointers to Objects 577' Understanding When Pointers Are Required 577
Declaring Pointers and Arrays of Objects 579Exercises 581Summary ;: 581
14 Using Overloaded Functions and OperatorsI in C++ 583
Overloading Member Functions 583Understanding C++ Overloading 584
; Declaring Overloaded Member Functions :...588! Overloading Friend and Nonmember Functions 590' Overloading Friends of a Class 590
Overloading Nonmember Functions 596i Understanding Type-Safe Linkage...! 597! Understanding Function Mangling 598j Controlling Linkage Problems with Standard C Include Files 600
xv
Overloading Operators with C++ 604Understanding Operator Overloading 604 x
Declaring Overloaded Operator Functions 611Setting the Stage for Operator Functions .611Understanding Overloaded Operator Function Syntax 612Specifying Arguments for Overloaded Operator Functions 614Specifying a Return Type for an Operator Function 616Overloading Operators with Friend Functions 617Defining Cast Operator Functions 621
Overloading Binary and Unary Operators 622Overloading the Subscript and Function Call Operators 62 5
Using an Overloaded Subscript Operator 625Using an Overloaded Function Call Operator 631
Exercises 635Summary, 636
15 Using C++ Constructors and Destructors 639Understanding Constructor and Destructor Calls 640
Declaring Constructors and Destructors 641Declaring Constructor Functions 641Declaring Destructor Functions 647
Using Constructor Initializers 649When Are Constructor Functions Called? 654When Are Destructor Functions Called? 662
Overloading Constructor'Functions 667Writing a Default Constructor Function 668
Writing Other Constructors 671Deciding When You Need a Copy Constructor 673
Using operator new( ) and operator delete( ) 674Using new and delete in General 675Dynamically Creating and Deleting Class Objects 678
Overloading operator new( ) and operator delete( ) 681Overloading the Global Operators 682Overloading the Operators for a Class 690
Exercises '. 693Summary 693
16 Using C++ Streams 695Introducing C++ Streams 695
Comparing C++ Streams to Standard Streams 696Using C++ Streams for Standard I/O .TTT. 700
Handling C++ Stream Errors 711Detecting C++ Stream Error States 711Using the Stream State Member Functions 714
Controlling Data Formats with C++ Streams 716Using Inserters and Extractors for Built-in Types 717Overloading the << and >> Operators 723
LXVI
UsingC++ Stream Manipulators 727Understanding C++ Manipulators 728Using Manipulators To Change States and Attributes 729
Using C++ File I/O Streams 740Reading and Writing fstream Files 745File Positioning with C++ Streams 746
Using and Converting Old C++ Streams 748Differences between Old and New C++ Streams 749Converting to the New C++ Streams 751
Exercises 752Summary 753
17 Using C++ Derived Classes 757Reusing Code without Inheritance 757
Understanding Code Reusability 758Reusing Code by Composition 758
Using Single Base Classes 763Understanding Inheritance .• 763Declaring Base and Derived Classes 764
A Practice Project: Extended Memory Support 773Understanding the Extended Memory Specification (XMS) 773Building the XMS Manager Class 776Deriving a Swap Buffer Class 789
Using Virtual Functions,- 795Using Late Binding and Virtual Functions 795Using Scope Resolution To Control Member Function
Access , 799Using Multiple Base Classes 801
Deriving from More Than One Base Class 801Declaring and Using Virtual Base Classes ....805
Deriving Classes from Abstract Classes 807Understanding Pure Virtual Functions 808Implementing Pure Virtual Functions 808
Using Constructors and Destructors with Inheritance 810Initialization Code Is Not Inherited 810Understanding the Order of Constructor and Destructor
Calls with Inheritance 810Using Virtual Destructor Functions 811
Exercises 814Summary '....814
18 Object Control, Performance, and FutureDirections 817
User-Defined Type Conversions 818Using Constructors To Convert Types 818Overloading Type Cast Operators 820
xvu
Using Generic Classes 824Understanding Abstraction and Generic Class Design 825Building Generic Classes 829
Controlling Object Behavior and Performance 839Using Friend Functions To Boost Efficiency : 839Using Friend Functions To Control Object Syntax 840Using the static Storage Class To Avoid Repetitive
Instantiation 844Using References and Pointers : ; 847Using Inline Functions To Eliminate Function Calls 850
Directions in Class Object Control 851Class and Function Templates 851Exception Handling : 854
Exercises ._. 856Summary T .....856
19 Using Turbo Vision 859Understanding Turbo Vision 859
What Are Application Frameworks? 860Why Use Turbo Vision's Application Frameworks? 860Benefits of Using Object-Oriented Code 862Benefits of Event-Driven Programming 862
Understanding a Turbo Vision Application's Parts 863Events ..- ..864Views <?.. ..864Mute Objects 865
Building Your First Turbo Vision Application 866Making a Basic Turbo Vision Application 866Adding Menu Support 869
Creating Windows with Turbo Vision ..' '. 874Adding Windows to a Turbo Vision Application 875
, Adding Text to Turbo Vision Windows 880Using the Dialog Box Window 885
Exercises 893Summary 893
III Using Borland C++with Microsoft v
Windows
20 Introducing Borland's Advanced Compilers 897New Features and Utilities 898Running the Programmer's Platform 901
Running the Programmer's Platform Under MS-Windows 901Options for Starting Borland C++ 904
Turbo Debugger 2.5 and 3.0 905
XVlll
Using Turbo Assembler 2.5 and 3.0 907C Compiler Differences That Affect TASM 907NewMODEL Identifiers for Windows 3.0 Support 910Virtual Segment Support 912486 Instruction Support 912RETCODE Instruction 913TASMX.EXE and the Windows DPMI 914
Using the Whitewater Resource Toolkit 914Using the Resource Workshop 917Exercises 930Summary 930
21 Using Borland C++with Windows 933Understanding the Windows Programming Environment 934
Windows Is a Multitasking Environment 935Windows Is an Object-Oriented Environment 938
Setting Up Borland C++ for Windows Applications Building 947Setting Up Transfer Options for the Resource Compiler
and the Import Librarian 948' Running the IDE Alone 949
Running the IDE under Windows 949Setting Up Compiler Options for Entry and Exit Code 951
Using the Whitewater Resource Toolkit (RT) 954Setting Up RT as a Windows Program Item 955Resources Needed£by xhefcwin.c Sample Program 956Running RT '. 958
Navigating RT's Main Window 958Using the Resource Browser 959Creating and Editing Icons 960Creating and Editing Menus 960Creating and Editing Dialog Boxes 964
Building FCWIN Resources with Borland's Resource Workshop 970Exercises 979Summary 980
22 Designing Windows Applications 983Setting Up the Windows Application Environment 983
Building Your Own Windows Stub Executable File 984Understanding Windows Directory Usage 986Learn by Doing: Designing fcwin.c 988
Creating Source Files for Windows Applications 1019Understanding the Window? 3.0 Programming
Environment 1019Creating a Module Definition File .' : 1020Designing the Program's Header File 1022Creating a Project File for FCWIN 1024
Exercises 1025Summary 1026
XIX
23 Writing Windows Applications withBorland C++ 1027
Designing Windows Interfaces 1027Registering the Window Class 1028Setting Up the Main Message Loop 1031Writing the WndProc( ) Function 1033Setting Up Callback Functions for Dialogs 1035
Creating Dialog Functions 1035Controlling the Dialog 1037
Using MessageBox( ) for Pop-Up Help and Error Messages 1038Spooling Hard Copy to the Windows Print Manager 1040
Using Dynamic Link Libraries 1050Understanding DLLs 1050Writing a DLL Application 1051
Exercises 1056Summary 1056
24 Using ObjectWindows : 1059Introducing ObjectWindows for C++ 1060
Using C++ Classes in a Windows Program 1061Understanding the ObjectWindows Library 1075Writing Your First ObjectWindows Program 1078
Interfacing with Basic Windows API Functions and Elements 1082Controlling Applications ...<?: 1082Controlling Window Objects 1084Controlling Windows Messages 1087
Exercises 1089Summary • 1090
A ASCII Charts 1091
B Details on Usingprintf( ) andscanf( ) 1095
C Details on Using exec...( ) and spawn... ( ) 1103
D Program Listings for the quad Class 1107
E Complete Listings of FCWIN Resources 1121
Index '. 1135
X X