Tuesday, May 29, 2007

Understanding VC++.NET -1st Installment

1. C++ is a strongly typed language.
2. It is an efficient language.
3. It is an object-oriented language.
4. It is based on C.
5. Is case sensitive.
6. Golden rule of C++ is everything must be declared before being used.
7. With Visual C++ Express Edition, you can create Visual C++ .NET projects, including Windows Forms applications. You can also create C and C++ programs that conform to ANSI and ISO standards.

HelloWorld program in VC++.NET

// HelloWorld.cpp : main project file.

#include "stdafx.h" //copy in the file named "stdafx.h" at the begining of the program

using namespace System;

int main(array ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}

Native C++ Program from the Command Line


1. Open the Visual Studio 2005 Command Prompt window.
2. At the command prompt, type notepad .cpp and press Enter.
3. In notepad type
#include

int main()
{
std::cout<<"This is a native c++ program." <<
C++ Program with .NET Classes with Command Prompt Compilation

1. Open the Visual Studio 2005 Command Prompt window.
2. At the command prompt, type notepad .cpp and press Enter.
3. In notepad type

int main ()
{
System: Console::WriteLine ("Hi, How are you”)
}

4. On the File menu, click Save. You have created a C++ source file.
5. On the File menu, click Exit to close Notepad.
6. At the command line prompt, type cl /Clr .cpp and press Enter.
(EH is the error handler routine.)
7. To see a list of all files in the directory named simple with any extension, type dir .* and press Enter.
8. To run the .exe program, type and press Enter.
9. The program displays this text and exits.

Note: The .Net class used here is Console from the namespace “System”.

Program 1: Creating a Text File Using Visual Studio and VC++.NET


1. Create a new VS 2005 project in VC++

2. Select CLRàSelect CLR empty Project.
3. Enter the solution name and location and click OK.
4. Right click the source folder and click on “Add Item”
5. In “Add Item”, select C++ File.cpp.
6. And write the following code:
using namespace System;
using namespace System::IO ;

int main()
{
String^ FileName = "TextFile.txt";

StreamWriter^ sw = gcnew StreamWriter(FileName);

//gcnew is used instead of new in c#
//^ is equivalent to a pointer as * in c#

sw->WriteLine("Hi! Archita!");
sw->WriteLine("You are creating a text file");
sw->WriteLine("To use it for write or writeline purpose");
sw->WriteLine("You can use this for {0} also" ,"formated output");
sw->WriteLine(DateTime::Now);
sw->Close();
Console::WriteLine(" You are using the file {0}to write",FileName);

return 0;

}
Note: The thing which I found most difficult in this exercise was to get the name of classes, properties and methods using “::” being a c# programmer I have got very comfortable with using “.” …I hope to get acquainted with “::” soon. J


7. Now build the solution using f7 key. Once the project is built now you can start the program by choosing “Start Without debugging”.

8. The text file is written on the same location as the project is:


Hi! Archita!
You are creating a text file
To use it for write or writeline purpose
You can use this for formatted output also
5/29/2007 5:08:37 PM



Declaration :
I am creating this post basically for my own reference while reading VC++.NET.
Many of the programs reffered here may resemble to the VC++.NET SDK.
I take no credit on saying that these are original programs written by me.


No comments: