ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

Hello World - How To Create Your First Visual C++ Program

Updated on June 10, 2014

Getting Started

So, you are ready to write your first C++ program. Well, in order to do so, you need to make sure you have a compiler installed. The compiler I recommend is Microsoft Visual C++ 2010 Express. If you have another one you are using, that's fine, but this programming tutorial will be demonstrated in Visual C++.

If you need to install Visual C++, you can find instructions for that here:

Installing Microsoft Visual C++ 2010 Express

Once that is taken care of, we are ready to move on and get this show on the road!

Launching Visual C++ and Creating a Project

Okay, the first thing we need to do is to launch Visual C++. You can do this by visiting the following location:

Start >> Programs >> Microsoft Visual C++ 2010 Express

If this is the first time you have launched it, there may be a slight delay while it creates the initial directory structure for the first time. When the program opens you will be taken to the "Start Page".

From here, you need to make a new Visual C++ project to house your C++ program. This can be done by selecting: File >> New >> Project as in the screenshot below.

Now you will be faced with a screen that asks what sort of project you would like to create. For the purpose of this tutorial, we will simply choose "CLR Empty Project".

We are choosing this because it allows us to create everything from scratch. That's what a computer programming tutorial is all about right? In order to learn to program properly, you need to be able to create what you need, not rely on others to build it for you.

On this same screen, you also need to name your project "Hello World", and when you do, it will automatically name the solution the same thing. Also, make sure to leave the option "Create Directory for Solution" checked. This way, it will create a whole new directory to house your project.

When you have finished making the selections above, simply click "OK" and the project and solution will be created for you in a brand new directory. Congratulations, you are now ready to move and create your first source code file!

Creating Your Source File

Now that you have created your project, you will see a screen that shows the "Solution Explorer" for your project. Other than the Solution Explorer, the rest of the project page will be blank.

So, to make our C++ Hello Word program, we need to write some source code that can be compiled and executed. But before you can write source code, you need a source file to put it in. Let's create it now.

In the Solution Explorer, right click on "Source Files" and select Add >> New Item

The next screen wants you to choose the type of item you would like to add to the project. Since this C++ tutorial is a simple "Hello Word" program, we only need to add one C++ file. These files will always have a .cpp extension.

Select "C++ File (.cpp)" as the type of item to add and then name it "Hello World", just like the project and solution.

After you have selected the type of file and named it, select "Add". This will now add the file "Hello World.cpp" to your collection of source files in the Solution Explorer. You will also now see the open "Hello World.cpp" file open and ready for adding your source code.

Adding You Source Code

In this C++ programming tutorial, you are simply creating a program that will print the phrase "Hello World" to your computer console. In doing this, you only need to write a grand total of 7 lines of code to the source code file. Below, let's go through each line and explain exactly what it does.

These lines should be added directly in the blank text editor space on the screen.

Line 1: #include <iostream>

This is known as a "pre-processor directive" and is used to pull in c++ libraries that need to be compiled with your source code. This line tells the compiler to add in the "iostream" library so your program can process simple input and output.

Line 2: using namespace std;

Namespaces are used to define a particular working space for a program. If you had multiple cpp files in your program, they would all have the same namespace so they could function together. Even though we only have one file, it is still good practice to define a namespace.

Line 3: void main()

Every c++ program must have a function called "main". This is always the starting point of execution for any c++ program. Every function must also have a "return type". The return type for our main function is "void" because it does not return anything. When it executes, it simply prints a line of text to the console and exits. If our function happened to perform some calculation and return an integer, it would be called "ïnt main()".

Line 4: {

All code within a function must be contained in "curly braces". These braces simply tell the compiler that all of the code in the function compiles as a single unit. This is the opening brace for our function before we place any code into it.

Line 5: cout << "Hello World" << endl;

"Cout" is the name of the function used to print output to the console. It is actually contained within the iostream library, which is why we included it in the first place. Then the compiler will output to the console anything after the "<<" delimiter.

"Hello World" is the string of text that cout is going to print to the console. The next "<<" is telling the compiler that there is something else after "Hello World" that needs to be sent to cout.

The "ëndl" statement is a reserved keyword in c++ that tells the compiler to print a new line on the console during execution. All this does is skip the cursor down one line and is basically used for readability.

Line 6: system("Pause");

The "system("Pause");" statement tells the compiler to pause the console until the user decides they are finished looking at it. If we did not pause the console, it would come up and go away so fast that you would not be able to tell if it printed your statement properly. This will pause the console until you "press any key" to exit.

Line 7: }

This is the closing curly brace that tells the compiler it has reached the end of the main function. At this point, since main is the only function in your program, compilation will stop and execution will begin.

Super congratulations !! You have completed adding your source code to your Hello World.cpp file. It should look something like this:


Compiling and Running Your Program

Now that you have all of the source code put in, you need to compile your program and see it work. To do this, simply press the green "play" button at the top of the Visual C++ Window.

You will be asked if you wish to compile and build the file. Select "Yes".

Then, if there are no errors, you should see a console screen come up and display your output like this:

What Next?

Now that you have written, compiled, and ran your first Visual C++ program, you can move on to more advanced topics.

You might also want to check out some of the books below. They contain some great C++ Programming Tutorials to build your expertise!

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Johng520 3 years ago

      Hi, I desire to subscribe for this web site to obtain latest updates, therefore where can i do it please assist. eefkffckedac