Tuesday, June 4, 2013

Microsoft Visual C++ Tutorial - Beginners

Microsoft 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 New.

This will bring up the following window.  If it is not already there click on the Workspace tab

Type in a name for your workspace and click OK.  I usually make one workspace for each class and then keep each assignment as a project in that workspace, but you can use just one workspace if you want to.
Next, you need to create a new project.  For this, you also want to go to the New menu, but this time click on the Projects tab.

Unless you are creating a program which will use graphics, you will probably want to create a Win32 Console Application.  Give the project a name and click "Add to current workspace" if it is not already selected.  Then click OK.  After that a window will come up asking what kind of Console Application you want.  Make sure "empty workspace" is selected and then click Finish.  Click OK in the next window and you have created a new project.  The project and its files will appear in the workspace window (the one in the upper left corner).  For example, I created a project called demo in my workspace and now the screen looks like this:

Creating Files

Next, we will write some code for the new project.  First, we will create a header file for a new class called Complex.  Bring up the File menu and click on New.  Then click on the tab that says files if it is not already selected.  Chose C/C++ Header File and then type the name Complex into the name field.  Also, make sure that "add to project" is checked.

Now, copy the following lines into the text filed of the screen.
class Complex
{
private:
double real;
double imaginary;
public:
Complex();
Complex(double,double);
double getReal();
double getImaginary();
void setReal(double);
void setImaginary(double);
};
The screen should look like this.  Save the file either by bring up the file menu and clicking on save, or clicking the icon of a disk right below the Edit menu.

Now create a new C++ source file and copy the following into the text box.  When you cut and paste from this page the alignment of the lines will not be correct, but this will not affect the programs functionality.
#include "Complex.h"

Complex::Complex()
{
real=0;
imaginary=0;
}

Complex::Complex(double r, double i)
{
real=r;
imaginary=i;
}

double Complex::getImaginary()
{
return imaginary;
}

double Complex::getReal()
{
return real;
}

void Complex::setReal(double r)
{
real=r;
}

void Complex::setImaginary(int i)
{
imaginary=i;
}
Save this new file.  Bring up the build menu and click on compile.

As you can see there is an error.  Errors that occur during the compiling of code are the first type of errors that you can fix with this program.  This one is relatively simple.  Double click on the error description and you will be taken to where the error is.

Replace the highlighted "int" with "double" and try to compile again.  This time it should work.
Now, create a new C++ source file and call it main.  Then type, rather than just copying, the following lines
#include <iostream.h>
#include "Complex.h"

int main()
{
Complex a,b;
a.setReal(25);

Notice that when you type a. a list of the functions and variables of the complex class will drop down.  This is a very handy feature.
(If this list doesn't appear then go to the Tools menu, click on Options.  Under the Editor tab there are boxes which will turn on and off the auto-complete features.)
Now write the rest of these lines.
a.setImaginary(2.5);
cout << "a = " << a.getReal() << " + " << a.getImaginary() << "i" << endl;
return 0;
}
Save this file and then go to the build menu and select build.  This will compile the necessary files and link them together into an executable file.  (Note: if you ever want to exclude one of the files in the project from the build, right click on the file in the workspace window, and select settings.  This will bring up the project settings window, click on the general tab and then select "Exclude file from build")

Debugger

Now that we have an executable file, we can explore one of the most useful features, the debugger.  First, run the program normally by selecting execute from the build menu.  A window should pop up that looks like this.

Press any key to make that window disappear.  Now, make sure main.cpp is in the text window and right click next to a.setReal(25).  From this menu select Insert/Remove Breakpoint.

There should now be a red dot next to this line.  This sets a spot where the program execution 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 variables that are associated with the current statement.  The window on the bottom right contains variables that you want to keep and eye on.  Go up to the text window and right click on b.  Click on QuickWatch and select AddWatch.  This variable will now always be accessible in the bottom right window.

Now, we will begin stepping through the program.  First, select Step Over (either from the Debug menu, from the icon below the menu bar or F10)  This will move to the next line regardless of whether there was a function in the line it was on.  If you click on the + next to a in the bottom left window, you will see that the value of real has changed to 25.

Now, rather than using Step Over, select Step Into for the next line.  This will take you to the function body of the setImaginary function.  You will see the value of i in the lower left window, and if you step to the next line imaginary will also show up.

Leave this function by selecting Step Out.
Finish the program execution either by selecting Go or Stop Debugging from the Debug menu.
This concludes the Visual C++ tutorial.

No comments:

Post a Comment