Well due to the lack of any programming tutorials here I figured I would post one.
I will be writing new tutorials as I find time which should allow me to release every few weeks or so.
First, C++ is a popular and powerful programming language used in many large scale games and applications. Although C++ is a rather difficult language compared it's predecessors (such as C#) it makes up for that with it's speed and how much you can do with it.
I will be using the Microsoft Visual C++ 2008 express edition compiler and basing these tutorials around it. This is, at least in my opinion, a very good compiler and it is absolutely free. You can only compile AND run your projects on a windows based operating system that have the .NET framework installed. So that means if you want to write C++ programs for any other operating system you must use a different C++ compiler. Most other C++ compilers should compile the native code with few changes but are unable to compile managed C++ code.
One last thing to consider is that I am only 14 years old and NOT a professional. This means that, although they work, I may use some techniques that are not considered good practice or that have an easier way to preform.
Alright with that over lets move on to writing our first program.
Open up Visual C++ and under Win32 projects select the Win32 console application. Give your project a name and push OK. When the wizard comes up, push next and check the option "Empty Project". Push finish and you should see, in the solution explorer, your project and three blank folders below it. Right click the source folder and choose the option add - new item. Select, under the code section, the .cpp file, name it main.cpp, and push add.
.cpp files contain the working code for your program. There are also .h files that contain class, struct, function definitions, and preprocessor dierctives. I'll get into .h files more next lesson but for now don't worry about them.
Before we can do anything in a program we must define an entry point. All executable programs and libraries have entry points. They are simply where the program begins.
Lets define an entry point to our application, type the following code in main.cpp:
int main() { return 0; } Let's go through this line by line.
First, int main() defines the main function of the program. Regardless of where you put it, the program will always start by running main. The "int" before it specifies that it returns a value that is of the type int. An int stores a number using 4 bytes of memory and can range from -2.1 to 2.1 billion. Notice that after main there is a pair of ()'s. These are required to define a function and without them you would be defining a variable, or a word associated with an area of memory that you can store data in.
Next we have a "{". This means that everything after the { and before the } will be in the scope of the function main.
Then we have the statement return 0;. When a function reaches a return statement it terminates and "returns" the value after it, if any, to what called it, in this case the operating system. The semi-colon ";" after the statement simply means that it is the end of the statement. Returns are not necessary when programming C++ since the ; ends the statement but they make the code a lot easier to read and follow.
Finally, we have the } which signals the end of the function.
Now that we have a working program let's make it do something. Add the line "#include " (without the quotes) at the top of the file and the line 'std::cout
int main() { std::cout
Build Solution. This will compile the project in to a .exe file. After that in the same menu click on Debug - Start without debugging. You should see a black console window displaying the words "Hello!".
Ok so lets go over how this works. First the line #include includes a library of functions (such as cout and endl) that deal with input and output to the console and external files. The # specifies that the command following is a preprocessor directive that the compilers preprocessor is to handle.
Next we added the line 'std::cout