Visual C++5: The Complete Reference
Chris H. Pappas and William H. Murray, III
Osborne McGraw-Hill Berkeley New York St. Louis San Francisco Auckland Bogota Hamburg London Madrid Mexico City Milan Montreal New Delhi Panama City Paris Säo Paulo Singapore Sydney Tokyo Toronto
Contents Introduction
xxv
A Quick Overview of Visual Ct i
1 The Visual C++ Compiler, Version 5
3
Recommended Hardware Minimum Hardware and Software Requirements Recommended Hardware and Software A Typical Windows Installation Directories Documentation The Development System The New Integrated Debugger The New Integrated Resource Editors Additional Tools Outside the Integrated Environment
6 6 6 7 8 8 9 10 10 n 11
What's New Automation and Macros ClassView Customizable Toolbars and Menus Internet Connectivity Project Workspaces and Files Wizards Important Compiler Features P-Code Precompiled Headers and Types The Microsoft Foundation Class (MFC) Library Function Inlining Compiler Options General Debug Custom Build C/C++ Link Resources OLE Types Browse Info
12 12 12 13 13 13 14 14 14 15 15 17 17 18 18 18 18 19 20 20 20
HIM 2 A Quick Start Using the IDE
21
Starting the Visual C++IDE Accessing Context-Sensitive Help Understanding Menus Docking or Floating a Toolbar The File Menu New. Open Close Save Save As SaveAll Rename Page Setup Print Recent Files List Recent Workspaces List Exit The Edit Menu Undo
22 23 23 24 24 25 25 26 26 26 27 27 27 27 28 28 28 29 29
Redo Cut Copy Paste Delete Select All Find Find in Files Replace Go To Bookmarks ActiveX Control in HTML... and HTML Layout Advanced Breakpoints View Menu Script Wizard ClassWizard Resource Symbols... and Resource Includes Full Screen Workspace InfoViewer Topic Results List Output Debug Windows Refresh Properties Insert Menu New Class Resource Resource Copy. Into HTML File As Text NewATLObject Project Menu Set Active Project Add To Project Dependencies Settings Export Makefile Insert Project into Workspace Build Menu Compile Build
29 30 30 30 30 31 31 31 31 33 33
. .
33 33 33 33 34 34 35 35 35 36 36 36 36 36 36 36 36 37 37 37 37 38 38 39 39 39 39 39 39 40 40 40
Rebuild Batch Build Clean Update All Dependencies Start Debug Debugger Remote Connection Execute Set Active Configuration Configurations Profile Tools Menu Source Browser. Close Source Browser File Spy++ MFC Tracer Register Control ActiveX Control Test Container Error Lookup OLE/COM Object Viewer Customize Options Macro Window Menu New Window Split Docking View Close CloseAll Next Previous
Cascade Tile Horizontally Tile Vertically History List Help Menu Contents and Search Documentation Home Page InfoViewer Bookmarks Synchronize Contents Define Subsets Select Subsets Keyboard Map Tip of the Day... and Technical Support
41 41 41 42 42 42 42 42 42 42 43 43 43 44 44 44 44 44 44 45 45 45 45 45 45 45 46 46 46 46
46 47 47 47 47 48 48 48 48 48 48 48 48
Microsoft on the Web About Developer Studio
3 Writing, Compiling, and Debugging Simple Programs Starting the Developer Studio Creating Your First Program Editing Source Code Saving Files Creating the Executable File Using Workspaces Choosing Build or Rebuild All Debugging Programs Differences Between Warning and Error Messages Your First Unexpected Bug Viewing Output and Source Windows Using Find and Replace Shortcuts to Switching Views Useful Warning and Error Messages More Work with the Debugger Running Your First Program Using the Integrated Debugger Advanced Debugging Techniques Using Breakpoints An Introduction to QuickWatch What's Coming?
4 Advanced Visual C++ Features Creating System Resources Designing Bitmaps Designing Dialog Boxes Setting Resource HotSpots Online Documentation Opening Reference Materials Searching for Specific Topics Getting a Hard Copy Diagnostic Tools Spy++ Process Viewer WinDiff What's Coming?
48 49
51 52 53 55 55 56 57 59 61 62 62 62 63 65 66 67 69 69 74 75 76 76
77 78 78 81 81 82 83 85 85 86 86 87 88 88
Programming Foundations
5 C and C++ Programming C Archives C Versus Older High-Level Languages Advantages of C Disadvantages of C "C Is Not for Children!" American National Standards Institute—ANSI C From C to C++ and Object-Oriented Programming C++Archives Object Code Efficiency Subtle Differences Between C and C++ Major Differences Between C and C++ Fundamental Components for a C/C++ Program Five Elements of Good C Program Design .... A Simple C Program A Simple C++Program Adding a User Interface to a C Program Adding a User Interface to a C++ Program . . . . Adding File I / O
6 Working with Data Identifiers Keywords Standard C and C++Data Types Characters Three Integers Unsigned Modifier Floatingpoint Enumerated And, the New C++Type—bool Access Modifiers const Modifier #define Constants volatile Modifier const and volatile Used Together pascal, cdecl, near, far, and huge Modifiers
93 94 95 97 99 100 101 101 104 104 105 108 110 110 Ill 113 113 118 120
125 126 128 129 130 132 132 134 136 138 139 139 140 141 141 142
pascal cdecl near, far, and huge Data Type Conversions Explicit Type Conversions Using the Cast Operator Storage Classes Variable Declarations at the External Level . . . . Variable Declarations at the Internal Level . . . . Variable Scope Review Function Declarations at the External Level . . . Operators Bitwise Operators Left Shift and Right Shift Increment and Decrement Arithmetic Operators Assignment Operator Compound Assignment Operators Relational and Logical Operators Conditional Operator Comma Operator Understanding Operator Precedence Levels Standard C and C++ Libraries
Hill 7 Program Control Conditional Controls if if-else Nested if-elses if-else-if The ?: Conditional Operator switch-case Combining if-else-if and switch Loop Controls for while do-while break continue Combining break and continue exit() atexit()
142 144 144 145 147 147 148 149 151 151 152 152 153 154 156 156 157 159 162 163 163 164
173 174 174 176 179 180 182 183 190 192 193 198 201 203 204 205 208 211
8 Writing and Using Functions What is Function Prototyping? The Syntax for Prototypes Ways to Pass Actual Arguments Storage Classes Identifier Visibility Rules Recursion Function Arguments Actual Versus Formal Parameters void Parameters char Parameters int Parameters float Parameters double Parameters Array Parameters Function Return Types void Return Type char Return Type bool Return Type int Return Type long Return Type float Return Type double Return Type Command-Line Arguments Alphanumeric Integral Real Functions in C Versus C++ When Is a Function a Macro? Prototyping Multiple Functions with the Same Name Functions with Varying-Length Formal Argument Lists Things Not to Do with Functions Attempting to Access Out of Scope Identifiers External Versus Internal Identifier Access Internal Versus External Identifier Access It's Legal, But Don't Ever Do It! Overriding Internal Precedence
9 Arrays What Are Arrays?
215 216 216 219 221 221 222 223 223 224 225 226 227 228 229 231 231 233 234 235 235 239 240 241 242 243 245 246 246 247 249 251 251 252 253 254 256
259 260
Array Properties Array Declarations Initializing Arrays Default Initialization Explicit Initialization Unsized Initialization Accessing Array Elements Calculating Array Dimensions Array Index Out of Bounds Output and Input of Strings Multidimensional Arrays Arrays as Function Arguments Passing Arrays to C Functions Passing Arrays to C++ Functions String Functions and Character Arrays gets(), puts(), fgets(), fputs(), and sprintf() . . . strcpy(), strcat(), strncmp(), and strlen() ....
lllfl 10 Using Pointers Pointer Variables Declaring Pointers Using Pointer Variables Initializing Pointers What Not to Do with the Address Operator Pointers to Arrays Pointers to Pointers Pointers to Strings Pointer Arithmetic Pointer Arithmetic and Arrays Problems with the Operators ++ and - Using const with Pointers Comparing Pointers Pointer Portability Using sizeof with Pointers under 16-Bit DOS Environments Pointers to Functions Dynamic Memory Using void Pointers Pointers and Arrays—a Closer Look Strings (Arrays of Type char) Arrays of Pointers More on Pointers to Pointers Arrays of String Pointers
260 261 262 262 263 264 265 268 270 271 274 278 278 280 287 288 290
295
...
296 297 299 304 305 306 307 310 312 315 317 317 319 320 320 323 327 330 333 333 334 338 346
The C++ Reference Type Functions Returning Addresses Using the Integrated Debugger When Should You Use Reference Types?
Hill 11 Complete I/O inC Stream Functions Opening Streams Input and Output Redirection Altering the Stream Buffer Closing Streams Low-Level Input and Output in C Character Input and Output Using getc(), putc(), fgetc(), and fputc() Using getchar(), putchar(), fgetchar(), and fputcharO Using getch() and putch() String Input and Output Using gets(), puts(), fgets(), and fputs() Integer Input and Output Using getw() and putw() Formatting Output Using printf() and fprintf() Using fseek(), ftell(), and rewind() Using the Integrated Debugger Formatting Input Using scanf(), fscanf(), and sscanf()
348 349 350 350
353 355 355 356 357 360 360 361 361 362 363 363 363 365 365 368 370 375 378 380 380
Hill 12 An Introduction to I/O in C++
383
Streamlining I/O with C++ ein, cout, and cerr The » Extraction and « Insertion Operators From STREAM.H to IOSTREAM.H Operators and Member Functions
384 385
111! I 13 Structures, Unions, and Miscellaneous Items Structures Structures: Syntax and Rules C++ Structures: Additional Syntax and Rule Extensions
385 394 395
411 412 412 415
Accessing Structure Members Constructing a Simple Structure Passing Structures to Functions Constructing an Array of Structures Using Pointers to Structures Passing ah Array of Structures to a Function . . . Structure Use in C++ Additional Manipulations with Structures . . . . Unions Unions: Syntax and Rules Constructing a Simple Union Miscellaneous Items Using typedef Using enum
Hill 14 Advanced Programming Topics Type Compatibility ANSI C Definition for Type Compatibility What Is an Identical Type? Enumerated Types Array Types Function Types Structure and Union Types Pointer Types Multiple Source File Compatibility Macros Defining Macros Macros and Parameters Problems with Macro Expansions Creating and Using Your Own Macros Macros Shipped with the Compiler Advanced Preprocessor Statements #ifdef and #endif Directives #undef Directive #ifndef Directive #if Directive #else Directive #elif Directive #line Directive #error Directive #pragma Directive Conditional Compilation
415 416 418 420 424 426 430 433 435 435 436 438 438 440
443 ....
444 444 444 446 446 446 447 447 447 447 448 449 450 452 453 453 454 455 455 456 456 457 457 458 458 459
Preprocessor Operators # Stringize Operator ## Concatenation Operator #@ Charizing Operator Proper Use of Header Files Making Header Files More Efficient Precompiled Header Files Creating Precompiled Headers Using Precompiled Headers LIMITS.H and FLOAT.H Handling Errors—perror() Dynamic Memory Allocation— Linked Lists Considerations When Using Linked Lists A Simple Linked List
111 15 Power Programming: Tapping Important C and C++ Libraries Important C and C++ Header Files Standard Library Functions (STDLIB.H) Performing Data Conversions Performing Searches and Sorts Miscellaneous Operations The Character Functions (CTYPE.H) Checking for Alphanumeric, Alpha, and ASCII Values Checking for Control, White Space, and Punctuation Conversions to ASCII, Lowercase, and Uppercase The String Functions (STRING.H) Working with Memory Functions Working with String Functions The Math Functions (MATH.H) Building a Table of Trigonometric Values The Time Functions (TIME.H) Time and Date Structures and Syntax What's Coming
460 460 461 462 462 463 464 464 465 465 466 469 470 471
475 476 477 477 481 484 486 487 489 492 494 495 498 504 504 507 508 515
Foundation» for Object-Orient«.«! Programming in C++ 11111 16 A n Introduction to Object-Oriented Programming There Is Nothing New Under the Sun Traditional Structured Programming Object-Oriented Programming C++ and Object-Oriented Programming Object-Oriented Terminology Encapsulation Class Hierarchy A First Look at the C++Class A Structure as a Primitive Class The Syntax and Rules for C++ Classes A Simple C++Class
519 521 522 523 524 524 526 526 528 528 534 535
IIII117 C++Classes
539
Special Class Features ASimple Class Nesting Classes Working with Constructors and Destructors . . . Overloading Class Member Functions Friend Functions The this Pointer Operator Overloading Overloading Operators and Function Calls . . . . Overloading Syntax Derived Classes Derived Class Syntax Working with Derived Classes
540 540 541 545 549 554 558 559 559 560 563 563 564
lllll 18 Complete I/O in C++
571
Using enum Types in C++ Reference Variables Default Arguments The memset() Function
572 573 575 577
Formatting Output I/O Options The iostream Class List Input Stream Classes Output Stream Classes Buffered Stream Classes String Stream Class Binary Files Combining C and C++ Code Designing Unique Manipulators Manipulators Without Parameters Manipulators with One Parameter Manipulators with Multiple Parameters
11III19 Working in an Object-Oriented Environment An Object-Oriented Stack An Object-Oriented Linked List in C++ Creating a Parent Class A Derived Child Class Using a Friend Class Examining the Complete Program Output from the Linked List More Object-Oriented Programming
578 583 583 588 590 592 594 597 600 603 603 604 606
609 610 613 614 615 617 621 630 633
Windows Programming Foundations
i 1111 20 Concepts and Tools for Windows 95 and NT Windows Concepts The Windows Environment The Advantages of Windows The Windows Executable Format Windows Programming Concepts and Vocabulary The Windows Window The Windows Layout The Windows Class OOPs and Windows Windows Messages
637 638 638 639 646 647 647 647 650 651 655
Accessing Windows Functions The Windows Header File: WINDOWS.H .... The Components of a Windows Application . . . Visual C++ Windows Tools Project Files Resources Resource Editors Additional Resource Information
11111 21 Procedure-Oriented Windows Applications A Framework for All Applications Components in a Windows Application Make or Project Utility? The NMAKE Utility Project Utility A Simple Windows Program and Template Drawing an Ellipse Drawing a Chord Drawing a Pie Wedge Drawing a Rectangle Using the SWP.C as a Template Creating a Windows Pie Chart Application The Project File The PIE.H Header File The PIE.RC Resource File The PIE.C Source Code More on Traditional C Windows Programming
681
. .
1! 111 22 Microsoft Foundation Class Library: Concepts The Need for a Foundation Class Library MFC Design Considerations Key MFC Library Features It All Begins with CObject Important Foundation Library Classes A Simplified Application Establishing a Window with SIMPLE.CPP Running the SIMPLE.CPP Application A Simplified Design Ensures Easy Maintenance
659 660 660 661 662 662 662 680
682 684 697 697 698 703 706 708 709 711 712 716 725 726 726 727 732
733
....
734 735 736 737 740 744 744 752 753
11111 23 Windows Applications Using the MFC A Simple Application and Template The MFCSWP.H Header File The MFCSWP.CPP Source Code File Running MFCSWP Drawing in the Client Area The GDI.H Header File The GDI.CPP Source Code File Running the GDI Application A Fourier Series Application with Resources The FOURIER.H Header File The Resource Files The FOURIER.CPP Source Code File Running FOURIER A Bar Chart with Resources The BARCHARTH Header File The Resource Files The BARCHART.CPP Source Code File Running BARCHART What's Next?
. . 755
....
756 758 759 759 760 766 766 767 768 776 778 778 785 787 797 798 799 806 808
Wizards
11 III 24 Application and Class Wizards The Graph Application TheAppWizard The ClassWizard Building the Application Drawing in the Client Area The Word Processor Application Building the Application What's Coming?
Mill 25 An Introduction to OLE OLE Features and Specifications Objects Files Data Embedding Linking Building a Container Application Using the AppWizard
811 813 813 818 820 832 836 843 858
859 860 860 861 861 861 867 867 867
The AppWizard Files Testing the Container Application What's Coming?
26 ActiveX Controls with the MFC Library OLE ActiveX Controls ActiveX Control Design Criterion The COleControl Class Control Containers Creating a Control with the MFC ActiveX ControlWizard A Basic ActiveX Control A Look at Important Code Customizing the Initial ActiveX Control Changing the Shape, Size, and Colors of the TDCtrl Mouse Events Testing the TDCtrl ActiveX Control More ActiveX Controls?
868 889 891
893 894 895 895 900 901 901 905 915 915 918 923 925
Appendixes
A Extended ASCII Table
929
B DOS 10H, 21H, and 33H Interrupt Parameters
935
Screen Control with BIOS-Type 10H Interrupts Interface Control of the CRT Handling Characters Graphics Interface ASCII Teletype Output Specifications and Requirements for the DOS 21H Interrupt Mouse Control Functions Accessed Through Interrupt 33H
936 936 938 938 939 940 947
C Dynamic Link Libraries An MFC-Based Dynamic Link Library The Framer.H Header File
954 956
The Framer.CPP Source Code File BuildingtheFramer.DLL An Application That Calls a DLL The DLLDEMOVIEW.H Header File The DLLDEMOVIEW.CPP Source Code File More DLLs?
Index
...
957 959 960 961 962 965
967