Friday, June 18, 2010

OO Hello World - C++/CLI



The C++/CLI version of the Hello World program is here!
This is the third language I show you using the OO Hello World program. The first two were C# and VB.NET, both are the most widely used programming languages targeting the .NET CLR and both are meant to be used to create all types of High Level applications. C++/CLI used in conjunction with the Visual C++ compiler is the most powerful compiler done by Microsoft. You can build either High and Low level applications using several provided native and CLR libraries. You can even combine managed and native code in the same program. All those features make C++/CLI unique and the language of choice if high performance and native interoperability are part of the requirements.

You can go directly to the C++/CLI Info section to get some extra info about the language.
The programs below use only managed code.


By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html
where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.
#include "stdafx.h"
using namespace System;

ref class Greet {
 String ^name;
public:
 Greet(String ^name) {
  this->name = name[0].ToString()->ToUpper() + name->Substring(1, name->Length - 1);
 }
 void Salute() {
  Console::WriteLine("Hello {0}!", name);
 }
};

// Greet the world!
int main(array<System::String ^> ^args)
{
    Greet ^g = gcnew Greet("world");
    g->Salute();
}

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
#include "stdafx.h"
using namespace System;

namespace GreetProgram {
 private ref class Greet {
 private:
  String ^name;
 public:
  Greet(String ^name) {
   this->name = name[0].ToString()->ToUpper() + name->Substring(1, name->Length - 1);
  }
  void Salute() {
   Console::WriteLine("Hello {0}!", this->name);
  }
 };
}

// Greet the world!
int main(array<System::String ^> ^args)
{
    GreetProgram::Greet ^g = gcnew GreetProgram::Greet("world");
    g->Salute(); 
    return 0;
}

The Program Output:



 





C++/CLI Info:
“C++/CLI (Common Language Infrastructure) is Microsoft's language specification intended to supersede Managed Extensions for C++.” Taken from (http://en.wikipedia.org/wiki/C%2B%2B/CLI)
“Visual C++ provides a powerful and flexible development environment for creating Microsoft Windows–based and Microsoft .NET–based applications”. That targets the CLR or Native code.
“The Visual C++ Libraries include the industry-standard Active Template Library (ATL), the Microsoft Foundation Class (MFC) libraries, and standard libraries such as the Standard C++ Library, and the C RunTime Library (CRT)”
“Visual C++ is, in 2007, the best compiler product that Microsoft has shipped.” Taken from (http://msdn.microsoft.com/en-US/library/60k1461a(v=VS.80).aspx)

Appeared:
2005
Current Version:
Developed by:
Microsoft
Creator:
Herb Sutter, Stanley B. Lippman
Influenced by:
C++/C#
Predecessor Language
Managed C++ | C++
Predecessor Appeared
1991
Predecessor Creator
Herb Sutter, Stanley B. Lippman | Bjarne Stroustrup
Runtime Target:
CLR
Latest Framework Target:
4.0
Mono Target:
No
Allows Unmanaged Code:
Yes
Source Code Extension:
“.cpp”, “.h”
Keywords:
84
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
No
Standard:
ECMA-372
Latest IDE Support:
Visual Studio 2010 Express
Language Reference:
Extra Info:


No comments:

Post a Comment