Close this window

Getting Started with Visual C++i

Visual C++ is now the key tool in the Visual Studio family for developing unmanaged code. Certain applications such as system utilities need to work outside of the .NET framework and Visual C++ provides for such development. If you want to learn about using .NET in Visual C++, please see the Getting Started with Visual C++.NET guide instead.

General Description

This sample application demonstrates how to build a simple C++ project that implements the QuickSort algorithm. It covers the basic components of a C++ program, reading/writing to the console and files, creating functions, and using basic arrays.

The Getting Started guides are not meant to cover all aspects of the programming language. They are meant to give you a starting point for exploring the language. You are encouraged to follow along as the tutorial covers the different pieces of the QuickSort application. The complete source code and project files are also available.

Suggested Requirements

Visual Studio.NET (Beta 2 or later) is required to compile the sample application. Knowledge of C/C++ is helpful but not required. Step 1. Starting Projects

Development in Visual Studio is organized around solutions, which contain one or more projects. For this tutorial, we will create a solution with a single C++ project.

Creating a New Project

1. In the Visual Studio.NET environment, select File | New | Project from the menu.

2. Select Visual C++ on the left and then Win32 Application on the right.

3. Specify the name of your project and enter the location in which to create the project. Visual Studio will create the project directory automatically. Then click OK.

4. In the Win32 Application Wizard, click on the Application Settings tab.

5. Select Console application under Application type and check the Empty project option. 6. Click Finish and you're on your way!

Creating a Source File

1. Select Project | Add New Item from the menu to create a new C++ source file in your empty Visual C++ project.

2. Select Visual C++ under Categories and C++ File (.cpp) under Templates.

3. Specify the name of new source file (quicksort.cpp) and click Open.

Your Visual C++ Solution

Visual Studio.NET has created a solution with one simple Visual C++ project. Your project now contains a single source file: quicksort.cpp.

The next few steps discuss these different files and how to compile the project. Step 2. Hello, World!

Let's do the classic "Hello, World!" application that was first written in the C language.

Source Code Modifications

1. Double-click the C++ source file 'quicksort.cpp' in the Solution Explorer if the source code file is not already open.

2. Insert the following code into your empty C++ source file.

// Include files #include

// Application initialization int main () { printf ("Hello, World!\n"); return 0; }

3. Notice that as you type, Visual Studio will help you with the names of classes and functions, since the .NET Framework publishes the type information.

Compiling Your Application

1. Now that you have made your modifications, you can compile the Visual C++ project by simplying selecting Build in the Build menu.

2. Errors and messages from the C++ compiler will be displayed in the Output window. If there were no errors, you can run the Hello World application by clicking Start without Debugging under the Debug menu. Program Output

This is a screenshot of the output from the Hello World sample application when run from within the Visual Studio environment.

Understanding the Changes

Control passes to the main() function after the program has been loaded by the operating system. This is why we insert the print command in the procedure. The printf() function defined in the header file prints the string passed to it. Step 3. Program Structure

Now that we have built a simple Hello World application, let us stop and examine the basic components of a Visual C++ application.

Source Code Comments

The characters // marks the rest of the line as a comment so it is ignored by the C++ compiler. In addition, code surrounded by /* and */ are also treated as comments.

// This line is ignored by the compiler. /* This block of text is also ignored by the Visual C++ compiler. */

The Include Directive

C++ header files generally contain definitions for functions and classes. A standard library of functions is provided with the Visual C++ compiler for basic operations such as string manipulation, memory allocation, and input/output. You must include the appropriate header file before using a standard library function.

In the case of the printf() function, it is defined in the header file and that is why it was included in the Hello World application.

The main() Function

The main() function receives control after the application is loaded into memory, so your application startup code should be placed in this function.

back to top Step 4. Using Debugger

The debugger is an essential tool for diagnosing problems with your programs. We felt that it was important enough to cover in this Getting Started guide. This last step will show you how to walk through your program and use features such as QuickWatch.

Setting Breakpoints

When a program is running in the debugger, a breakpoint will halt execution and give the developer control of the debugger. To set a breakpoint, right-click the line you want the program to halt at and click Insert Breakpoint as shown below.

Lines with a breakpoint are highlighted in red. You can remove a breakpoint by right- clicking the line again and selecting Remove Breakpoint.

Stepping Through a Program

Now with a breakpoint set (preferably on the line shown above), let us run the program in the debugger. In the Debug menu, select Start instead of Start Without Debugging as we did earlier. This starts your program in the debugger, thus breakpoints will enabled.

Once the program hits your breakpoint, the debugger receives control of the program. There will be an arrow pointing to the current line of execution.

To step through one line of code, select Debug | Step Over and watch the cursor move to the next line. The Debug | Step Into command allows you to step into a function that is about to be called. This is what the screen looks like after Step Over two times.

If you want the program to resume execution until it hits another breakpoint, encounters an exception, or exits, select Debug | Continue from the menu.

Inspecting Variable Values

While you have control of the debugger, you can move your mouse cursor over a variable to obtain its basic value.

Other Debugger Tools

The Visual Studio debugger features a number of other tools such as a Call Stack viewer, which you can use to see what functions have been called so far. You can also obtain memory dumps and information on the threads in the process. We encourage you to make use of these very powerful debugging tools.

back to top Conclusion & Further Reading

The intent of this Getting Started guide was to help you build a simple C++ project in Visual Studio. In no way is it comprehensive. We encourage you to seek other resources on C++ and to try out the new .NET platform using C++ with managed extensions. Having finished this tutorial, at least you have a working project and can start modifying this code as you explore Visual C++.

We have provided the complete source code and project files for your convenience. They can be accessed via the table of contents at the top of this document.

Further Reading

We highly recommend these books on C++ and the .NET platform. They are good resources for developers trying to learn these new technologies.

Deitel, Harvey. C++: How to Program, 3rd ed. Upper Saddle River, NJ: Prentice Hall, 2001.

Gunnerson, Eric. A Programmer's Introduction to C#. New York: Apress, 2000.

back to top

©2001 Microsoft Corporation. All rights reserved. Terms of Use | Privacy Statement | Accessibility i Excerpted from http://www.msdnaa.net/technologies/Getting Started/visualc/pfv.asp