Beginning Visual CPP 2005 Express Part 2 - Online Article

The Visual C++ 2005 Express Edition Integrated Development Environment (IDE) offers a set of tools that help you write and edit the code for your programs, as well as detect and correct errors in your programs.

In this topic, you create a new standard C++ program and test its functionality using features available in Visual Studio for the C++ developer.

Working with Projects and Solutions

Visual Studio organizes your work in projects and solutions. A solution can contain more than one project, such as a DLL and an executable that references that DLL The first step in writing a C++ program with Visual Studio is to choose the type of project. For each project type, Visual Studio sets compiler settings and generates starter code for you.

To create a new project

1. On the File menu, point to New, and then click Project....

Image1

2. In the Project Types area, click Win32, then in the Visual Studio installed templates pane, click Win32 Console Application.

Image2

3. Type game as the project name.

When you create a new project, Visual Studio places the project in a solution. Accept the default name for the solution, which by default is the same name as the project. You can accept the default location, type in a different location, or browse to a directory where you want to save the project.

4. From the Overview page of the Win32 Application Wizard dialog, press Next.

Image3

5. Select the Empty Project setting and click Finish.

You now have a project with no source code files.

Using Solution Explorer

Solution Explorer allows you to work with files and other resources in your solution. In this step, you add a class to the project and Visual Studio adds the .h and .cpp files to your project. You then add a new source code file to the project for the main program that tests the class.

To add a class to a project

1. If the Solution Explorer window is not visible, on the View menu click Solution Explorer.

Image4

2. Right-click on the Source Files folder in Solution Explorer and point to Add, then click Class.

Image5

In the Visual C++ category, click C++ (or Generic if you are using Visual C++ Express Edition) and click C++ Class in the Visual Studio installed templates area. Click Add.

Image6

3. In the Generic C++ Class Wizard, type Cardgame as the Class name and accept the default file names, then click Finish.

Image7

4. Make these changes to the Cardgame.h file displayed in the editing area:

Add two private data members after the opening brace of the class definition:

int players; static int totalparticipants;

Add a public constructor prototype that takes one parameter of type int:

Cardgame(int p);

Delete the default constructor generated for you, since you won't need it:

Cardgame(void);

5. The Cardgame.h file should look like this after your changes:

// Cardgame.h
#pragma once
class Cardgame
{
	int players;
	static int totalparticipants;
		public:
			Cardgame(int p);
			~Cardgame(void);
}; 

6. Double-click on Cardgame.cpp in the Source Files folder to open it for editing.

7. Add the code for the constructor that takes one int argument:

Cardgame::Cardgame(int p)
{
	players = p;
	totalparticipants += p;
} 

When you begin typing pl or to, you can press Ctrl-Spacebar and auto-completion will finish typing players or totalparticipants for you.

8. Delete the default constructor that was generated for you:

Cardgame::Cardgame(void);

9. The Cardgame.cpp file should look like this after your changes:

// Cardgame.cpp
#include "Cardgame.h"
Cardgame::Cardgame(int p)
{
	players = p;
	totalparticipants += p;
}
Cardgame::~Cardgame(void) { }

Adding a Source File

In this step, you add a source code file for the main program that tests the class.

To add a new source file

1. From the Project menu, click Add New Item.

Alternatively, to use Solution Explorer to add a new file to the project, right-click on the Source Files folder in Solution Explorer and point to Add, then click New Item. In the Visual C++ area, select Code, then click C++ File (.cpp).

Image8

2. Type testgames as the Name and click Add.

3. In the testgames.cpp editing window, type the following code:

//testgames.cpp
#include "Cardgame.h"
int Cardgame::totalparticipants = 0;
int main()
{
	Cardgame *bridge = 0;
	Cardgame *blackjack = 0;
	Cardgame *solitaire = 0;
	Cardgame *poker = 0;
	try
	{
		bridge = new Cardgame(4);
		blackjack = new Cardgame(8);
		solitaire = new Cardgame(1);
		delete blackjack;
		delete bridge;
		poker = new Cardgame(5);
		delete solitaire;
		delete poker;
	}
	catch (...)
	{
		if (bridge)
			delete bridge;
		if (blackjack)
			delete blackjack;
		if (solitaire)
			delete solitaire;
		if (poker)
			delete poker;
	}
	return 0;
} 

4. On the Build menu, click Build Solution.

You should see output from the build in the Output window indicating that the project compiled without errors.

Image9

Fixing Compilation Errors

In this step you deliberately introduce a C++ syntax error in your code to see what a compilation error looks like and how to fix it. When you compile the project, an error message indicates what the problem is and where it occurred.

To fix compilation errors using the IDE

1. In testgames.cpp, delete the semicolon in the last line so it looks like this:

return 0

2. On the Build menu, click Build Solution.

3. A message in the Output window indicates that building the project failed. Click on the Go To Next Message button (the green, right-pointing arrow) in the Output window. The error message in the Output window and status bar area indicates there is a missing semicolon before the closing brace. You can press the F1 key to view more help information about an error.

4. Add the semicolon back to the end of the line with the syntax error:

return 0;

5. On the Build menu, click Build Solution. A message in the Output window indicates that the project compiled correctly.

Testing a Program

Running a program in Debug mode allows you to use breakpoints to pause the program to examine the state of variables and objects. In this step you observe the value of a variable as the program runs and deduce why the value is not what you might expect.

To run a program in Debug mode

1. Click on the testgames.cpp tab in the editing area if that file is not visible.

2. Set the current line in the editor by clicking on the following line:

Cardgame* solitaire = new Cardgame(1);

3. To set a breakpoint on that line, on the Debug menu, click Toggle Breakpoint or press F9. Alternatively, you can click in the area to the left of a line of code to set or clear a breakpoint. A red circle appears to the left of a line with a breakpoint set.

4. On the Debug menu, click Start Debugging (or press F5). When the program reaches the line with the breakpoint, execution stops temporarily (your program is in Break mode). A yellow arrow to the left of a line of code indicates that is the next line to be executed.

5. To examine the value of the totalparticipants variable, hover over it with the mouse. The variable name and its value of 12 is displayed in a tooltip window. Right-click on the totalparticipants variable and click Add Watch to display that variable in the Watch window. You can also select the variable and drag it to the Watch window.

6. On the Debug menu, click Step Over or press F10 to step to the next line of code. The value of totalparticipants is now displayed as 13.

7. Right-click on the last line of the main method (return 0;) and click Run to Cursor. The yellow arrow to the left of the code points to the next statement to be executed. The totalparticipants number should decrease when a Cardgame terminates.At this point, totalparticipants should equal 0 because all Cardgame pointers have been deleted, but the Watch 1 window indicates totalparticipants equals 18. There is a bug in the code that you will detect and fix in the next section.

8. On the Debug menu, click Stop Debugging orpress Shift-F5 to stop the program.

Debugging a Program

In this step you edit the program to fix the problem.

To fix a program with a bug

1. To see what happens when a Cardgame object is destroyed, view the destructor for the Cardgame class.

2. On the View menu, click Class View or click the Class View tab in the Solution Explorer window.

3. Open the game project tree and click on the Cardgame class. The area below shows the class members and methods.

4. Right-click on the ~Cardgame(void) destructor and click Go To Definition.

5. To decrease the totalparticipants when a card game terminates, type the following code between the opening and closing braces of the Cardgame::~Cardgame destructor:

totalparticipants -= players;

6. The Cardgame.cpp file should look like this after your changes:

// Cardgame.cpp
#include "Cardgame.h"
Cardgame::Cardgame(int p)
{
	players = p;
	totalparticipants += p;
}
Cardgame::~Cardgame(void)
{
	totalparticipants - = p;
}

7. On the Build menu, click Build Solution.

8. On the Debug menu, click Run or press F5 to run the program in Debug mode. The program pauses at the first breakpoint.

9. On the Debug menu, click Step Over or press F10 to step through the program.

Note that after each Cardgame constructor executes, the value of totalparticipants increases and after each pointer is deleted (and the destructor is called), totalparticipants decreases. If you step to the last line of the program, just before the return statement is executed, totalparticipants equals 0.

10. Continue stepping through the program until it exits or on the Debug menu, click Run or press F5 to allow the program to continue running until it exits.

About the Author:

No further information.




Comments

No comment yet. Be the first to post a comment.