C++ Beginner Tutorial - Hello World

Greeting and welcome to my tutorial on C++. If you want to learn how to program using C++, you come to the right place. These tutorials will focus mainly on the console program, meaning the program will use a text-based interface to communicate with the users and show the results. Alright, let's talk a little bit about C++.

Introduction: C++ is a computer programming language. It is widely known and used by most programmers because of its data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. The number one reason that programmers love it is the portability and efficiency of the language. The language provides programmers many choices, even though it’s possible for them to make a wrong choice. C++ continues to be the most preferred language when it comes to designing professional applications.

History: C++ is developed by Bjarne Stroustrup. At first, the original name of it was “C with classes.” In 1983, the name was changed to C++ (++ is an increment operator used in C++.) The idea of C++ came from his experience with problems during working on his Ph.D. thesis. At that time, he found that all the languages at the time were not powerful enough. So he built the language from C but with features of other languages as well. Up until now, C++ has been evolving as well as its library (the STL – Standard Template Library.)

First Program: Okay, let’s begin to create our first and basic program with C++. Now, I’m assuming that we all have Visual Studio installed. If not, you can get one here: Microsoft Visual Studio Express Editions. (You can also use Dev C++ if you don’t like using Visual Studio at all. However, they’re a bit different. I will point this out for you. I’ll be using Microsoft Visual Studio 2010 Ultimate. I’m sure it is not much different from the Express Edition.)
Visual Studio 2010 Startup Screen - Click on image to view larger size.
Go ahead and start Visual C++ Express up. Now, we are going to New Project -> Empty Project and enter the name: “Hello World.” We do not need to worry about other project right now. Click OK. 

Creating a New Project - Click on image to view larger size.

Now, on the far left of the window, there are folders: Header Files, Resource Files, and Source Files.” I want you to right click on Source File and click Add -> New Item…. This is how you would add a new source code files for your program.

Adding a Source Code File - Click on image to view larger size.
Select “C++ File (.cpp)” and enter “helloWorld” for the name or you can call it anything you want when prompted. Usually, programmers tend to name it “main” because it is the main file of the program. Every program has one main file.

Naming Your Source Code - Click on image to view larger size.

Ok, now this is what we’re going to type in for the “main.cpp” file:


#include <iostream>              
using namespace std;            

void main()                     
{
       cout << "Hello world!!!";
       
       cout << endl;            

       system("pause");          
}

Let's build the program and run it by pressing F5. If everything is ok, here's what you should have

Hello World First Run - Click on image to view larger size.
Okay, let's dissect our code:
  1. #include <iostream>: this is called a directive. Any statement that appears at the top of the code with a "#" symbol is directive. Basically what an #include directive does is to get all the constants and macro definitions from the specified file and add these into the source file. These files have the ".h" extension and are called header files. There are two types of header file. One is included and created by Microsoft and we use < headerfileName >. The other is user-defined header file and we use quotation marks to include them. (i.e. "myHeaderFile.h".) We will go into detail how to create one of these in the coming tutorials.
  2. using namespace std;: clearly this statement means to use the namespace named std. You may wonder what namespace means by this time. A namespace is a group of classes, objects, and functions under a name. The namespace std is default for <iostream> header file, which is a standard library. That is why we have included the “using namespace std;” statement in all programs that used any entity defined in <iostream>. Without using this namespace, you will not be able to write cout instead of std::cout
  3.  void main() {...}  this is the main part of your program. In this tutorial what we are doing is to output the string "Hello world!!!" by using cout << "Hello world!!!"  Whatever you put inside "..." will be printed out on the console. After that line is executed, the program will output endl, which tells the compiler it is a new line character. To end the program will use the function system("pause"); which will require user to press enter to continue. Now this is really dangerous to use in program, but for now it's okay to use. I will explain why in later tutorials.
Notice: Any statement in C++ ends with semicolon (;)

And voila, you have just written your very first C++ program. Come back later for more tutorials like this. If you encounter any problems, comment and I will reply.

Comments

Popular posts from this blog

Creating Markdown Blog with ASP.NET and C# - Part 1

Verbose Mode C++

Markdown vs. the World