The Obligatory Hello, World Program


The Obligatory "Hello, World!" Program

It seems like all the programming books I read always start with a "Hello, World!" program. Who am I to do things differently? Here is the "Hello, World!" program, Managed C++ style:

 // Obligatory Hello World! #using <mscorlib.dll> using namespace System; Int32 main(void) {     Console::WriteLine(S"Hello Managed World");     return 0; } 

You can create the Hello.cpp program by typing it in with any text editor. You can use Edit or Notepad, as both come with all versions of Windows. To compile it into an assembly called Hello.exe, simply execute the following line from the command prompt:

 cl Hello.cpp /CLR 

Note

You need the command prompt to be configured for the .NET development environment. Unless you have configured your default command prompt for this environment, I recommend that you use the command prompt provided by Visual Studio .NET.

Even though this is an assembly, you run it like you would any other executable. If you run it from the command line, you should get something like Figure 2-1.

click to expand
Figure 2-1: Executing Hello.exe from the command line

All Managed C++ programs will contain this line:

 #using <mscorlib.dll> 

#using is a preprocessor directive specific to Managed C++. When compiled, it generates metadata that is used by the common language runtime (CLR) to identify which assemblies to load. Those of you existing C++ programmers can think of this directive as being similar to the #include directive, except that instead of including an .h file, you are now including a compiled .dll assembly file. In the case of Hello.cpp, you are only using mscorlib.dll, which is an assembly that contains most of the core elements of the .NET Framework.

I don't cover namespaces until later in this chapter, but for now think of them as a way of assembling a bunch of functions and variables into a group. When you want to access this group, you use the using statement. Basically, the next line

 using namespace System; 

says you are going to use the stuff in this System namespace or grouping.

Every Managed C++ program must start with a main() function and every program can have only one main() function. When the main() function finishes executing, so does the program. In the case of Hello.cpp, main() also happens to be the only function. The first line of the main() function is this:

 Int32 main(void) 

There are other variations of main(), including the WinMain() function used to start Windows programs. I cover those other variations later in this chapter. In the preceding variation of main(), you are receiving no parameters, which is signified by the (void) placed after the main, and you are expecting the function to return a 32-bit integer number value, Int32, after it is completed.

A function is a block of code referenced by name, in this case main. It starts with an open curly bracket ({) and ends with a closed curly bracket (}). Within a function is the set of statements that it will execute. The main() function of Hello.cpp contains two statements:

 System::Console::WriteLine(S"Hello Managed World"); return 0; 

If more than one statement were present, as shown in the preceding code, the statements would be executed sequentially from beginning to end, unless a statement specifically altered the flow, either by looping back or by conditionally bypassing some of the code. You will see how this is done later in this chapter.

In Managed C++, displaying text strings, which are enclosed in quotation marks ("") and prefixed with an S, to a console window is handled using the static WriteLine() method of the class Console in the namespace System. Don't panic if that didn't mean much to you—it will shortly. You will learn about classes and static methods in Chapter 3. You will also examine text strings and namespaces in Chapter 3. For now, all you need to know about displaying your own text is to replace "Hello Managed World" with whatever you want to replace it with.

The final line

 return 0; 

simply tells the function main() that it is done and that it should return with a value of 0 back to the operating system.




Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net