You are on page 1of 16

Visual C++ Tutorial

This tutorial is meant to be a startup tool for student unfamiliar with Microsoft Visual C+
+. It's purpose is to be a basic introduction to creating files and using the debugger,
additional information on the more in depth features of the Visual C++ program can be
found in the help files.
Getting Started
The first thing to do when beginning to use Visual C++ is to create a workspace. Go to
the file menu and click on ew.
This will bring up the following window. If it is not alread! there click on the
"orkspace tab
T!pe in a name for !our workspace and click #$. I usuall! make one workspace for
each class and then keep each assignment as a pro%ect in that workspace, but !ou can use
%ust one workspace if !ou want to.
e&t, !ou need to create a new pro%ect. 'or this, !ou also want to go to the ew menu,
but this time click on the (ro%ects tab.
)nless !ou are creating a program which will use graphics, !ou will probabl! want to
create a "in*+ Console ,pplication. Gi-e the pro%ect a name and click .,dd to current
workspace. if it is not alread! selected. Then click #$. ,fter that a window will come
up asking what kind of Console ,pplication !ou want. Make sure .empt! workspace. is
selected and then click 'inish. Click #$ in the ne&t window and !ou ha-e created a new
pro%ect. The pro%ect and its files will appear in the workspace window /the one in the
upper left corner0. 'or e&ample, I created a pro%ect called demo in m! workspace and
now the screen looks like this1
Creating Files
e&t, we will write some code for the new pro%ect. 'irst, we will create a header file for
a new class called Comple&. 2ring up the 'ile menu and click on ew. Then click on
the tab that sa!s files if it is not alread! selected. Chose C3C++ 4eader 'ile and then t!pe
the name Comple& into the name field. ,lso, make sure that .add to pro%ect. is checked.
ow, cop! the following lines into the te&t filed of the screen.
class Comple&
5
pri-ate1
double real6
double imaginar!6
public1
Comple&/06
Comple&/double,double06
double get7eal/06
double getImaginar!/06
-oid set7eal/double06
-oid setImaginar!/double06
86
The screen should look like this. 9a-e the file either b! bring up the file menu and
clicking on sa-e, or clicking the icon of a disk right below the :dit menu.
ow create a new C++ source file and cop! the following into the te&t bo&. "hen !ou
cut and paste from this page the alignment of the lines will not be correct, but this will
not affect the programs functionalit!.
;include .Comple&.h.
Comple&11Comple&/0
5
real<=6
imaginar!<=6
8
Comple&11Comple&/double r, double i0
5
real<r6
imaginar!<i6
8
double Comple&11getImaginar!/0
5
return imaginar!6
8
double Comple&11get7eal/0
5
return real6
8
-oid Comple&11set7eal/double r0
5
real<r6
8
-oid Comple&11setImaginar!/int i0
5
imaginar!<i6
8
9a-e this new file. 2ring up the build menu and click on compile.
,s !ou can see there is an error. :rrors that occur during the compiling of code are the
first t!pe of errors that !ou can fi& with this program. This one is relati-el! simple.
>ouble click on the error description and !ou will be taken to where the error is.
7eplace the highlighted .int. with .double. and tr! to compile again. This time it should
work.
ow, create a new C++ source file and call it main. Then t!pe, rather than %ust cop!ing,
the following lines
;include ?iostream.h@
;include .Comple&.h.
int main/0
5
Comple& a,b6
a.set7eal/+A06
otice that when !ou t!pe a. a list of the functions and -ariables of the comple& class will
drop down. This is a -er! hand! feature.
/If this list doesn't appear then go to the Tools menu, click on #ptions. )nder the :ditor
tab there are bo&es which will turn on and off the autoBcomplete features.0
ow write the rest of these lines.
a.setImaginar!/+.A06
cout ?? .a < . ?? a.get7eal/0 ?? . + . ?? a.getImaginar!/0 ?? .i. ?? endl6
return =6
8
9a-e this file and then go to the build menu and select build. This will compile the
necessar! files and link them together into an e&ecutable file. /ote1 if !ou e-er want to
e&clude one of the files in the pro%ect from the build, right click on the file in the
workspace window, and select settings. This will bring up the pro%ect settings window,
click on the general tab and then select .:&clude file from build.0

Debugger
ow that we ha-e an e&ecutable file, we can e&plore one of the most useful features, the
debugger. 'irst, run the program normall! b! selecting e&ecute from the build menu. ,
window should pop up that looks like this.
(ress an! ke! to make that window disappear. ow, make sure main.cpp is in the te&t
window and right click ne&t to a.set7eal/+A0. 'rom this menu select Insert37emo-e
2reakpoint.
There should now be a red dot ne&t to this line. This sets a spot where the program
e&ecution should pause while debugging. Go to the build menu, go under start debug and
click on go. This should bring up the console window and also change the main window
into debugging mode. The screen should now look something like this.
There are two new windows for the debugger. The window on the bottom left will show
-ariables that are associated with the current statement. The window on the bottom right
contains -ariables that !ou want to keep and e!e on. Go up to the te&t window and right
click on b. Click on Cuick"atch and select ,dd"atch. This -ariable will now alwa!s
be accessible in the bottom right window.
ow, we will begin stepping through the program. 'irst, select 9tep #-er /either from
the >ebug menu, from the icon below the menu bar or 'D=0 This will mo-e to the ne&t
line regardless of whether there was a function in the line it was on. If !ou click on the +
ne&t to a in the bottom left window, !ou will see that the -alue of real has changed to +A.
ow, rather than using 9tep #-er, select 9tep Into for the ne&t line. This will take !ou to
the function bod! of the setImaginar! function. Eou will see the -alue of i in the lower
left window, and if !ou step to the ne&t line imaginar! will also show up.
Fea-e this function b! selecting 9tep #ut.
'inish the program e&ecution either b! selecting Go or 9top >ebugging from the >ebug
menu.
This concludes the Visual C++ tutorial.

You might also like