Search


Software Design Using C++



Using Microsoft Visual C++ 6.0



Starting the Software


Microsoft Developer Studio 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 (perhaps labeled Visual C++) on the Microsoft Office toolbar at the top of the desktop, just click on it to start it up. You can also begin Visual C++ by selecting Start, Programs, Microsoft Visual Studio 6.0, Microsoft Visual C++ 6.0.

Creating a New Workspace


The first time that you start you should probably create a new workspace. Then each time you write a program you will add its project to this one workspace. This allows Developer 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, for example, you could create a separate workspace for each course.

Select File, New. Click on the Workspaces tab. (Be sure to always check that you have selected the correct tab. Otherwise you may create some other type of new item.) Fill in a name for the new workspace. For the location use whatever is reasonable in your setting. At Saint Vincent College we recommend the use of your private M drive. Do not use a diskette, as you will quickly run out of room.

Creating a New Project


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.

To create a new project, select File, New and click on the Projects tab. Click on Win32 Console Application. 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. The location shown should be correct, but check that it is set to be a subdirectory of your workspace directory. Make sure the radio buttons are set to select Add to current workspace. Do not use Create a new workspace. The "Dependency of" check box should not be checked. Finally, click OK. At the next screen make sure that the radio button labeled An empty project is selected and click Finish. Click OK on the next screen. 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 just created a new project, that project will probably already be active. Select Project, Set Active Project to see what project is currently the active one. If it is not the one to which you want to add your C++ source file, click on the correct one.

Once you are sure that you have the correct project active, select File, New. Click on the Files tab and click on C++ Source File. Then fill in the desired file name. The .cpp extension will be added automatically; there is no need to type that. The project name and location should be correct, but glance at them to be sure. The Add to Project box should be checked and the correct project name should be shown below it. See this picture of these choices to have a better idea of what this looks like. Finally, click on OK. You should then be in the editor, 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 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.

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. Then select File, New. Click on the Files tab and click on C/C++ header file. The rest proceeds much as above. See this picture of the details for further help.

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 Rebuild All, found under the Build menu item. The Compile option would only handle a project with a single source file. Rebuild All will see that everything is recompiled, linked, etc. For a project with multiple source files, the Build option under the Build menu is smart enough only to recompile files that must be recompiled due to changes made since the last time you compiled. Thus, Build is usually faster than Rebuild All in such cases.

Error messages will normally be shown in a small Build window at the bottom of the screen. Of course, you can enlarge this window by placing the mouse on the dividing line between this window and the one above and dragging it. If this window is not shown at all, try clicking on the Output button in the middle of the toolbar. (If you unsure which button this is, place your mouse pointer over each button for a while until the descriptive label is shown.) Double clicking on an error message should take you to the spot in the program where the error occurred.

To run your program, either click on the button with the red exclamation point, or select Build, Execute. Since we are creating console applications, the programs run in a DOS window showing all input and output. (Hint: If you get an error message about WinMain, that means that you did not set up your project to be a console application. Make a new project of the correct type and add your files to it.)

Saving Your Work


You can find the save commands under the file menu. This saves the file that is currently active in the editor window. If you want to save all files that are part of your project use the Save All command under the file menu. 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. Note that there is a separate Save Workspace command under the file menu. This saves the information about what is in your workspace. If you simply close Visual C++ without explicitly saving your workspace information, it will by default ask you if you want to save this information.

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.) You only need to copy your .cpp and .h files for console applications. The workspace, project, and other associated files can be recreated if need be.

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

Returning to Your Workspace, Project, and Files


By way of example, suppose that your workspace is on your M drive. Start Visual C++ 6.0. Select File, Open Workspace. (Do not use Open as that is only for opening a single C++ file.) In the resulting dialog window, select your M drive in the "Look in:" pulldown. Then move to your workspace directory by double clicking on it. Then double click on your .dsw workspace file. This opens your workspace and places you right where you left off within it. (Note that an alternative is to use My Computer to find your .dsw workspace file and to double click on it.)

To select an existing project to work on within your workspace, click on Project and then click on Select Active Project. Click on the desired project name in the list that appears. 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 to bring it into the editor. However, it is easier to use the File View pane usually shown in a window on the left. If this window is not visible, try clicking on the Workspace button near the middle of the toolbar at the top of the screen. (This button toggles on and off the viewing of your workspace information, including the File View pane.) At the bottom of this window should be tabs labeled ClassView and FileView. Clicking on the FileView tab gives you the File View pane showing your entire workspace with all of its projects. 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. Double click on the appropriate folder and then double click on the desired file to bring it into the editor.

To close a file that you no longer want on the screen, click the lower of the two x buttons at the top right of the screen. (The upper x button closes Visual C++ itself.) 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 Build, Start Debug. This brings up the choices: Go (to execute until a breakpoint is reached), Step Into (to execute the program line by line), Run to Cursor (run to the current cursor location). If you select Step Into or Run to Cursor, you should be given a debug toolbar that has other options such as Step Over (also for executing line by line but without going inside the code for any function that is called). This toolbar also allows you to restart the program, to end debugging, etc. If the debug toolbar isn't shown, look under Tools, Customize, Toolbars and see that the debug toolbar is checked.

When the debugger is running, one or more windows may be open for displaying values. The debug toolbar has buttons for toggling on/off a watch window, an auto variable display window etc. The former lets you enter the names of variables whose values you would like to see. The latter automatically shows the values of variables used in the line just executed.

Note that to add or remove breakpoints (probably done before you even start the debugger), you can right click at the desired location in the code and then click on Insert/Remove breakpoint. Doing the same thing again removes the breakpoint. The debugger has many more features which you can learn if you like. See the Debugger Lab for a practice exercise on using the debugger.

Seeing Your Workspace Files and Classes


In an earlier section the File View of your workspace was mentioned. This is normally shown on the left of your screen. With File View you can scroll through a tree showing all of your source files, organized by project.

Clicking on the ClassView tab gives you the Class View. Even if your program does not use classes, this view can be helpful in that it shows you the names and parameter information for all of your functions. 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. The data fields for the class are also shown within Class View. Under the directory labeled Globals you will find the stand-alone functions (such as main) and global variables (if any) that are used by your project.

Another useful tool is the Wizard Bar, usually found a few items to the left of the ! button on the toolbar. It has a "magic wand" button for a default action and a downward triangle pulldown for displaying other possible class-related actions (such as go to function definition, add new class, etc.). The Wizard also uses the three windows on the rest of the line to the left of this. If you are using classes, try the pulldowns for these windows as they give a convenient way of working with classes.

Customizing a Project


Look under Project, Settings. Here you can find a lot of detailed settings. Some of these, for example, can be used to omit debugging info, to give a faster executable, etc. Although you may 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 produce a small and possibly faster executable. It does this 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 click on Build and then on Set Active Configuration. Click on the line labeled with your project name and having Debug or Release (whichever you want). Then click on OK.

Printing


Use File, Print. 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. If you want to look within all files in your project, use Edit, Find in Files.

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 use Project, Add to Project, Files. This should give a dialog box for choosing the file(s) to insert. Select the desired file(s) and click on OK.

Help


Explore the items under the help menu to see what is available. It is fairly obvious how to use these, so no more will be said here about them.

Related Items

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

Author: Br. David Carlson
Last updated: August 03, 2006