Sie sind auf Seite 1von 18

1

This appendix contains the instructions for using Microsoft Visual C++ 2010, which is a full-featured
Integrated Development Environment (IDE) for the C++ language. Visual C++ 2010 is available as a
stand-alone product, called Visual C++ 2010 Express Edition, or as part of Visual Studio 2010. At the
time of this writing, you can download a free copy of Visual C++ 2010 Express Edition at
www.microsoft.com/express/Downloads/#2010-Visual-CPP.

To start Visual Studio 2010 or Visual C++ Express Edition:


1. Click the Start button on the Windows 7 taskbar and then point to All Programs.
2. If you are using Visual Studio 2010, click Microsoft Visual Studio 2010 on the All Programs
menu and then click Microsoft Visual Studio 2010. If the Choose Default Environment Settings
dialog box appears, click Visual C++ Development Settings and then click Start Visual
Studio.
If you are using Visual C++ 2010 Express Edition, click Microsoft Visual Studio 2010 Express
on the All Programs menu and then click Microsoft Visual C++ 2010 Express.
3. A copyright screen appears momentarily, and then either the Microsoft Visual Studio window or the
Microsoft Visual C++ 2010 Express window opens. Click Window on the menu bar, then click
Reset Window Layout, and then click the Yes button.
4. If necessary, select the Close page after project load and Show page on startup check boxes
on the Start Page. When you start the Professional Edition of Visual Studio 2010, your screen will
appear similar to Figure D-1. (If you are using a different edition of Visual Studio 2010, your startup

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
2

screen might look slightly different than the one shown in Figure D-1.) When you start the Express
Edition of Visual C++ 2010, on the other hand, your screen will appear similar to Figure D-2.

Important note: To select a different window layout, click Tools on the menu bar, click Import and
Export Settings, select the Reset all settings radio button and then click the Next button. Select the
appropriate radio button in the Save Current Settings pane, click the Next button, click the preferred
collection of settings (such as Visual C++ Development Settings), and then click the Finish button.
Click the Close button.

Auto Hide button

Toolbox
window

be sure these
check boxes
are selected

Figure D-1 Microsoft Visual Studio 2010 Professional startup screen

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
3

Auto Hide button

Toolbox
window

be sure these
check boxes are
selected

Figure D-2 Microsoft Visual C++ 2010 Express startup screen

Setting the Appropriate Options


Next, you will set some options so that your screen agrees with the figures and instructions in this
appendix.

To set the appropriate options:


1. If you are using Visual C++ 2010 Express Edition, click Tools on the menu bar, point to Settings,
and then click Expert Settings (if necessary) to select it.
2. Click Tools on the menu bar and then click Options to open the Options dialog box.
3. Click the Projects and Solutions node. Use the information shown in Figure D-3 to select and
deselect the appropriate check boxes. (Your dialog box will look slightly different if you are using the
Express Edition.) When you are finished, click the OK button to close the Options dialog box.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
4

select these five


check boxes and
deselect the
others

Figure D-3 Completed Options dialog box

Managing the Windows in the IDE


Usually, you will find it easier to work in the IDE if you either close or auto-hide the windows you are not
currently using. The easiest way to close an open window is to click the Close button on the window’s
title bar. In most cases, the View menu provides an appropriate option for opening a closed window.
Rather than closing a window, you also can auto-hide it. You auto-hide a window using the Auto Hide
button (shown earlier in Figures D1 and D2) on the window’s title bar. The Auto Hide button is a toggle
button: clicking it once activates it, and clicking it again deactivates it. The Toolbox windows in Figures
D1 and D2 are auto-hidden. In the next set of steps, you will close the windows that you will not need.
You also will practice auto-hiding and displaying the Solution Explorer window.

To manage the windows in the IDE:


1. Place your mouse pointer on the Toolbox tab. When the Toolbox window slides into view, click the
Close button on its title bar.
2. If the Document Outline tab appears in the IDE, place your mouse pointer on the tab and then click
the Close button on the window’s title bar.
3. If the Server Explorer tab appears in the IDE, place your mouse pointer on the tab and then click the
Close button on the window’s title bar.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
5

4. If necessary, close the following windows by clicking their tabs and then clicking the Close button
on their title bars: Class View, Property Manager, Team Explorer, Code Definition, Output, and
Database Explorer.
5. If the Solution Explorer window is not open in the IDE, click View on the menu bar and then click
Solution Explorer.
6. Click the Auto Hide (vertical pushpin) button on the Solution Explorer window’s title bar to auto-
hide the window.
7. Place your mouse pointer on the Solution Explorer tab and then click the Auto Hide (horizontal
pushpin) button on the window’s title bar to permanently display the window.

Creating a Console Application


Applications created in either Visual Studio 2010 or Visual C++ 2010 Express are composed of solutions,
projects, and files. A solution is a container that stores the projects and files for an entire application. A
project also is a container, but it stores files associated with that particular project. Although the idea of
solutions, projects, and files may sound confusing, the concept of placing things in containers is nothing
new to you. Think of a solution as being similar to a drawer in a filing cabinet. A project then is similar to
a file folder that you store in the drawer, and a file is similar to a document that you store in the file
folder. You can place many file folders in a filing cabinet drawer, just as you can place many projects in a
solution. You also can store many documents in a file folder, similar to the way you can store many files
in a project. Figure D-4 illustrates this analogy.

Figure D-4 Illustration of a solution, project, and file

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
6

Although you can create many different types of C++ applications, the applications you will create in this
book are C++ console applications. A console application is a program that runs in a Command Prompt
window.

To create a C++ console application:


1. Click File on the menu bar, point to New, and then click Project. The New Project dialog box
opens.
2. If necessary, click Visual C++ in the list of Installed Templates, and then (if necessary) click Win32
Console Application in the middle column of the dialog box.
3. If necessary, select the Create directory for solution check box.
4. Change the name in the Name box to MyFirst Project. Change the name in the Solution name box
to MyFirst Solution. Click the Browse button, which appears next to the Location box, to open
the Project Location dialog box. Locate and then click the Cpp6 folder, and then click the Select
Folder button. See Figures D-5 and D-6.

your drive letter


might be different

Figure D-5 Completed New Project dialog box in Visual Studio 2010

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
7

your drive letter


might be different

Figure D-6 Completed New Project dialog box in Visual C++ 2010 Express

5. Click the OK button to close the New Project dialog box. The Win32 Application Wizard dialog box
appears and displays the Welcome to the Win32 Application Wizard pane. Click the Next button to
display the Application Settings pane. Verify that the Console application radio button is selected.
Select the Empty project check box. See Figure D-7.

this radio button


should be selected

select this check box

Figure D-7 Completed Win32 Application Wizard dialog box

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
8

6. Click the Finish button. The computer creates a solution and adds a Visual C++ project to the
solution. It also records the names of the solution and project, as well as other information pertaining
to the project, in the Solution Explorer window. See Figure D-8.

Figure D-8 Solution and project names shown in the Solution Explorer window

Adding a New Source File to an Open Project


To add a new source file to an open project:
1. Right-click the Source Files folder in the Solution Explorer window. Point to Add and then click
New Item to open the Add New Item dialog box. Click C++ File (.cpp) in the middle column of the
dialog box. Change the name entered in the Name box to MyFirstSourceFile. Figure D-9 shows
the completed dialog box in Visual Studio 2010. Your dialog box will look slightly different if you are
using the Express Edition.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
9

select this
template

Figure D-9 Completed Add New Item dialog box in Visual Studio 2010

2. Click the Add button to close the Add New Item dialog box. The computer adds an empty C++
source file to the project. It also records the file’s name (MyFirstSourceFile.cpp) in the Solution
Explorer window. See Figure D-10.
source filename

insertion point

you can use this list box


to increase/decrease the
size of the font used in the
source filename source file’s window

Figure D-10 Source file added to the project

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
10

Adding an Existing Source File to a Project


Although you don’t need to add an existing source file to the current project, the following steps show
you how to accomplish that task.

To add an existing source file to an open project:


1. Right-click the Source Files folder in the Solution Explorer window. Point to Add and then click
Existing Item to open the Add Existing Item dialog box.
2. Locate and then click the name of the file you want to add to the project, and then click the Add
button.

Entering C++ Program Instructions


In the next set of steps, you will enter some C++ program instructions in the source file’s editor window.
Do not be concerned if you do not understand the instructions. The instructions are explained in Chapter
4.

To enter C++ program instructions in the source file’s editor window:


1. Auto-hide the Solution Explorer window. Doing this allows you to view more of the source file’s
editor window.
2. Enter the C++ program instructions shown in Figure D-11 in the MyFirstSourceFile.cpp window. Be
sure to use the exact capitalization and punctuation shown in the figure. The asterisk on the
MyFirstSourceFile.cpp tab indicates that the changes made to the file have not been saved.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
11
asterisk

Figure D-11 C++ instructions entered in the editor window

3. Save the program. You can do so by clicking File on the menu bar and then clicking Save All. Or,
you can click the Save All button on the standard toolbar.

Important note: If you prefer to use the buttons on the standard toolbar, it’s best to use the Save All
button and not the Save button. This is because the Save button saves only the changes made to the item
selected in the Solution Explorer window. For example, when the source filename is selected, the Save
button saves only the changes made to the source file. Similarly, when the project name is selected, only
changes made to the files included in the project are saved. The tooltip box that appears when you rest
your mouse pointer on the Save button indicates the files that will be saved. In this case, the tooltip box
will say “Save MyFirstSourceFile.cpp” when the source file’s name is selected in the Solution Explorer
window, “Save MyFirst Project” when the project’s name is selected, and “Save MyFirst Solution.sln”
when the solution’s name is selected.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
12

Running a C++ Program


Before you run a C++ program, you need to save it and then build it. Building a C++ program involves
compiling the source code into object code and then invoking the linker program to link the object code
to other machine code necessary for the program to run. (You will learn about the compiler and linker
programs in Chapter 4.)

To run a C++ program:


1. Save the program (if necessary).
2. Click Build on the menu bar and then click Build Solution. (If you are using Visual C++ 2010
Express Edition and do not have the Build menu, click Tools on the menu bar, point to Settings, and
then click Expert Settings.) The compiler translates the source code into object code, and the linker
links the object code with other machine code. If your program does not contain any errors, the
“Build succeeded” message appears in the lower-left corner of the Output window. In addition, the
Output window displays the message “Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped”. See
Figure D-12. The messages indicate that the program was built (compiled and linked) successfully.
Important note: If the compiler found errors in your program, carefully compare your code with
the code shown in Figure D-11. Make any needed corrections and then repeat Steps 1 and 2.

Figure D-12 Status messages shown in the Output window

3. Click Debug on the menu bar and then click Start Without Debugging. The program’s output
appears in a Command Prompt window, as shown in Figure D-13.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
13

Figure D-13 Program output appears in a Command Prompt window

Important note: Be sure to use the Start Without Debugging option rather than the Start
Debugging option. If you use the Start Debugging option, the editor will not automatically display the
“Press any key to continue” message in the Command Prompt window. Instead, the window will close
immediately after the program output is displayed. If you want to use the Start Debugging option,
you will need to enter the system(“pause”); statement above the return 0; statement in the program.

4. You can change the color of the Command Prompt window’s background and text to make the
display clearer. Right-click the Command Prompt window’s title bar and then click
Properties. Click the Colors tab. Select the Screen Text radio button and then click the black
square. Click the Screen Background radio button and then click either the light gray square
or the white square.
5. You also can change the font used to display the text in the Command Prompt window. Click the
Font tab. Click Lucida Console in the list of fonts and then select the Bold fonts check box. Click
20 in the Size list box and then click the OK button. See Figure D-14.

Figure D-14 Result of changing some of the Command Prompt window’s properties

6. Press Enter (or any key) to close the Command Prompt window.
7. Close the Output window by clicking the Close button on its title bar.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
14

Displaying Line Numbers in the Editor Window


At times, it’s helpful to display lines numbers in the editor window.

To display lines numbers in the editor window:


1. Click Tools on the menu bar and then click Options to open the Options dialog box.
2. Expand the Text Editor node (if necessary) and then click C/C++. Select the Line numbers check
box. See Figure D-15.

select this check box

Figure D-15 Options dialog box

3. Click the OK button to close the Options dialog box. See Figure D-16.

Figure D-16 Line numbers in the editor window

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
15

Debugging a C++ Program


As you will learn in Chapter 4, the errors in a program are called bugs, and the process of locating and
correcting a program’s bugs is called debugging. Many program bugs are caused by syntax errors. A
syntax error occurs when you break one of the programming language’s rules, such as typing cut rather
than cout. In the next set of steps, you will introduce two syntax errors into the current program. You
then will learn how to locate and correct the errors.

To introduce two syntax errors in the current program and then debug the program:
1. Change cout in Line 8 to cut. Also delete the semicolon that appears at the end of Line 9. See Figure
D-17.

first error

second error

Figure D-17 Two errors in the MyFirstSourceFile.cpp program

2. Save the program. Click Build on the menu bar and then click Build Solution. The Error List
window indicates that the program contains 4 errors. Double-click the first error message in the
Error List window. See Figure D-18.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
16

arrow

Error
List
window

Figure D-18 Errors listed in the Error List window

3. The editor displays an arrow at the location where the error was encountered in the program. In this
case, the arrow is pointing to the statement on Line 8. The error message indicates that the word
“cut” is undeclared. This simply means that the compiler does not recognize the word. Change cut in
Line 8 to cout.
4. Save and then build the program. The Error List window indicates that the program now contains 2
errors. Double-click the first error message in the Error List window. See Figure D-19.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
17

Figure D-19 Remaining errors listed in the Error List window

5. The error message indicates that a semicolon is missing before the word “return”. Although the error
message and arrow refer to Line 10, it’s the instruction in Line 9 that caused the error. Recall that you
deleted the semicolon at the end of that line. Type ; (a semicolon) at the end of Line 9.
6. Save and then build the program. The Output window indicates that the build was successful.
7. Finally, click Debug on the menu bar and then click Start Without Debugging to run the
program. Press any key to close the Command Prompt window.
8. Close the Output and Error List windows.

Printing a C++ Program


To print a program’s instructions:
1. Click File on the menu bar and then click Print to open the Print dialog box. If the Include line
numbers check box is selected, line numbers will be printed even if they do not appear in the editor
window. If the Include line numbers check box is not selected, no line numbers will appear on the
printout, even though they may appear in the editor window.
2. Select the Include line numbers checkbox. If your computer is connected to a printer, click the
OK button to begin printing; otherwise, click the Cancel button.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)
18

Closing the Current Solution


You close a solution using the Close Solution option on the File menu. Be sure to use the Close Solution
option and not the Close option. The Close option on the File menu does not close the solution; instead,
it closes only the editor window.

To close the current solution:


1. Click File on the menu bar and then click Close Solution.
2. Temporarily display the Solution Explorer window to verify that the entire solution is closed.

Opening an Existing Solution


You can use the File menu to open an existing solution. If a solution is already open in the IDE, it is
closed before another solution is opened.

To open the MyFirstProject solution:


1. Display the Solution Explorer window. Click File on the menu bar, point to Open, and then click
Project/Solution to open the Open Project dialog box.
2. Locate and then open the Cpp6\MyFirst Solution folder. Click MyFirstSolution.sln in the list of
filenames and then click the Open button. If necessary, double-click the name of the .cpp file in
the Solution Explorer window to open the file in the editor.

You have completed Appendix D.

Appendix D – 5/25/2010
An Introduction to Programming with C++, Sixth Edition (0538466529)

Das könnte Ihnen auch gefallen