We’d like to give our new category, Programming, a bit of love. So, we’re going to have some C++ tutorials from the ground-up, starting with your classic Hello World.
This tutorial should work on any system with a C++ compiler. For Windows, try Dev-C++. Ubuntu and Debian users, install the package build-essential. Fire up your text editor or IDE and let’s get going. The rest of this tutorial will be in the code comments (marked by /* */ and //) below; while you can copy them into your compiler if you want, they are not necessary.
/* This line isn't actually C++ code at all; it simply tells the * compiler that you want to include the file into your program. * * iostream is a standard library for C++ applications, and it * basically lets you output information to the screen and read back * from it. You'll use it in 99.9% of all C++ projects. */ #include <iostream> /* Next, we need to tell the program what our main function is. This * must be called "main" and usually looks like the following: */ int main(){ /* A function is constructed with the return type, the name of * the function, and any arguments or parameters. In this * function, it is called "main," has no parameters, and will * return a value of type integer. "void main(){" could also * work here to return nothing, however you will want to read * on for more information on this. * * (Advanced note: We're leaving out the standard arguments for * now.) */ // And now, let's output some text on the terminal screen. std::cout << "Hello World!"; /* Instead of trying to explain this all at once, we're going * to dissect it into parts: * ** std:: * This is a function of the standard library, and all of these * need to be prefixed as such. You can actually avoid adding * std:: to your code by adding: * using namespace std; * right after your #include lines, however it is not good * practice. * ** cout * This is the actual function we are referencing in the * standard library: cout. It outputs data put into it to the * console. * ** << * This makes the data on the right get put into the left. * In this case, we are placing text into std::cout. * Think of it as throwing data at a function; the arrows point * to the left, so the data is output to std::cout. * ** "Hello World!" * This is what is known as a string. A string is, in a general * sense, text. Quotes are *always* placed around strings. * ** ; * Semicolons are required at the end of all lines of C++ that * are not control statements. Basically, they go after every * line that doesn't have braces {} at the end of it or are not * #include lines. * */ return 0; /* This return statement finishes what we started with * "int main" above. Since we told the compiler that the * function is returning an int (integer), then we need to * follow through with that by actually returning one. If you * chose to use "void main" above, this line should not be * included since void functions do not return anything. */ //This closing } ends the main() function. }
I recommend manually typing in the code above, line by line (excluding the comments). This makes sure you are able to learn by doing and not just by seeing. Save the file you just wrote as test.cpp and then open a terminal (unless you are using Dev-C++, in which this can be accomplished by hitting Compile).
g++ -o test test.cpp
This invokes the compiler using the file test.cpp and outputs it as a test binary. To run your newly-made program:
./test
If it didn’t compile correctly, check your error messages and look for common mistakes, such as a missing quote or semicolon. If it did work, congratulations! You’ve just written your very first C++ program. Subscribe to the Programming section for more tutorials like these in the future.


Cisco Routers, Car Alarms, and C++ — Trevor’s Weblog wrote:
[...] came across an article on FOSSwire on beginning to program in C++. Decided to give it a try, their first tutorial was [...]
# Posted on 01-Nov-07 at 9:52 pm