CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Using Microsoft Visual C++ .NET



Starting the Software


Microsoft Visual Studio .NET is used as a common programming environment for Visual C++ and a number of other Microsoft development tools. If you have an icon for it on the desktop or on the Microsoft Office toolbar at the top of the desktop, just click on it to start it up. Otherwise you can probably find it under Start, Programs, Microsoft Visual Studio .NET, Microsoft Visual Studio .NET.

When you start up Visual Studio you will probably be given a Start Page from which you can open previous projects if you wish to do so. If not, just close this Start Page by clicking on the x in the upper right corner.

Creating a New Solution (Workspace)


A "Solution" is used to group several projects together. (In Visual Studio 6.0 this was called a workspace instead of a solution.) The first time that you start Visual Studio you should probably create a new workspace. For example, you could create a Solution named CS111 and use it to hold all of the projects (programs) that you write for the CS111 course. Each time you write a program you will add its project to this solution. This allows Visual Studio to show you a list of all of your projects and to easily select among them. If you plan to create a large number of projects for different courses, you could create a separate solution for each course.

Select File, New, Blank Solution. Click on the Blank Solution template. Fill in a name for the new solution, such as CS111. For the location use whatever is reasonable in your setting. At Saint Vincent College we recommend the use of your private M drive, specified as M:\. Do not use a diskette, as you will quickly run out of room (perhaps before even completing one program). Click on OK.

Creating a New Project


This is typically the next step. Note that a project groups together all of the files associated with one of your programs and stores them in the project's directory. Especially when you start creating programs with the source code stored in more than one file it will be important to group things into projects. However, even for a program with a single C++ source file, it helps a lot to use a project to keep your code separate from the files for other programs.

Select File, New, Project. Click on the Visual C++ Projects folder. Several templates are shown. Click on the one that says Win32 Project. (We will use this template nearly everywhere in these web pages. Only in the AppWizard section will we use another template, the one labeled MFC Application.) Be very careful to select the correct template as otherwise the examples in these web pages will not work. Fill in a project name, which can be the same name as the main source file (without the .cpp). For example, hw4 would be a reasonable project name for your 4th homework project. Make sure that the Add to Solution button is selected. (If not, your project won't be grouped under your solution workspace.) The location shown should be correct, but check that it is set to be a subdirectory of your solution's directory. Finally, click OK. In the next window, click on Application Settings. Be certain to set the Application type to Console Application and the Additional options to Empty Project. We will use these settings for all of our projects except for a few where you will be told otherwise. Click on Finish. You now have a new, empty project.

Creating a New C++ Source File


In your project you will want to have at least one C++ source file containing the code for your program. If you have more than one project in your solution (workspace), you need to be sure that the new one is the active project. The so-called Solution Explorer should be visible on your screen. (If not, use View, Solution Explorer.) Right click on the name of your project in the Solution Explorer. Then select Set as Startup Project to be sure that this is the active project, the one that you want to work on.

Once you are sure that you have the correct project active, right click on the project name in the Solution Explorer. Select Add, Add New Item. Be sure that the Visual C++ folder is selected. Click on the C++ File Template, fill in a name for your file (such as hw4.cpp), and click on Open. (The location should be fine as is.) You should then have an editor on screen, editing a blank .cpp document with the correct name. Type in your desired code. Select File, Save to save your file. You can also copy in code from another file by selecting Edit, Insert File as Text. It is also possible to add code to a new file by using Cut, Copy, and Paste (found under Edit) to bring in code from another file.

Sometimes you will want to use more than one C++ source file. It is typical to place the main function in one source file, other stand-alone functions in another source file, and functions belonging to a user-defined class in their own source file. All of these files that belong to the same program must be added to the same project.

Hint: Make sure that all of your project's .cpp and .h files (and any data files as well) are all located under your project directory. You can use My Computer to check on the locations of these files. Many students run into problems with their projects because they have several versions of their files in various directories and don't know which ones are being used by their project. Another method is to click on each file name in the Solution Explorer and look in the Properties window to see that the full path field contains something reasonable. Again, all of your .cpp and .h files for a particular project should be in the exact same directory.

Creating a New C++ Header File


A header file is a file with a .h extension. It is typically used to contain constants, types, class declarations, function prototypes, etc. Executable code is usually not placed in header files. To add a new header file to a project, one follows almost the same steps as in creating a C++ source file. Begin by checking that the correct project is active. Note that the Solution Explorer shows the name of the active project in bold. Then right click on the name of this project in Solution Explorer. Select Add, Add New Item and then select the Header File template. Fill in a name for the new file (such as hw4.h). The location should be fine. Click on Open. You should now have the new, empty file in the editor.

Compiling and Running a Program


Once you have created a project and its associated C++ source and header file(s), you are ready to compile. Actually, the best choice is often to "build" your project. For our example hw4 project, this would be done by selecting Build, Build hw4. You do not want to use the Build Solution choice as it would compile every project that is in your solution, which would normally be a waste of time.

For a project with multiple source files, the Build option is smart enough to only recompile files that must be recompiled due to changes made since the last time you compiled. Thus, Build (project name) is usually faster than Rebuild (project name) since the latter blindly recompiles everything in the currently active project.

General messages from the compiler are placed in an Output window that can probably be found at the bottom of the screen. Error messages go into a Task List window. Of course, you can enlarge either of these windows by placing the mouse on the dividing line between the window and the one above and dragging it. If either window is not shown at all, try selecting View, Other Windows and then selecting the missing window. Note that double clicking on an error message conveniently takes you to the approximate spot in the program where the error occurred.

To run your program, select Debug, Start Without Debugging. The program will run in a DOS window that shows all input and output.

Saving Your Work


You can find the save commands under the file menu. If you are editing a .cpp file, there should be a selection for saving it. If you first click on the project name in the Solution Explorer, the file menu will show a selection for saving the entire project. The safest option may well be Save All. Save often so that you do not lose much of your work should a computer lock up or the power fail. At Saint Vincent College we suggest that you save your files on your personal M drive.

You might wish to copy your files to another drive in case the originals are somehow damaged. You can use My Computer to copy the desired files. (For example, use edit, copy and then edit, paste.)

On a homework assignment it is important not to leave your .cpp and .h files behind on a public drive where others can see your work. Files saved on your M drive are pretty safe from the view of others.

Returning to Your Solution, Project, and Files


By way of example, suppose that your workspace is on your M drive. Start Visual C++ .NET. If the Start Window shows your desired solution, you can click on it. Otherwise, close the Start Window and select File, Open Solution. In the dialog window, select your M drive in the "Look in:" pulldown. Then move to your solution directory by double clicking on it. Then double click on your .sln solution file. (For example, if your solution was named CS111, the solution file will be named CS111.sln.) This opens your solution and places you right where you left off within it. (Note that an alternative is to use My Computer to find your .sln solution file and to double click on it.)

To select an existing project to work one, click on the project name within the Solution Explorer. Then select Set as Startup Project. You can, of course, create a new project if you wish to do that instead. (You start with File, New, Project. Remember?)

If the file that you want to work on in the editor is not already visible, you can use File, Open, File to bring it into the editor. However, it is easier to use the Solution Explorer. If this window is not visible, try View, Solution Explorer. The Solution Explorer shows all of your projects by name. Look for your current project. It should be listed in bold to show that it is the current active project. If there is a little box with a + in front of the line for your project, click on it to expand what is inside of this project. Inside you will probably find three folders, labeled Source Files, Header Files, and Resource Files. Your .cpp files will be in the Source Files folder. Your .h files will be in the Header Files folder. In most of these web pages we will not use the Resources Files folder. Expand the desired folder and then double click on the file that you want in order to bring it into the editor.

To close a file that you no longer want on the screen, click x button at the top right of the editor window. You may have several files in the editor and want to close them all before opening another one.

Debugging a Program


There are many ways to debug a program. One of the simplest is to use cout to output the values of key variables at various places in the program. Another is to use the debugger. You can access it by selecting Debug. This gives various choices, with the simplest being Step Into. This will start the debugger at the beginning of your program and wait for you to indicate what to do next. (It may take a few seconds for this to happen.) The DOS box for input and output will probably automatically be minimized for you, but if need be you can get it out of the way by minimizing it yourself (by clicking on the _ button in its right corner).

The Debug toolbar should now be visible. If for some reason the Debug toolbar cannot be seen, use Tools, Customize, Toolbars and see that the Debug toolbar is checked. Some of the most useful buttons on the Debug toolbar are a rightward blue arrow (to continue the program to the end), a blue square (to stop debugging), a backward arrow (to restart debugging from the beginning of the program), a Step Into button, a Step Over button, and a Step Out button. You can put the mouse over the top of a button to see the text description of the button.

The Step Over button is used to execute one instruction and move the yellow location arrow to the next instruction. It is very useful for executing one instruction at a time. The Step Into button is similar, but will move you inside the code for any function call, where you could repeatedly use Step Over until you eventually get out of that function or you could use the Step Out button to get out of that function. The Continue button will run the program until the end or until a breakpoint is reached. You will read how to set breakpoints in an upcoming paragraph.

When the debugger is running, one or more windows will typically be open for displaying values. Usually you use a window called Autos which automatically displays the values of variables currently in use. You may also use windows called Locals and Watch. With a Watch window you can click on a blank entry under the Name column and type in the name of a variable whose value you would like to watch. You can remove a variable from a Watch window by right clicking on it and selecting Delete Watch. These windows are very useful in seeing the current values of your variables as you move through your program using the Step Over (or similar) buttons. If the desired Autos, Locals, or Watch windows are not visible when you have the debugger running, you can turn them on by selecting Debug, Windows, and then the desired window.

If you want to look at the values in an array, you can put something like DataArray[k] into a field of a Watch window. This shows you the number in the array at the index given by the current value of k. If you want to look at the fields on an object, the Autos window will probably show the object. Click on the + in front of the object name so as to display its fields if they are not already visible. If one of the fields is a pointer to another object, you should be able to click on the + in front of the field in order to see the object it points at. This is useful in looking at the data in a linked list, for example.

Note that to add or remove breakpoints (either before or after starting the debugger) you can right click at the desired location in the code and then click on Insert Breakpoint or Remove Breakpoint, as desired. If you want to run your program to a specific point and then look at the values of the variables, set a breakpoint at this location and then use the Continue button to run to the breakpoint. (An alternate method is to right click on the desired location in the code and select Run to Cursor.) The debugger has many more features which you can learn if you like. For example, you can set breakpoints that stop execution if some condition is true. You can view a simple representation of the stack used for function calls. And there is a lot more. See the Debugger Lab for a beginning practice exercise on using the debugger.

Seeing Your Workspace Files and Classes


In an earlier section the Solution Explorer, which lets you see a tree-like listing of your projects and their files, was explained. In a similar way, the Class View window lets you see any classes that you have set up in your project, along with their fields (variables) and methods (functions). Even if your program does not use classes, Class View can be helpful in that it shows you the names and parameter lists for all of your functions under Global Functions and Variables. As usual, if there is a box with a + in front of your project, click on the + to expand things to show the classes inside of your project. Then click on the + in front of a class name to see a list of the functions in that class. Double clicking on the class name will take you to the class declaration. Double clicking on a class function name will take you to the code for that function (its definition). You can also right click on a function, class, or variable to be given a context-sensitive menu that includes selections for going to these items in the code.

Another useful thing that you can do in Class View is to right click on a class name and then select Add. You are then given a choice of adding a function (method) or variable (field) to this class. You can also right click on your project name in Class View, select Add, and use this to add a new class to your project. We will not go into the details of these two items here, but you might want to try them on your own.

Customizing a Project


In the Solution Explorer you can right click on a project, select properties, and then see all of the properties for this project. This includes compiler and linker options that are in use for your project. Although you can change these settings, do so at your own risk. The default settings will almost always be appropriate.

By default, Visual C++ uses a Debug configuration when compiling your project. Once you have your project working well, you may wish to switch to a Release configuration. This will typically produce a faster and smaller executable. It does this in part by leaving out information needed to use the debugger. Thus a Release configuration is only used when you are done with debugging. To change the configuration between Debug and Release use Build, Configuration Manager and then click on the little triangle pull down for the box labeled Active Solution Configuration. This will let you select the Debug or Release configuration. (There is also a grid of information for all of your projects. Here you can change the desired configuration for any of these projects.)

Printing


If you have a file in the editor, use File, Print to print it. Check that the pull-down has the desired printer selected. Then click on OK.

Finding Some Text


If you need to find some particular text (perhaps a variable name) within the file shown in your editor window, use Edit, Find and Replace, Find. Each time that you click on Find Next it will go to the next line of code in the editor that has the desired text in it.

If you want to look within all files in your project, use Edit, Find and Replace, Find in Files. It will show all of the matches it found in a Find Results window. You can click on any line of the results to go to that line of code in the editor.

Adding Files to a Project


If you already have some .cpp and/or .h files that you would like to add to an existing project, first use My Computer to copy these files into the project folder. Then click on your project in the Solution Explorer. Select Add, Add Existing Item. You are then given a dialog box where you can select the desired file(s). Note that inside your project folder you will see a subfolder named Debug and perhaps a subfolder named Release. Inside the Debug folder is where the executable file (such as hw4.exe) is located. If you created a release version of your program, its compiled executable is inside the Release folder. This is the file that you would distribute if you were making your program available for others to use.

Help


Explore the items under the help menu to see what is available. The most commonly used item is Search. Once you select this, put the name of the item that you wish to search for in the Look for: box. Use the pull down triangle in the second box (labeled Filtered by:) to limit the scope of the search to something reasonable such as "Visual C++" or perhaps "Visual Studio". Otherwise you may get so many matches as a result of your search that you won't be able to wade through them all.

Related Items

Back to the main page for Software Design Using C++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: August 27, 2009
Disclaimer