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

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

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

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

  4. On the Overview page of the Win32 Application Wizard dialog box, click 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.

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

  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. To simplify console output, add the following code immediately below #include "Cardgame.h":

    #include <iostream>
    using namespace std;
    
  8. Add the code for the constructor that takes one int argument:

    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.

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

    Cardgame::Cardgame(void);
    
  10. The Cardgame.cpp file should resemble this after your changes:

    #include "Cardgame.h"
    #include <iostream>
    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

  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+) and delete Operator (C+).

  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. If not, compare your code to the code that appears earlier in the topic.

Next Steps

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

See Also

Tasks

Visual C++ Guided Tour

Other Resources

Managing Solutions, Projects, and Files