Export (0) Print
Expand All

Walkthrough: Using the Visual Studio IDE 

Visual Studio 2005

The Visual Studio Integrated Development Environment (IDE) offers a set of tools that help you write and modify the code for your programs, and also detect and correct errors in your programs.

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

This walkthrough covers the following:

  • Working with Projects and Solutions

  • Using Solution Explorer

  • Adding a Source File

  • Fixing Compilation Errors

  • Testing a Program

  • Debugging a Program

This topic assumes you understand the fundamentals of the C++ language.

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. For more information, see Introduction to Solutions, Projects, and Items.

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

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

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

  3. Type game as the project name.

    When you create a new project, Visual Studio puts 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 a different location, or browse to a directory where you want to save the project.

    Press OK to start the Win32 Application Wizard.

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

  5. 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.

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

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

  2. 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.

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

  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. A default constructor is a constructor that takes no arguments. The default constructor looks similar to the following:

      Cardgame(void);
      
  5. The Cardgame.h file should resemble this after your changes:

    #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++).

  6. Double-click 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 resemble this after your changes:

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

    For an explanation of #include, see The #include Directive.

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 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).

  2. Type testgames as the Name and click Add.

  3. In the testgames.cpp editing window, type the following 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++), delete Operator (C++), The if-else Statement, and The try, catch, and throw Statements.

  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.

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.

To fix compilation errors using the IDE

  1. In testgames.cpp, delete the semicolon in the last line so that it resembles 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.

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.

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 the following line:

    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 (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.

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

  8. 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.

  9. On the Debug menu, click Stop Debugging or press Shift-F5 to stop the program.

In this step, you modify the program to fix the problem that was discovered above.

To fix a program that has a bug

  1. To see what occurs 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. Expand the game project tree and click the Cardgame class.

    The area underneath shows the class members and methods.

  4. Right-click 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 resemble this after your changes:

    #include "Cardgame.h"
    
    Cardgame::Cardgame(int p)
    {
        players = p;
        totalparticipants += p;
    }
    Cardgame::~Cardgame(void)
    {
        totalparticipants -= players;
    }
    
  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 to run until it exits.

Community Additions

ADD
Show:
© 2014 Microsoft