In this guided tour you will learn about the Visual Studio
development environment and about the various types of applications
that you can create with Visual C++.
A D V E R T I S E M E N T
This includes command-line
application, Windows applications, and even a simple game. This
guided tour will also teach you how to create reusable libraries of
code, and how to ship your code to customers after you have written
and tested it.
Because each topic builds on information in the
topics before it, we recommend that you complete the guided tour in
order. At the bottom of each topic, you will find navigation links
to the next topic in the guided tour, and a link to the previous
topic if you want to go back and review something.
The guided tour assumes that you understand the fundamentals of
the C++ language.
In
This Section
Introducing the Visual Studio IDE (C++)
Describes how to use the Visual Studio IDE to create
solutions and projects, to write code efficiently, and
to build, debug, test, and deploy applications.
Creating Command-Line Applications (C++)
Introduces C and C++ command-line application, talks
about how to create an ANSI conformant C or C++ program,
and describes how to compile applications by using the
command-line compilers.
Creating Windows Applications (C++)
Describes how to create Windows API (Win32)
applications, Windows Forms applications, Windows Forms
controls, and even a simple DirectX game.
Creating Reusable Code (C++)
Describes how to create dynamic link libraries
(DLLs), static libraries, and managed assemblies so that
code can easily be reused by multiple applications.
Where to Go Next (C++)
Contains links to other sections of the
documentation where you can learn more about the topics
that are introduced in the guided tour.
Introducing the Visual Studio IDE (C++)
The Visual Studio Integrated Development Environment (IDE) offers a
set of tools that help you write and modify code, and also detect
and correct errors.
In these topics, you create a new standard C++
program and test its functionality by using features available in
Visual Studio for the C++ developer. The simple program you create
will track how many players are playing different card games.
Introducing the Visual Studio IDE (C++)
The Visual Studio Integrated Development Environment (IDE) offers a
set of tools that help you write and modify code, and also detect
and correct errors.
In these topics, you create a new standard C++
program and test its functionality by using features available in
Visual Studio for the C++ developer. The simple program you create
will track how many players are playing different card games.
Projects and Solutions (C++)
In Visual Studio, you organize 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. For more information, see
Introduction to Solutions, Projects, and Items.
Prerequisites
This topic assumes that you understand the fundamentals
of the C++ language.
Working
with Projects and Solutions
The first step in writing a Visual 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
From the File menu,
point to New, and then click
Project�.
In the Project Types
area, click Win32. Then, in
the Visual Studio installed
templates pane, click Win32
Console Application.
Type a name for the project. In this example,
we'll use game.
When
you create a new project, Visual Studio puts the
project in a solution. Accept the default name for
the solution, which is the same name as the project.
You can accept the default location, type a
different location, or browse to a directory where
you want to save the project.
Press OK to start the
Win32 Application Wizard.
On the Overview page of
the Win32 Application Wizard
dialog box, click Next.
On the Application Settings
page under Application type,
select Console Application.
Select the Empty Project
setting under Additional options
and click Finish.
You now
have a project without source code files.
Using
Solution Explorer
Solution Explorer makes it easy for 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
If the Solution Explorer
window is not visible, on the View
menu click Solution Explorer.
Right-click the Header Files
folder in Solution Explorer
and point to Add. Then click
Class.
In the
Visual C++ category, click
C++ and click
C++ Class in the
Visual Studio installed templates
area. Click Add.
In the Generic C++ Class Wizard,
type Cardgame as the
Class name and accept the
default file names and settings. Then click
Finish.
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:
Copy Code
int players;
static int totalparticipants;
Add a public constructor prototype that
takes one parameter of type
int:
Copy Code
Cardgame(int p);
Delete the default constructor generated for
you. A default constructor is a constructor that
takes no arguments. The default constructor
looks similar to the following:
Copy Code
Cardgame(void);
The Cardgame.h file should resemble this
after your changes:
Copy Code
#pragma once
class Cardgame
{
int players;
static int totalparticipants;
public:
Cardgame(int p);
~Cardgame(void);
};
The line #pragma once
indicates that the file will be included only one
time by the compiler. For more information, see
once.
For information about other C++ keywords included
in this header file, see
class (C++),
int,
Static (C++), and
public (C++).
Double-click Cardgame.cpp
in the Source Files folder
to open it for editing.
Add the code for the constructor that takes one
int argument:
Copy Code
Cardgame::Cardgame(int p)
{
players = p;
totalparticipants += p;
cout << p << " players have started a new game. There are now "
<< totalparticipants << " players in total." << endl;
}
When you begin typing pl
or to, you can press
Ctrl-Spacebar and auto-completion will finish typing
players or
totalparticipants for you.
Delete the default constructor that was
generated for you:
Copy Code
Cardgame::Cardgame(void);
The Cardgame.cpp file should resemble
this after your changes:
Copy Code
#include "Cardgame.h"
#include
using namespace std;
Cardgame::Cardgame(int p)
{
players = p;
totalparticipants += p;
cout << p << " players have started a new game. There are now "
<< totalparticipants << " players in total." << endl;
}
Cardgame::~Cardgame(void)
{
}
For an explanation of #include,
see
The #include Directive.
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
From the Project menu,
click Add New Item.
Alternatively, to use Solution Explorer to add a new
file to the project, right-click 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).
Type testgames as the
Name and click Add.
In the testgames.cpp editing window, type
the following code:
Copy Code
#include "Cardgame.h"
int Cardgame::totalparticipants = 0;
int main()
{
Cardgame *bridge = 0;
Cardgame *blackjack = 0;
Cardgame *solitaire = 0;
Cardgame *poker = 0;
bridge = new Cardgame(4);
blackjack = new Cardgame(8);
solitaire = new Cardgame(1);
delete blackjack;
delete bridge;
poker = new Cardgame(5);
delete solitaire;
delete poker;
return 0;
}
For information about C++ keywords included in
this source file, see
new Operator (C++) and
delete Operator (C++).
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. If not, compare your code
to the code that appears earlier in the topic.
Building a Project (C++)
In this step, you deliberately introduce a Visual 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.
Prerequisites
This topic assumes that you understand the fundamentals
of the C++ language.
To fix compilation errors using the IDE
In testgames.cpp, delete the semicolon in the last
line so that it resembles this:
Copy Code
return 0
On the Build menu, click
Build Solution.
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.
To view more help information about an error, highlight the
error and press the F1 key.
Add the semicolon back to the end of the line with the
syntax error:
Copy Code
return 0;
On the Build menu, click
Build Solution.
A message in the
Output window indicates that the project
compiled correctly.
Testing a Project (C++)
Running a program in Debug mode enables you to use
breakpoints to pause the program to examine the state of
variables and objects.
In this step, you watch the
value of a variable as the program runs and deduce why
the value is not what you might expect.
Prerequisites
This topic assumes that you understand the
fundamentals of the C++ language.
To run a program in Debug
mode
Click on the testgames.cpp
tab in the editing area if that file is not visible.
Set the current line in the editor by clicking
the following line:
Copy Code
solitaire = new Cardgame(1);
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.
On the Debug menu, click
Start Debugging or press F5.
When the program reaches the line with the
breakpoint, execution stops temporarily (because
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.
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
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.
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.
Right-click 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.
On the Debug menu, click
Stop Debugging orpress
Shift-F5 to stop the program.
Deploying Your Program (C++)
Now that we've created our application, the last step is
to create an installer that other users can use to
install the program on their computer. To do this, we'll
add a new project to our existing solution. The output
of this new project is a setup.exe file that will
install the application that we've previously created on
another machine.
This walkthrough uses Windows
Installer for deploying your application. You can also
use ClickOnce to deploy an application. For more
information, see
ClickOnce Deployment. For more information on
deployment in general, see
Deploying Applications and Components.
Prerequisites
This topic assumes that you understand the
fundamentals of the C++ language.
To create a setup project
and install your program
From the File menu,
click Add, and then click
New Project....
The
Add New Project dialog box
appears.
From the Project types:
pane, expand the Other Project
Types node and select Setup
and Deployment.
From the Templates pane,
select Setup Wizard. Type a
name for the setup project, such as
gameInstaller, and click
the OK button.
The Setup Wizard will
appear. Click Next to
continue.
From the Choose a project type
pane of the wizard, select the
Create a setup for a Windows application
option and click Next to
continue.
From the Choose project outputs
to include pane of the wizard, select
Primary Output from game,
and click Next to continue.
We do not need to include any additional files
in our installer, so from the
Choose files to include pane of the
installer, click Next.
Review the changes from the wizard, and verify
that everything is correct. Click
Finish to create the project.
The new
gameInstaller project
will be listed in the Solution
Explorer. This project will list the
dependencies that your application depends on (such
as the C Runtime Library or the .NET Framework) as
well as the project files that are to be included in
the installer.
There are many options that can be changed after
the setup project has been created. For more
information, see
Windows Installer Deployment.
Build the installer by selecting it in the
Solution Explorer and
clicking Build gameInstaller
from the Build menu.
Locate the setup.exe and gameInstaller.msi
programs created by the previous section. Double
click on either file to install the application on a
computer.