Visual Studios 2010 c++ hello world

February 16, 2012

So you’re new to programming, perhaps just to c++? Then there’s no better place to start than with the “hello world” program. So what does this program do, you ask. This program seeks to achieve a simple goal, and that goal is to output the text “Hello world!” to the screen/console, or any text of your choosing  for that matter.

The First step is to get hold of Visual Studio. Now there’s no need to go get a paid package, the best option is to download Visual c++ Express. Here’s a link to the download page http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express. Once downloaded install using all default options.

Now that  installation has completed, fire up the application. I’m now going to guide you through some rudimentary steps, but I’ll make them explicit as I want this to be a very easy to follow guide . Select the File menu, followed by the  New menu , then the Project item. Below is a picture for clarity.

Now Select the Visual C++ tab, and then the Empty Project Item. Enter the name of the project in the name dialog, this can be anything, I have opted for “HelloWorld”, then click OK.  Again here is an image for clarity.

You should see the following on your screen.

What you see here are basically folders that you can use to organise you project files, in particular when you have a large number of files, something we are clearly not concerned with here. We will only need one file. Right-click on the Source Files  folder, go to the Add menu, then the New Item option.

Again select the Visual C++ tab, then select the C++ File(.cpp) option. Fill in the name of this file, again the choice of name is yours, I have used “MyFirstProgram” . Then click the Add button.

Now in front of you we have an empty file. This is were we will actually be coding, and if you’re new to programming this will probably be your home for a while. This may seem like a lot of effort to get things up and running, but if you quickly scroll back to the top of this post you’ll see it’s actually not at all bad.

Now in the file(“MyFirstProgram.cpp”) you have just created add the following text:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.      cout<<“Hello World”<<endl;
  6.      system(“pause”);
  7.      return 0;
  8. }

Line 1 “#include <iostream>” tells the compiler that you intend on using functions from the “iostream” library, specifically in our case to output to the terminal. Line 2 simply sets the namespace that we intend to use to “std” which means the standard namespace. This namespace will probably be the only namespace you will need for quite a while, so you can treat like “just one of those things you have to do”, for the moment at least.  Now we get to the main function which has the return type int(Integer), again for the moment just go with it. The important line is #6. This is were we actually output to the terminal. You can think of the “cout” as the terminal (this is of course a simplification), that whenever you proceed it with “<<” you wish to output something to the screen. The c++ language is also quite nice in that it allows you to change these kind of statemnts. for example

cout<<“He”<<“llo”<<” World”;

has an equivalent output to

cout<<“Hello World”;

the way to understand this is as follows:

((cout<<“he”)<<“llo”)<<” World”;

after each bracket resolves it returns another “cout”. The “system(“pause”);” leaves the created console open until a user input is received. The “return 0;”  is to signify that the program executed successfully, this line is however actually optional.

hope this helped and good luck with your c++ endeavours.


C++, without the agony.

June 6, 2011

Hi all.

I’m a programmer/maths lover with a special place in my heart for the bastard child known as c++. My first experience with c++ wasn’t all that pleasant. I had been coding primarily in java for about a year when I was thrown into the world of c++. It was a course on design patterns, but in c++(gulp). Luckily they were nice enough to give as a week crash course on the language. Unfortunately and needless to say any knowledge gained about the language was fragmented, and littered with false assumption carried over from my knowledge of java. The course ended and I tried to forget the horrors it contained, of which the primary one was c++.

So what was it about the language that put me off? The answer is easy I was uninterested, and the mastery of c++ requires interest. This may seem like a odd thing to say, of course as with anything, interest will facilitate better results. But in my experience c++ more then any language requires this. It was only later on in my programming life did I develop the interest , perhaps I had a stroke who knows.

Recently I gave an introductory course in c++, nothing complicated just the fundamentals. But I noticed the shear terror developing in the eyes of students when I started adding more  and more c++ to there knowledge pool. What I found odd was, that it was seldom based on the difficulty of the topic, they had just reached the point of information overload. It hit me that the reason people have a problem with c++ has very little to do with difficulty of components, but rather, the shear number of them.

That’s were this blog comes in. I aim to make this a a quick go to site , for when you just want an explanation of a component in isolation without the need to know 101 other things first. The blog will contain posts varying from fundamental to more advanced topics. I hope this blog will help your learning experience 🙂