Getting Started With Microsoft Visual C++ 6: Objectives
Getting Started With Microsoft Visual C++ 6: Objectives
with Microsoft®
1 Visual C++ ™6
Objectives
• To be able to use Visual C++ to create, compile
and execute C++ console applications.
• To understand and be able to use the Microsoft’s
Visual C++ 6 integrated development
environment.
• To be able to search Microsoft’s on-line
documentation effectively.
• To be able to use the debugger to locate program
logic errors.
2 GETTING STARTED WITH MICROSOFT® VISUAL C++™ 6 CHAPTER 1
Outline
1.1 Introduction
1.2 Integrated Development Environment Overview: Visual C++
1.3 On-line Visual C++ Documentation
1.4 Creating and Executing a C++ Application
1.5 Debugger
1.1 Introduction
Welcome to the Visual C++ 6 integrated development environment (part of the Microsoft
Visual Studio® 6 suite of development tools). In this appendix you will learn how to create,
compile, execute and debug C++ programs using the powerful C++ development environ-
ment from Microsoft—Visual C++ 6. When you complete this appendix, you will be able
to use Visual C++ to begin building applications.
Before proceeding with this appendix, you should be familiar with the topics in
Chapter 1, “Introduction to Computers and C++ Programming,” and Chapter 2, “Control
Structures,” of C++ How to Program: Third Edition. A few of the examples in this chapter
make reference to functions. For these examples, you should be familiar with the material
through Section 3.5 of Chapter 3, “Functions,” in C++ How to Program: Third Edition.
Also, one figure in this chapter uses keyword struct which is first covered in Section 6.4
of Chapter 6, “Classes and Data Abstraction,” in C++ How to Program: Third Edition.
We hope you enjoy learning about the Visual C++ 6 integrated development environ-
ment.
The Visual C++ documentation is also accessible via the World Wide Web at the
Microsoft Developer Network Web site:
http://msdn.microsoft.com/library/default.asp
The on-line documentation is arranged hierarchically. You can find the Visual C++ docu-
mentation under:
Note: if you are not using the introductory edition or are on the Microsoft Developer Net-
work Web site the Visual C++ documentation can be found under
Information about all aspects of Visual C++ is available. Topics range from the Stan-
dard C++ Library to the Microsoft Foundation Classes (MFC). Topics are displayed in
tree-view format (see the leftmost portion of Fig.1.3). Clicking the left-mouse button on the
plus (+) sign next to a topic expands its subtopics. [Note: For the rest of this appendix, we
refer to “clicking the left mouse button” simply as clicking.]
©2000 Deitel & Associates, Inc. and
Prentice Hall. All rights reserved.
CHAPTER 1 GETTING STARTED WITH MICROSOFT® VISUAL C++™ 6 5
The Visual C++ 6.0 Introductory Edition toolbar (the row of icons near the top of
the window in Fig. 1.3) is used to navigate through the on-line documentation in a manner
similar to viewing pages in a Web browser. In fact, a modified version of Microsoft’s
Internet Explorer Web browser is used to view the documentation. Clicking the left and
right arrows on this toolbar move back and forward, respectively, through any previously
viewed pages. The Stop button causes the program to stop loading the current topic. The
Refresh button reloads the current topic from the document’s source. The toolbar also pro-
vides a Home button that displays the Visual C++ start page (Fig. 1.3).
In the left panel, the user can control the display in the right panel by making selections
from one of the tabs, Contents, Index, Search or Favorites. The Contents tab dis-
plays the tables of contents. The Index tab displays a list of key terms from which to select
a topic. The Search tab allows a programmer to search the entire on-line documentation
contents for a word or phrase. The Favorites tab lets the user save links to interesting
topics and later return to them.
On-line information is divided into categories. Each category is preceded by a book
icon. The Visual C++ Start Page is the starting point for navigating the on-line docu-
mentation. The Visual C++ Documentation Map outlines the various sections of the
Visual C++ documentation by category. The What’s New in Visual C++ 6.0 topic
explains the newest features introduced in Visual C++ 6.0.
Getting Started with Visual C++ 6.0 contains links to various topics in the docu-
mentation, including Beginning your Program, Porting and Upgrading, Visual
C++ Tutorials, What’s New for Visual C++ 6.0, Getting Help and Visual C++
Home Page. These topics cover a broad range of subjects such that a programmer new to
Visual C++, regardless of programming background, can find something of interest.
The Using Visual C++ category is composed of four subcategories—Visual C++
Tutorials, which contains tutorials on how to develop applications that use advanced lan-
guage features (e.g., OLE server, OLE containers, ActiveX controls, etc.); Visual C++
Programmers Guide, which contains information on various programming topics (e.g.,
portability issues, debugging, errors, etc.); Visual C++ User’s Guide, which contains
information about the Visual C++ IDE (e.g., projects, classes, editors, utilities, etc.); and a
Glossary, which contains acknowledgments and terminology.
contains the code to print Hello World) and a few support files. Selecting An applica-
tion that supports MFC creates several files which add support for Visual C++’s Win-
dows programming library called MFC. At this point, you should select An empty
project and click Finish to display the New Project Information dialog (Fig. 1.6). If
you selected the wrong project type (i.e., a type other than Win32 Console Application)
in the New dialog and do not see the choices in Fig. 1.5, click <Back to view the New
dialog.
The New Project Information dialog (Fig. 1.6) provides a summary of the project
about to be created. For our Win32 Console Application, the dialog specifies that the
project is empty (i.e., no additional files were created). This dialog also specifies in the
lower-left corner the location on disk for this project. Clicking OK closes the dialog and
creates the project. If you created the wrong project type, click Cancel to go back to the
Win32 Console Application - Step 1 of 1 dialog.
At the bottom of the workspace pane are two tabs, ClassView and FileView.
Clicking ClassView displays classes and class members (discussed in Chapter 6 of C++
How To Program: Third Edition), and functions (discussed in Chapter 3 of C++ How To
Program: Third Edition) in your project. FileView displays the names of the files that
make up the project. FileView initially displays the project name followed by the word
files (e.g., Welcome files). Clicking the plus sign, +, to the left of Welcome files dis-
plays three empty folders: Source Files, Header Files and Resource Files. Source
Files displays C++ source files (i.e., .cpp files), Header Files displays header files (i.e.,
.h files) and Resource Files displays resource files (i.e., .rc files that define window
layouts). For this example we only use the Source Files folder.
The next step is to add a C++ file to the project. Selecting New... from the File menu
displays the New dialog (Fig. 1.8). When a project is already open, the New dialog dis-
plays the Files tab containing a list of file types. The file types will vary based on the
Microsoft development tools installed on your system.
Select C++ Source File for a C++ file. The File name field is where you specify the
name of the C++ file. Enter welcome in the File name field. You do not have to enter
the file name suffix “.cpp” because it is implied when you select the file type. Do not
modify the Location text box. When checked, Add to Project adds the file to the project.
If Add to Project is not checked, check it. Click OK to close the dialog. The C++ file is
now saved to disk and added to the project. In our example, the file welcome.cpp is
saved to the location D:\Welcome (the combination of the Location field and the project
name). Figure 1.9, shows the IDE after adding welcome.cpp to the project. In Fig. 1.9
we clicked the + character next to Source Files to see that the C++ source file is indeed
part of the project. The plus + becomes a minus -, and vice versa, when clicked.
We are now ready to write a C++ program. Type the following sample program into
the source code window. [Note: The code examples for this book are available at the Deitel
& Associates, Inc. Web site (www.Deitel.com). Click the “downloads” link to go to our
downloads page. The line numbers are for presentational purposes and are not part of the
program code.]
1 #include <iostream>
2
3 using std::cout;
4 using std::endl;
5
6 int main()
7 {
8 cout << "Welcome to Visual C++!" << endl;
9 return 0;
10 }
The source code window is maximized (also called docked) in Fig. 1.9. Click the Restore
button (see Fig. 1.9) to restore the source code window to its default size.
The Visual C++ IDE has a highlighting scheme called syntax coloring for the key-
words and comments in a C++ source file—you may have noticed this while you were
typing the program. Syntax color highlighting is applied as you type your code and is
applied to all source files opened in Visual C++. By default, keywords appear in blue, com-
ments in green and other text in black, but you can set your own color preferences.
Testing and Debugging Tip 1.2
Visual C++’s syntax highlighting helps the programmer avoid using keywords accidentally
as variable names. If a name appears blue (or whatever color you have selected for key-
words), it is a keyword and you should not use it as a variable name or other identifier.
1.2
Another useful editor feature is IntelliSense®. When typing certain language elements, In-
telliSense displays help automatically to let the programmer select a symbol from a list of
names that can appear in the current context in the program; this saves typing time as well
as the time it might otherwise take the programmer to look up options.
Figure 1.10 shows two examples of IntelliSense. When function srand’s opening
parenthesis is typed, Visual C++ automatically displays its function header as a tip. When
the dot (.) operator is typed after struct variable c, a list appears containing the valid
members.
Testing and Debugging Tip 1.3
IntelliSense helps the programmer type a correct program. 1.3
After you have typed the program, click Save (in the File menu) or click the save
button (the one that resembles a floppy disk icon) on the tool bar to save the file.
Before executing a program, you must eliminate all syntax errors (also called compi-
lation errors) and create an executable file. A syntax error indicates that code in the pro-
gram violates the syntax (i.e., the grammatical rules) of C++.
To compile the C++ file into an executable, click the Build menu’s Build wel-
come.exe command or press the F7 key. Compiler messages and errors appear in the
output pane’s Build tab Fig. 1.11). If there are no errors when compilation is complete,
welcome.exe - 0 error(s), 0 warning(s) should appear at the bottom of the output
pane’s Build tab as shown in Fig. 1.11.
If an error message appears in the output pane’s Build tab, double-clicking anywhere
on the error message displays the source file and places a blue-arrow marker in the margin
indicator bar (i.e., the gray strip to the left of the source code), indicating the offending line
as shown in Fig. 1.12. The error in this particular case is a missing < character after cout.
Error messages are often longer than the output pane’s width. The complete error mes-
sage can be viewed either by using the horizontal scrollbar to the right of the tabs at the
bottom of the screen (e.g., Build, Debug) or by reading the status pane. The status pane
displays only the selected error message.
If you do not understand the error message, highlight the error message number by
dragging the mouse over the number, then pressing the F1 key. This displays a help file that
provides information about the error and some helpful hints as to the cause of the error.
Please keep in mind that C++ compilers may mark a line as having an error when, in fact,
the error occurs on a previous line of code.
Blue
marker
Status
pane
Fig. 1.12 Blue marker indicating that a line contains a syntax error.
After fixing the error(s), recompile the program. C++ compilers often list more errors
than actually occur in the program. For example, a C++ compiler may locate a syntax error
in your program (e.g., a missing semicolon). That error may cause the compiler to report
other errors in the program when, in fact, there may not be any other errors.
Testing and Debugging Tip 1.4
When a syntax error on a particular line is reported by the compiler, check that line for the
syntax error. If the error is not on that line, check the preceding few lines of code for the
cause of the syntax error. 1.4
Once the program compiles without errors, you can execute the program by clicking
Execute welcome.exe in the Build menu. If you are using the Visual C++ 6 Introduc-
tory Edition software, a dialog is displayed containing the message
Note: The terms of the End User License Agreement for Visual C++ Introductory
Edition do not permit redistribution of executable you create with this product.
each time a program is executed (users of the Standard, Professional and Enterprise Edi-
tions of Visual C++ 6 will not see this message). The program is executed in an MS-DOS
window as shown in Fig. 1.13. Pressing any key closes the MS-DOS window.
©2000 Deitel & Associates, Inc.—All rights reserved.
©2000 Prentice Hall—All rights reserved.
16 GETTING STARTED WITH MICROSOFT® VISUAL C++™ 6 CHAPTER 1
To create another application, you follow the same steps outlined in this section using
a different project name and directory. Before starting a new project you should close the
current project by selecting the File menu’s Close Workspace option. If a dialog appears
asking if all document windows should be closed or if a file should be saved, click Yes.
You are now ready to create a new project for your next application or open an existing
project. To open an existing project, in the File menu you can select the Recent Work-
spaces option to select a recent workspace or you can select Open... to see an Open
Workspace dialog and select a workspace (.dsw file) to open.
1.5 Debugger
The Visual C++ IDE provides a debugger tool to help the programmer find run-time logic
errors in programs that compile and link successfully but do not produce expected results.
The debugger lets the programmer view the executing program and its data as the program-
mer runs either one step at a time or at full speed. The program stops on a selected line of
code or upon a fatal run-time error. When the programmer does not understand how incor-
rect results are produced by a program, running the program one statement at a time and
monitoring the intermediate results can help the programmer isolate the cause of the error.
The code example used in the screen captures is provided as Examples/Ch01/
Fig01_18.
To use the debugger, set one or more breakpoints. A breakpoint is a marker set at a
specified line of code that causes the debugger to suspend execution of the program upon
reaching that line of code. Breakpoints help the programmer verify that a program is exe-
cuting correctly. A breakpoint is set by clicking the line in the program where the break-
point is to be placed and clicking the Insert/Remove Breakpoint button in the build
toolbar (Fig. 1.12). The Insert/Remove Breakpoint button is grayed (disabled) unless
the C++ code window is the active window (clicking in a window makes it active). When
a breakpoint is set, a solid red circle appears in the margin indicator bar to the left of the
line (Fig. 1.14). Breakpoints are removed by clicking the line with the breakpoint and
clicking the Insert/Remove Breakpoint button or pressing the F9 key.
Selecting the Go command (in the Build menu’s Start Debug submenu) starts the
debugging process. Because we have chosen to debug a console application, the console
window (i.e., MS-DOS window) that contains our application appears (Fig. 1.15). All pro-
gram interaction (input and output) is performed in this window. Program execution sus-
pends for input and at breakpoints. You may need to manually switch between the IDE and
the console window to perform input. To switch between windows you can use Alt + Tab
or click your program’s panel on the Windows taskbar at the bottom of the screen.
©2000 Deitel & Associates, Inc. and
Prentice Hall. All rights reserved.
CHAPTER 1 GETTING STARTED WITH MICROSOFT® VISUAL C++™ 6 17
A breakpoint
Fig. 1.15 C++ program executing in an MS-DOS window during debug mode.
Figure 1.16 shows program execution suspended at a breakpoint. The yellow arrow to
the left of the statement
myFunction( x );
indicates that execution is suspended at this line. This statement will be the next statement
executed. Note in the IDE that the Build menu is replaced with the Debug menu and that
the title bar displays [break] to indicate that the IDE is in debug mode.
Testing and Debugging Tip 1.6
Loops that iterate many times can be executed in full (without stopping every time through
the loop) by placing a breakpoint after the loop and selecting Go from the Debug menu. 1.6
The bottom portion of the IDE is divided into two windows—the Variables window
(i.e., the left window) and the Watch window (i.e., the right window). The Variables
window contains a list of the program’s variables. Note that different variables can be
viewed at different times, by clicking either the Auto, Locals or this tabs. The Auto tab
displays the name and value of the variables or objects (discussed in Chapter 6 of C++ How
To Program: Third Edition) used in both the previous statement and the current statement.
The Locals tab displays the name and current value for all the local variables or objects in
the current function’s scope. The this tab displays data for the object to which the exe-
cuting function belongs.
The variable values listed in the Variables window can be modified by the user for
testing purposes. To modify a variable’s value, click the Value field and enter a new value.
Any modified value is colored red to indicate that it was changed during the debugging ses-
sion by the programmer.
©2000 Deitel & Associates, Inc.—All rights reserved.
©2000 Prentice Hall—All rights reserved.
18 GETTING STARTED WITH MICROSOFT® VISUAL C++™ 6 CHAPTER 1
Often certain variables are monitored by the programmer during the debugging pro-
cess—a process known as setting a watch. The Watch window allows the user to watch
variables as their values change. Changes are displayed in the Watch window.
Variables can be typed directly into the Watch window or dragged with the mouse
from either the Variables window or the source code window and dropped into the Watch
window. A variable can be deleted from the Watch window by selecting the variable name
and pressing the Delete key. The four tabs at the bottom of the Watch window are used by
the programmer to group variables. Figure 1.17 shows a watch that has been set for variable
x.
Like the Variables window, variable values can be modified in the Watch window
by editing the Value field. Changed values are colored red. Note also that the current value
of a variable during debugging can also be viewed by resting the mouse cursor over the
name of that variable in the source code window (Fig. 1.18).
Fig. 1.17 Watch window showing the current value for variable x.
©2000 Deitel & Associates, Inc. and
Prentice Hall. All rights reserved.
CHAPTER 1 GETTING STARTED WITH MICROSOFT® VISUAL C++™ 6 19
The Debug toolbar contains buttons that control the debugging process. These buttons
perform the same actions as the Debug menu items. Each button is labeled in Fig. 1.19.
The Debug toolbar can be displayed by positioning the mouse pointer over an empty
region of the main menu or any toolbar, right-clicking the mouse and selecting the Debug
option in the popup menu.
The Restart button restarts the application, stopping at the beginning of the program
to allow the programmer to set breakpoints before starting to execute the code. The Stop
Debugging button ends the debugging session to let the programmer edit and rebuild the
program before running another test.
Break Execution suspends program execution at the current location. Apply Code
Changes allows the programmer to modify the source code while in debug mode. [Refer
to the on-line documentation for limitations on this feature.] Show Next Statement
places the cursor on the same line as the yellow arrow that indicates the next statement to
execute. Show Next Statement is useful to reposition the cursor to the same line as the
yellow arrow when editing the source code during debugging.
Restart
Run to Cursor
Registers
QuickWatch Watch Variables Disassembly
Memory
Call Stack
The Step Into button executes program statements, one per click, including code in
functions that are called, allowing the programmer to confirm the proper execution of the
function, line-by-line. The results of the “step into” operation are shown in Fig. 1.20. Func-
tions that can be stepped into include programmer-defined functions and C++ library func-
tions. If you want to step into a C++ library function, Visual C++ may ask you to specify
the location of that library.
The Step Over button executes the next executable line of code and advances the
yellow arrow to the following executable line in the program. If the line of code contains a
function call, the function is executed in its entirety as one step. This allows the user to exe-
cute the program one line at a time and examine the execution of the program without
seeing the details of every function that is called. This is especially useful at cin and cout
statements.
The Step Out button allows the user to step out of the current function and return con-
trol back to the line that called the function. If you Step In to a function that you do not
need to examine, click Step Out to return to the caller.
If there is a section of code which you do not want to step through, click the mouse on
the line following this code (the next line you would like to step through) and select Run
to Cursor. This will execute all code up to the line where the cursor is positioned. This
technique is useful for executing loops or functions without having to enter the loop or
function.
The QuickWatch button displays the QuickWatch dialog (Fig. 1.21), which is
useful for monitoring expression values and variable values. The QuickWatch dialog pro-
vides a “snapshot” of one or more variable values at a point in time during the program’s
execution. To watch a variable, enter the variable name or expression into the Expression
field and press Enter. As with the Variables window and Watch window, values can be
edited in the Value field, but changed values are not color coded red. Clicking Recalcu-
late is the same as pressing Enter.
To maintain a longer watch, click the Add Watch button to add the variable to the
Watch window. When the QuickWatch dialog is dismissed by clicking Close, variables
in the dialog are not preserved. The next time the QuickWatch dialog is displayed, the
Name and Value fields are empty. The QuickWatch window can also be used to eval-
uate expressions such as arithmetic calculations (e.g., a + b - 9, etc.) and variable assign-
ments (e.g., x = 20, etc.) by typing the expression into the Expression field.
The Watch button displays the Watch window. The Variables button displays the
Variables window.
The Memory button displays the Memory window and the Registers button dis-
plays the Registers window (these buttons are beyond the scope of this appendix).
The Call Stack button displays a window containing the program’s function call
stack. A function call stack is a list of the functions that were called to get to the current line
in the program. This helps the programmer see the flow of control that led to the current
function being called. An alternative way of viewing the call stack is the Context: combo
box (Fig. 1.16).
The Disassembly button displays the Disassembly window. Analyzing a program
that has been disassembled is a complex process likely to be used by only the most
advanced programmers. We do not discuss the Disassembly window in this appendix.
Each of the buttons (e.g., Variables, Watch, Memory, Registers, Call Stack and
Disassembly) act as toggle buttons—clicking them hides the window (if it is visible) or
displays the window (if it is invisible). Debug windows (e.g., Variables, etc.) can also be
displayed during the debugging session by selecting the appropriate debug window from
the View menu’s submenu Debug Windows.
Disabled
breakpoint
When you have finished your debugging session, click the Stop Debugging button
on the Debug toolbar. The environment changes back to the pre-debugging setup. Refer
to the on-line documentation for additional debugger features.