< Building and Running C++ .Net 2003 | Index #1 | Index #2 | .Net Framework >


 

 

Step-by-step On Using Visual C++ 2005

Building C++ CLR Console Based Application

 

 

 

 

 

Note: The code used is new C++ .Net syntax.

 

 

1.        Start Microsoft Visual Studio .NET 2003 and create a new CLR Console Application project named Structs. Select File menu Project... submenu. Type in the project name in the Name: field. Adjust the project location in Location: field if needed. Leave the Solution name as given.

 

Invoking the new project page

 

Figure 1: Invoking the new project page

 

Creating a new CLR Console Application project

 

Figure 2: Creating a new CLR Console Application project

 

2.        Open the Structs.cpp, the main project file. At the top of the Structs.cpp file, immediately under using namespace System;, add the following structure definition:

// The Point structure definition

value struct Point

{

    public:

        int x, y;

};

Files listing seen in the Solution Explorer

 

Figure 3: Files listing seen in the Solution Explorer

 

Adding a Point structure to the main project file

 

Figure 4: Adding a Point structure to the main project file

 

The value and struct keywords start a structure definition, and you’ll notice that structures look very similar to classes in the way they are defined. The body of the structure is enclosed in braces and finishes with a semicolon, and the public and private keywords are used to set the access level for structure members. Notice the use of the value keyword here (without double underscore). This keyword tells the compiler that this is a value type and not a traditional C++ structure. It’s important that you remember to use value when defining your structures. This simple structure represents a point on a graph, so it has two integer data members representing the x and y coordinates.

 

3.        To create and initialize a Point object, add the following lines to the main function of your application, replacing the Console::WriteLine(L"Hello World");. Notice the main instead of _tmain used in VC++ 2005. _tmain will compiled to wide character (wmain) or ANSI (main) and it is visible in VC++ 2003 only because in VC++ 2005 the main is default to wide character or Unicode.

// Create a Point

Point p1;  // use the default constructor

 

// Initialize its members

p1.x = 10;

p1.y = 20;

Creating and initializing the Point object

 

Figure 5: Creating and initializing the Point object

 

Notice that the code doesn’t use the gcnew operator. The gcnew operator is used to create references to objects, and value types aren’t accessed by reference. Instead, a Point has been created on the program stack, and you access it directly as p1. Because the data members are public at this point, you can access them using the familiar dot notation.

 

4.        Add two lines to print out the value of one of the struct members, like this:

Console::Write(L"p1.x is ");

Console::WriteLine(p1.x);

Adding codes do display to the standard output

 

Figure 6: Adding codes do display to the standard output

 

5.        Compile and run the program at this point, you should see the output p1.x is 10. The steps to compile and run your program are shown below.

 

Building the solution

 

Figure 7: Building the solution

 

 

 

 

 

 

 

Running the solution without debugging

 

Figure 8: Running the solution without debugging

 

The console program output

 

Figure 9: The console program output

 

6.        Add more functionality. Add the following line immediately after the public declaration in your Point structure definition.

Point(int xVal, int yVal) { x = xVal; y = yVal; }

Adding default constructor explicitly

 

Figure 10: Adding default constructor explicitly

 

A constructor that takes no arguments is called a default constructor. In C++ .Net 2005, the default constructor for struct is invoked automatically and value types cannot contain user-defined special member functions such as default constructor, so we don’t have Point()/Point(void) here.  In this case the user defined constructor takes two int values and uses them to initialize the x and y data members on the heap. In this case, the arguments are simply being copied into the data members, but it would be simple to add some checking to ensure that the data passed in is correct.

Anyone who has used C++ before will be familiar with the use of default arguments on constructors. You can’t use default arguments on managed types in Visual C++ (for Visual C++ .Net 2003, there is no error if you provide the default constructor, Point() but in Visual C++ .Net 2005, it will generate error), so you need to provide an explicit default constructor.

 

7.        You can now add extra code to your main function to create and initialized Points. Edit the main by entering the following codes.

Point p1;  // use the default constructor

Point p2(10, 20);    // use the second constructor to set x to 10 and y to 20

Creating and initializing another Point object

 

Figure 11: Creating and initializing another Point object

 

8.        Add the following code to see the effect. Notice the using of the string modifier "L" instead of "S" as used in the old C++ syntax. L is for Unicode (optionally used in the new C++ .Net) and the S is for managed string (only used in old C++ .Net).

Console::Write(L"p1.y is ");

Console::WriteLine(p1.y);

 

Console::Write(L"p2.x is ");

Console::WriteLine(p2.x);

Console::Write(L"p2.y is ");

Console::WriteLine(p2.y);

Adding codes to display to the standard output

 

Figure 12: Adding codes to display to the standard output

 

9.        Finally, compile and run your program and the following output should be expected.

 

The console application program output

 

Figure 13: The console application program output

 

 

 

Part 1 | Part 2


 

< Building and Running C++ .Net 2003 | Index #1 | Index #2 | .Net Framework >