Ever wanted to dazzle your friends by writing programs that can do nearly anything? With C++ and this series of tutorials, you can.
Part I:
Hello, World!
Greetings, brave adventurer! You are about to set out upon a journey from which you will likely not return: the journey into C++. C++ is one of the world’s most popular programing languages. “C With Classes” was developed by a incredibly smart man named Bjarne Stroustrup (don’t ask me how to pronounce it) in 1979, and “C++”, its current name, was bestowed upon it in 1983. It is somewhat of a running joke among programmers. In C, if someone wrote a line of code that stated “x++”, it would mean that x = x + 1, or x gets the value of one more than x. Therefore that C++ means that it is one more than C. If you laughed at that, you’re already in too deep.
Now that we’ve established the history of the language, let’s talk business. Some of you might know that computers can only interpret instructions that are coded in 1’s and 0’s, also called binary. Don’t worry, though, none of you will have to memorize intricate combinations of the two numbers to perform specific instructions. However, this presents a dilemma: how can the computer understand your instructions if you aren’t programming in 1’s and 0’s? Well, a long time ago, people thought of that, so they made assembler code. Assembler is a very basic form of coding that easily translates into binary. Assembler is difficult to learn and use effectively, though, which is where high-level languages like C++ come in. C++ compilers translate your code into binary instructions for the computer to execute. Some compilers can cost hundreds of dollars, and you don’t want to spend big bucks like that when you can get an excellent compiler for free, do you? Microsoft makes a fantastic free compiler called “Microsoft Visual C++ 2008 Express Edition”. You should take into account that this is an express edition, so it doesn’t have quite the same power as its expensive counterpart.
Head on over to Visual C++ 2008 Express Edition’s download page, http://www.microsoft.com/exPress/download/ Download it and install. If you get some sort of error concerning the SQL server or something like that, don’t worry. That happened to me too. Now run it, and go to the file menu, New, Project, or just press Ctrl+Shift+N. Choose Win32 from the side menu, and Win32 Console Application from the presented menu. Name it helloworld, press okay, click Finish in the new project wizard, and we’re off!
Look over in the Solution Explorer (the bar on the far left that looks like a directory) and you should see several things. Under header files, there should be two files, called stdafx.h and targetver.h, and under source files, there should be helloworld.cpp, and stdafx.cpp. The first thing we are going to do is go to stdafx.h (by double-clicking on it) and underneath everything except the green line at the bottom, add the following:
#include
#include
using namespace std;
I will explain why we are doing all these in a moment. Now double-click on helloworld.cpp, and add the following lines of code inside the curly braces ( { and } ) of “int _tmain(int argc, _TCHAR* argv[])”
cout << “Hello, World!” << endl;
_getch();
Make sure that somewhere on this screen you see #include “stdafx.h” and then you are ready to go! Press F7 to compile it, and F5 to run it. It may prompt you to save your work, and do so. Look towards the bottom of your screen and click “Output” if it hasn’t already been selected. If you have done everything correctly, it should say on the last line:
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
If it doesn’t, go back and check over everything I have said and look for any errors. Once you get it compiled, press F5 to run and off we go! A black box should show up on your screen bearing the message for all to see, “Hello, World!” Press any key and it disappears.
Now that you got that working, it is time for me to help you understand why what you see what you saw. We’ll start at the beginning. C++, like nealy all high-level programming languages, uses header files to recieve built in functions. The header file you included, stdafx.h, included several other header files. The reason I had you include iostream is because iostream is the input and output stream on your computer. Simply put, it allows you to write and read from the screen. Skipping ahead to the using namespace std, I had you do this even though it wasn’t entirely neccesary. Std, meaning standard, is a very powerful module, or group of functions. It includes reading and writing to the screen. A namespace is like a prefix. Every command the compiler doesn’t recognize, it checks with the namespace to see if the namespace understands it. Without it, your first line of code would have had to have been something like this:
std::cout << “Hello, World!” << std::endl;
Nobody wants to have to have the std:: everywhere, so they just use a namespace. Namespaces can get tricky as you get more and more advanced, so watch out for ‘em! Conio.h is another header file that allows the use of the _getch command, which I’ll describe in just a few moments. Now, onto the code body.
In helloworld.cpp, the first thing you should see, besides another green line, is the inclusion of “stdafx.h”, the header file we have just modified. After that, you see your own function, _tmain. Just like header files, programs contain functions too. Those things in the parentheses are the arguments, but don’t worry about them yet. The left curly brace means your function is beginning. The right curly brace at the bottom of the screen means that your function is ending. The command cout tells the computer that you want to write something or multiple somethings to the screen. This something or somethings are separated from the command and each other by these: <<. You are writing two things to the screen, “Hello, World”, and an endline. As you might be able to guess, the endline has the same effect as pressing Return in a word processor: it begins a new line. If you are new to the world of computer programming, I’ll let you in on a little secret. Every line of actual code, not function declarations or includes but code, must end in a semicolon; it is essential for you to know this. Moving on, _getch() is part of the conio.h header file. What this does is it pauses the code until the user presses a key. It is quite useful way later for game development, reading in keys from the keyboard without writing them to the screen. All functions that are not of the type void (don’t worry if you don’t know what this means) return some sort of value. Often it is the result of some math equation or an error code. It is usually best to return a 0 from your main function until you really understand what’s going on.
For practice, try outputting multiple strings on different lines, numbers, answers to simple problems (you’ll want to go cout<<12*3 or something of the sort), and experiment around. Enjoy!
This concludes Lesson I.












Leave Your Response