Microsoft Visual C++.NET Tutorial
Microsoft Visual C++.NET Tutorial
I. Introduction
This tutorial teaches the basics of the Integrated Development Environment (IDE) of
Microsoft Visual Studio .NET. It introduces in detailed steps how to enter, compile,
link, and execute a C++ program in the IDE. Using two simple examples – a single
file and a multiple-file program – the beginning students should be able to learn
quickly the needed skills to carry out programming assignments in CS230, CS240,
and other and other CS courses.
The IDE is Graphical User Interface or GUI-based command center where all the
tools needed for software development are accessible through menus and tool bars.
Although Microsoft .NET supports many different programming languages, we focus
our discussion on C++.
2
Figure 1 – Relationships between Solution, Projects, and Files (Koneman)
1. Menu items appear in boldface. Two or more items selected or clicked in a row
are separated by arrows “->”, e.g., when we say click File -> Save As…, we
mean click File then click Save As…, where File and Save As… are items in the
File menu.
2. Words or phrases other than menu items that are displayed in windows or in
dialog boxes appear in bold and italicized, e.g., Solution Explorer, New Project,
Open Project, etc.
3. Words entered into a dialog box by the user are italicized without being bolded,
e.g., HelloWorld is the name entered by the user for the Solution as well as for the
Project in our first tutorial.
4. Program code is shown is shown in Courier New font; all others are in Times
New Roman font.
V. Summary of C++ program development steps: Assuming you have a new program to
compile and run and the program is ready to be typed in, you need to go through the
following steps. These steps may not make much sense to you at the moment but they
will once you finished the tutorials. You may come back here for a review and print
out this page as a quick reference.
1. Launching the Microsoft Visual Studio .NET
2. Setting the Profile for C/C++ program development
3. Creating an empty new C/C++ Project by selecting
a. Select Win32 as the Project Type
b. Select Win32 Console Project as the Template
3
c. Specify Disk drive and path; then enter a name for a folder in which all
files associated with the project are to be stored.
4. Entering C++ source code into file or files
5. Compiling, linking, and executing the project
6. Printing the source code and the output in the console window
7. Debugging basics
a. Syntax errors
b. Runtime errors
c. Logic errors
4
Tutorial I: Single-file project:
We use a simple program that displays a “Hello World” message on the screen as our demo. The
source code will be entered into a file named HelloWorld.cpp.
From the desktop, double click on the icon of MS Visual Studio .NET. If the .NET icon
is not on the desktop, click start (in the task bar of the Windows) -> Program ->
Microsoft Visual Studio .NET 2003 -> Microsoft Visual Studio .NET 2003. The
system displays the main window of the Visual Studio .NET as shown is Figure 1.
Note that the title bar displays Microsoft Design Environment [Design] – Start Page;
it will change once we enter a name for the project later. As shown, the main .NET
window contains a title bar, a menu bar, a standard tool bar, and a build tool bar. New
tools bars can be added to and existing ones removed from and the main window by
simply right clicking anywhere in the tool bar area; from the pop-up menu, you may them
select or de-select them. The standard and build tool bars used frequently. The debug
tool bar, which is not shown in Figure 1, is useful to debug or locate errors in a program.
The main window may contain several smaller windows and each serve a particular
purpose. As shown in Figure 1, the three smaller windows displayed are the Solution
5
Explorer, the Dynamic Help, and the Start Page windows. The Solution Explorer
window allows you to locate and work with individual files of you program while
Dynamic Help allows you to get online help quickly. Smaller windows may be deleted
from the main window. For example, you may choose to remove the Dynamic help
window by clicking the Close button at the upper right corner of this window. If this
window is not on the screen, you can display it by clicking Help -> Dynamic Help.
When many small windows are opened, the screen may be cluttered, especially if you
do not have a big monitor. Instead of closing down some windows, you may want to use
the Auto Hide feature associated with most of the small windows to avoid the cluttering
of the screen. Notice that there is a “push pin” icon to the left of the Close button in the
title bars of the Solution Explorer and Dynamic Help windows. When the push pin
points downward, the associated window is displayed as shown in Figure 1. By clicking
on it, the push pin points to the left and the window shrinks to an icon or “hidden” on the
left of the main window, as shown in Figure 2. By moving the cursor over it, the window
is restored; by moving the cursor away from the window, the window automatically
shrinks to an icon.
6
2. Setting the Profile for C/C++ program development
Click the My Profile tab as shown in Figure 2. From top toward bottom of the drop-
down lists, select Visual C++ Developer; Visual C++ 6 for Keyboard Scheme; Visual
C++ 6 for Window Layout; and Visual C++ for Help Filter and the remaining ones as
shown in Figure 3. Note that this is a one-time operation and there is no need to do it
again if the settings have not been changed.
Click the Projects tab in the Start Page window and then click the New Project
button at the lower left of the window, the New Project dialog box as shown in Figure 4
is displayed. Click the More button at the lower left corner to make sure the dialog box is
fully displayed. A dialog box is by itself a small window; it is used by the system to
prompt user to enter information. In the Project Type pane on the left, open the Visual
C++ Projects folder by clicking the “+” symbol to its left and select and Win32 as
Project Type. In the Templates pane on the left, select Win32 Console Project as the
template. Remember that, as discussed in the introduction section, a console project is a
project whose input and output are streams of characters and are displayed in the “DOS”
box.
7
Enter Hello World as the name of project in the Name box and specify where you
would like to have the project stored by directly entering drive label and path name in the
Location box. Alternatively, you may use the Browse button to do the same. In this
demo, we use the Browse button and select C:\Visual Studio .NET Projects as the place
to store our project, where C: is the drive label and Visual Studio .NET Projects is the
path name. A path name is actually a folder in which files are stored. The folder may
have been previously created; if it was not, the system will be created automatically for
the project. Note that you may choose any drive and name a folder any way you like.
Also notice that we have left Create directory for Solution unchecked, meaning the
Solution contains only a single Project (defined in the introduction section), and
therefore, share the same folder. For very large software where the Solution may contain
multiple Projects, you may want to check the box and enter a name for the Solution.
Click the OK button, the .NET display the following Win32 Application Wizard –Hello
World window as shown is Figure 5.
8
Figure 5 - Win32 Application Wizard window - Overview
Click the Application Settings tab to display the window shown in Figure 6 and make
sure that the Empty project box is checked!!!
9
Click the Finish button to display the main .NET window shown in Figure 7. Notice that
the title bar of the main window is changed from Microsoft Design Environment
[Design] – Start Page to HelloWorld – Microsoft Visual C++ [Design] – Start Page.
Also note that we have clicked the push pin icon in the title bar of the Solution Explorer
window to turn off the Auto Hide feature so that the window is fully displayed. In the
Solution Explorer window, there are 4 folders, the main HelloWord and three sub-
folders: the Source Files, Header Files, and Resource Files folders which are empty at
the moment. For single-file project, source code is always stored in the Source Files
folder as a file with a .cpp extension. For multiple-file project, all files with .cpp
extension are stored in Source Files folder and all programmer-defined header files are
stored in the Header Files folder; Resource Files folder is not used for console
applications.
Figure 7 - Four folders have been created in the Solution Explorer and window
4. Adding source file to the project and entering C++ source code for the file
Right click anywhere on the Source Files folder, a pop-up menu is displayed as shown in
Figure – 8 below.
10
Figure 8 – Pop-up menu upon right clicking anywhere on Source Files
Select Add in the pop-up menu and click Add New Item…, the system displays the Add
New Item – HelloWorld dialog box as shown in Figure 9 below. Select the Code folder
in the Categories pane on the left and C++ File(.cpp) in the Templates pane on the right,
then enter HelloWorld.cpp in the name box.
11
Accept the default location c:\Visual Studio .NET Projects\HelloWorld\ and click the
Open button. The new HelloWorld.cpp file has now been added to the Code folder in the
Solution Explorer folder and the empty file edit area appear on the right with
HelloWorld.cpp shown in the tab. We are now ready to enter the C++ source code to the
blank area.
Figure 10 – The HelloWorld.cpp had been added to the Source Files folder
12
Figure 11 – Source code has been entered in the code editing area
Compiling, linking, and executing are normally performed in three separate steps through
menu manipulations. There is a simple way to do the three steps all at once by clicking
the Start Without Debugging icon, a purple exclamation symbol in the Build tool bar.
The Build tool bar is shown in both Figures 10 and 11; if it is not displayed, simply right
click anywhere in the tool bar area to display the pop-up menu and then click on Build to
select it. We now click on the purple exclamation icon, the system displays following
Microsoft Development Environment dialog box:
Click Yes button, the system goes through compile and link steps and produces a
summary report about the build process in the Output window shown in Figure 13 below.
As the message indicates, the build process is successful since both compiling and linking
are without errors.
13
Figure 13 – The Output window showing a summary report of the Build process
Since there is no error in compiling and linking, the project is automatically executed and
results produced and displayed in the console or DOS window as shown in Figure 14
below. Note that the console window displays results or outputs from the execution of
your program HelloWorld.exe whereas the Output window of Figure 13 displays the
results or outputs of the compilation and linking process.
5. Printing the source code and the output in the console window
To print the source code in the project editing window, simply click anywhere on the
window then click on File -> Print… -> OK.
To print the output result in the console window involves the following steps:
a. Click on the drive icon (C:\ in this demo) on the left end of the title bar of the console
window to display the pop-up menu then click on Edit -> Mark as shown in Figure
15. A rectangular shaped marker is displayed.
14
Figure 15 – Display the marker in the console window.
b. Drag the marker to select the output as a block of text (“Hello World.” in this demo)
as shown in Figure 16.
c. Click on the drive icon again to display the pop-up menu. In the menu, click Edit ->
Copy to copy the selected block of text to the clipboard which it can be paste to any
word processor such as Microsoft Word where it can be printed.
15
Tutorial II: Multi-file project:
Multi-file or separate file project: In the previous demo, the project consists of a single
HelloWorld.cpp file. In practice, a C++ programming project may contain many .cpp and .h files.
It is desirable to have these files separately compiled and debugged before assembling them into
a project. In this tutorial, we demonstrate how separate .cpp and .h can be assembled, compiled,
linked, and executed together. Although not as detailed as the previous tutorial, we decide to
show these steps not to avoid confusion and the need to refer back frequently.
We use a time project for the demonstration. The time project displays time of the day in both
standard (0 – 12 AM or PM) and military (0 – 23 without AM or PM) formats. The project
contains three separate files: time.h, time.cpp, and timeMain.cpp. The header file time.h contains
the declaration or interface of the time class; time.cpp contains code that implements all the
member functions of the time class; and the timeMain.cpp is the driver function that uses the
time class to display time in both military and standard formats. The detailed code is listed in the
appendix I.
From the desktop, double click on the icon of MS Visual Studio .NET. If the .NET icon
is not on the desktop, click start (in the task bar of the Windows) -> Program ->
Microsoft Visual Studio .NET 2003 -> Microsoft Visual Studio .NET 2003. The
system displays the main window of the Visual Studio .NET as shown is Figure 1.
16
2. Setting the Profile for C/C++ program development:
Click the My Profile tab as shown in Figure 2. From top toward bottom of the drop-down
lists, select Visual C++ Developer; Visual C++ 6 for Keyboard Scheme; Visual C++ 6
for Window Layout; and Visual C++ for Help Filter and the remaining ones as shown in
Figure 2.
Enter time as the name for the “Project” and timeSolution as the name for the “Solution”;
specify C:\Visual Studio .NET as the drive and directory that will be used to stored all the
files for the time “Project” and the timeSolution solution. As shown in Figure 3 below,
the time project folder will be created at C:\Visual Studio .NET\timeSolutioin\time. It is
noted that in this demo, the “Solution” (timeSolution) contains only one “Project” (time);
in general, a complex “Solution” or application may contain more than one project.
17
Figure 3 –The New Project dialog box
Click the “Create directory for Solution” box and then the “OK” button, the system
displays the Win32 Application Wizard – time dialog box as shown in Figure 3 below.
18
Click the “Application Settings” tab to display the dialog box shown in Figure 4 below.
Make sure the Empty project box is checked and then click the Finish button to display
the main Visual Studio .NET window as shown in Figure 5 below. Let’s take a look at
the Solution Explorer - time pane on the left of the main window where the Solution
Explorer tab has been clicked. Notice the hierarchical organization of the solution in the
display: the timeSolution solution contains 1 project, namely, the time project and it
contains three empty folders. We will create and store all .cpp files in the Source Files
folder and all .h files in the Header Files folder as illustrated below.
Figure 4 – Three empty folders for the projects has been created
19
4. Adding files to the newly created but empty project
As mentioned in the beginning of this tutorial, the time application contains three files:
time.h, time.cpp, and timeMain.cpp. We first add time.cpp file to the Source Files folder
and then type in its source code. We then repeat the steps for the timeMain.cpp and the
time.h files. Note that in general, all the .cpp files are added to the Source Files folder
and all the .h files to the Header Files folder.
Right click anywhere on the Source Files and then click Add -> Add New Item… to
display the following dialog box (you may click on ADD Existing Item should the file
have previously been created) as shown in Figure 5.
20
The system displays the Add New Time – time dialog box as shown in Figure 6 below.
Click on Code icon in the left Categories pane and select C++ File (.cpp) in the right
Template pane; enter time as the name for the file (do not type .cpp, as the system will
automatically appends the .cpp extension).
Click the Open button the system display the screen shown in Figure 7. Notice that the
time.cpp file appears under the Source Files folder. Also shown in Figure 7 is the blank
area on the right for entering and editing the source code for the time.cpp file.
Figure 7 – Code entering/editing area on the right of the main .NET window.
21
Type in the C++ source code for time.cpp file as shown in Figure 8 below (only partially
visible; the detailed code is provided in Appendix I).
Figure 8 – Source code for the time.cpp has been entered as shown.
Repeat the above steps timeMain.cpp to the “Source files” folder and then type in its
code.
Repeat the above steps shown to add time.h to the Header Files folder and then type
in the code. The following screen shows all three files have been properly added to their
respective folders. In the code entry area, the time.h tab has been clicked so the window
shows the code of the time.h file. Now we have ready to compile, link, and execute our
program.
22
Figure 9 – All three files has been created; source code for the time.h is displayed.
Click Build Solution in Build menu or press F7, the system displays a floating
Output window shown below (Note: depending on how it is configured the Output
window may be docked right next to the .cpp and .h files. The window displays the
messages about the compilation and linking process. If your project contains syntax and
linkage errors, diagnostic messages are displayed. In our demonstration, two .cpp and one
.h files are successfully compiled and the system has produced the time.exe load module
or file for execution.
23
b. To execute the load module:
Click Debug -> Start Without Debugging or press Ctrl+F5, the load module
(time.exe file) is executed and the result is displayed in the console or DOS box as shown
below:
Figure 11 – The console or DOS window displays the result of execution of time.exe file.
Note that as demonstrated in tutorial I, steps 5.a and 5.b can be consolidated into a single
step by clicking the Start Without Debugging icon, an exclamation symbol in the Build
tool bar. Again, if the Build tool bar is not displayed; simply right click anywhere in the
tool bar area and then click Build in the pop-up menu to activate it. It is possible that the
Start Without Debugging icon is not present in the Build tool bar. To display it, right
click the tool bar area and then click Customize… The system displays the Customize
dialog box shown in Figure 12 below:
24
In the box, click the Commands tab and select Debug in the Categories pane on the left
and locate the exclamation mark in the Commands pane on the right; drag the
exclamation symbol and drop it in the Build tool bar.
6. Printing the source files and the output results in the console window:
a. To print the source files: click File -> Print in the menu bar and click to print all .h
and .cpp files.
b. To print the output displayed in the DOS box: click the DOS icon at the left end of
the title bar to show the popup menu below.
Click Edit -> Mark to display a little blinking rectanglar marker at the upper left
corner in the console or DOS window. Drag the rectangle to mark or highlight the
block of text as shown in below, cut and paste it to a blank MS Word or WordPad
document and print the document from there.
25
Appendix
The source code for time.h, time.cpp, and timeMain.cpp used in Tutorial II (Deitel & Deitel)
time.h file:
#endif
time.cpp file:
// Member function definitions for Time class.
#include <iostream>
using std::cout;
#include "time.h"
26
// Print time in standard format
void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << ( minute < 10 ? "0" : "" ) << minute
<< ":" << ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM" : " PM" );
}
timeMain.cpp file:
// Driver for Time1 class
// NOTE: Compile with time1.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "time.h"
27