Walkthrough: Working with Projects and Solutions (C++)

This walkthrough shows how to create a project in Visual Studio, use Solution Explorer to add files to the project, enter code in the files, and then build and run the project.

In Visual Studio, work is organized in projects and solutions. A solution can contain more than one project, for example, a DLL and an executable that references that DLL. For more information, see Introduction to Solutions, Projects, and Items.

Prerequisites

To complete this walkthrough, you must understand the fundamentals of the C++ language.

Creating a Project

The first task in using Visual Studio to write a C++ program is to choose the type of project. For each project type, Visual Studio sets compiler settings and generates starter code for you. In the project in this walkthrough, you create a basic program that tracks how many players are playing different card games.

To create a project

  1. On the menu bar, choose File, New, Project.

  2. In the left pane of the New Project dialog box, expand the Installed Templates node, expand the Visual C++ node, and then select Win32.

  3. In the list of installed templates in the center pane, select Win32 Console Application.

  4. Enter a name for the project in the Name box. For this example, enter Game.

    You can accept the default location in the Location drop-down list, enter a different location, or choose the Browse button to browse to a directory where you want to save the project.

    When you create a project, Visual Studio puts the project in a solution. By default, the solution has the same name as the project. You can change the name in the Solution name box, but for this example, keep the default name.

    Choose the OK button to start the Win32 Application Wizard.

  5. On the Overview page of the Win32 Application Wizard, choose the Next button.

  6. On the Application Settings page, under Application type, select Console Application. Under Additional options, clear the Precompiled header setting, and then select the Empty Project setting. Choose the Finish button to create the project.

    You now have a project, but it does not yet have source code files.

Using Solution Explorer

You can use Solution Explorer to organize and manage the files and other resources in your solution.

In this section, you add a class to the project, and Visual Studio adds the .h and .cpp files. You then add a new source code file, for the main program that tests the class.

To add a class to a project

  1. If Solution Explorer is not displayed, on the menu bar, choose View, Solution Explorer.

  2. In Solution Explorer, open the shortcut menu for the Header Files folder, and then choose Add, Class.

    In the left pane of the Add Class dialog box, expand the Visual C++ node, and then select C++, and then in the list of installed templates in the center pane, select C++ Class. Choose the Add button.

  3. In the Generic C++ Class Wizard, enter Cardgame in the Class name box. Choose the Finish button to accept the default file names and settings.

  4. The Cardgame.h file is opened in the editor. Make these changes:

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

      int players;
      static int totalParticipants;
      
    • Modify the default constructor that Visual Studio generated. Find the line that looks like this after the public: access specifier:

          Cardgame(void); 

      Modify it to take one parameter of type int named players, as shown here.

      Cardgame(int players);
      
    • Add an inline declaration after the default destructor for a static int member function named GetParticipants that takes no parameters and returns the totalParticipants value.

      static int GetParticipants() { return totalParticipants; }
      
  5. The Cardgame.h file should resemble this after you change it:

    #pragma once
    class Cardgame
    {
        int players;
        static int totalParticipants;
    public:
        Cardgame(int players);
        ~Cardgame(void);
        static int GetParticipants() { return totalParticipants; }
    };
    

    The line #pragma once tells the compiler to include the file only one time. For more information, see once.

    For information about other C++ keywords in this header file, see class (C++), int, Static (C++), and public (C++).

  6. Choose the Cardgame.cpp tab in the editing pane to open it for editing.

  7. Delete everything in the file and replace it with this code:

    #include "Cardgame.h"
    #include <iostream>
    
    using namespace std;
    
    Cardgame::Cardgame(int players)
        : players(players)
    {
        totalParticipants += players;
        cout << players << " players have started a new game.  There are now "
             << totalParticipants << " players in total." << endl;
    }
    
    Cardgame::~Cardgame()
    {
    }
    

    You can use auto-completion when you are entering code. For example, if you were entering this code, you could enter pl or tot and then press Ctrl+Spacebar so that auto-completion finishes entering players or totalParticipants for you.

    Note

    It can take a moment for Intellisense to update your project files. A temporary red underline appears under identifiers that have not yet been updated. If a red underline persists after Intellisense updates, check the spelling or capitalization of the identifier.

    For information about #include, see #include Directive (C/C++).

Adding a Source File

Now, add a source code file for the main program that tests the class.

To add a new source file

  1. In Solution Explorer, open the shortcut menu for the Source Files folder, and then choose Add, New Item.

    In the Add New Item dialog box, in the left pane under Installed Templates, expand the Visual C++ node, and then select Code. In the center pane, select C++ File (.cpp).

  2. Enter TestGames.cpp in the Name box, and then choose the Add button.

  3. In the TestGames.cpp editing window, enter the following code.

    // TestGames.cpp
    #include "Cardgame.h"
    #include <iostream>
    
    using namespace std;
    
    int Cardgame::totalParticipants = 0;
    
    void PlayGames()
    {
        Cardgame bridge(4);
        Cardgame blackjack(8);
        Cardgame solitaire(1);
        Cardgame poker(5);
    }
    
    int main()
    {
        PlayGames();
    
        return 0;
    }
    

Building and Running a Project

Now, build and run the application project.

To build and run the project

  1. On the menu bar, choose Build, Build Solution.

    Note

    If you are using an Express edition that does not display a Build menu, on the menu bar, choose Tools, Settings, Expert Settings to enable it.

    Output from a build is displayed in the Output window. If your build is successful, the output should resemble this:

    Output

    1>------ Build started: Project: Game, Configuration: Debug Win32 ------
    

1> TestGames.cpp 1> Cardgame.cpp 1> Generating Code... 1> Game.vcxproj -> c:\users\username\documents\visual studio 2012\Projects\Game\Debug\Game.exe ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Your **Output** window can show different steps, depending on the edition and the build configuration, but the last line resembles the output shown if the project build succeeds.

If your build did not succeed, compare your code to the code that is given in the earlier steps.
  1. To run the project, on the menu bar, choose Debug, Start Without Debugging. The output should resemble this.

    4 players have started a new game.  There are now 4 players in total.
    

8 players have started a new game. There are now 12 players in total. 1 players have started a new game. There are now 13 players in total. 5 players have started a new game. There are now 18 players in total.

Next Steps

Previous: Introducing the Visual Studio IDE (C++). Next: Walkthrough: Building a Project (C++).

See Also

Tasks

Visual C++ Guided Tour

Other Resources

Managing Solutions, Projects, and Files