A C++ Tutorial for Complete Beginners # 1

Submitted on: 1/2/2015 10:00:00 AM
By: Jared Devall (from psc cd)  
Level: Beginner
User Rating: By 170 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 12248
     I wrote this tutorial for those who wish to fully understand things from the beginning. If something is not explained clearly or is incoherent, please let me know and I will try to fix it ASAP!


I will continue to write these (If you people actually enjoy them.) until I come across things I do not yet fully understand.

First things first. To start programming you will need to get you a Compiler. There are several free as well as a few Commercial ones. Unless you have a few hundred dollars to buy the Commercial Compilers I suggest you get you a free compiler.

Here is a list of a few free compilers for Windows: Linux and Unix come with a C/C++ compiler(I think). Just make sure you installed it and read the manuals!

Okay. You'll need to download one of those compilers and read the manuals on them. I use Dev-C++. If you have some questions using that specific compiler I might be able to help.

Let's get on with the code Shall we?

Windows users: Open up Notepad.
Dev-C++ : Just open it up and click on the new file icon.
*nix: Open up your favorite Text Editor. :)

1: #include <iostream.h>
3: int main()
4: {
5: cout << "Hello World!" << endl;
6: return 0;
7: }

Go ahead and copy that little piece of code. (Starting with #include and ending with the last } ) You can paste it or just type it up in the text editor. Save it as hello.cpp . The .cpp extention lets the compiler know it's a C++ Source File. To compile in Dev-C++ click on the compile icon or Execute->Compile.
Note: The Remove the numbers. They are there only to help analyze the code!
You will have to read the instructions on how to compile it in Compilers other than Dev-C++.

Congratulations! You are now a C++ Programmer!

On Windows Machines the application will close very quickly if you run it. THAT IS OKAY! We will learn how to fix that later.. The point is you got the program to compile! Now let's break down the code.

#include <iostream.h>

The first symbol is the pound symbol. It is a symbol to the preprocessor. (Any words that are links will link to another tutorial with definitions in it. For now it DOES NOT EXIST)
After that is the word include. Together #include tells the compiler to include a file into your program. It is just as you had written it in there yourself!
Iostream.h is called a header file and is included our program. Why? Iostream stands for Input-Output-stream and is needed for cout, which prints things to the screen. iostream.h is surrounded by a < and a >. This tells the compiler to search in the 'include' directory for the file. iostream.h can also be surrounded by Quotations like so: "iostream.h"
This tells the compiler to look in the current directory for the file. iostream.h is obviously not in your current directory so leave the greater than and less than sign there!

Line 2 is just a blank line. It's just there as a part of whitespace.

Line 3 is the beginning of the Actual Program. On it is the main() function. Every C++ program has a main() program. When your program starts it automatically calls main(). According to ANSI Standard we must state main() to be int. This will be discussed in another tutorial.

Line 4 begins the body of the main() function. All functions begin with an Opening brace ( { ) and a closing brace ( } ) just as main() does. Everything inbetween is considered to be a part of the function.

Line 5 is what the program is all about! The object cout is used to print a message to the screen. This is how cout is used:
You write cout followed by the output redirection operator ( << ). The operator is created by hitting shift-comma twice. Everything after the < is printed to the screen. (Or atleast tried to.) until it reaches a semi-colon. ;
endl is way to make the string go to a new line. You also put a \n in the string instead of using endl like this:

cout << "Hello World\n";

If you want a string of characters to be written to the screen you put your text between two Quotations: "Hello world!" like so! You then put a semi-colon at the end signifying the end of the statement. Semi-Colons are somewhat like an English period. It tells the compiler that a statement is over. You'll learn where to put them and where not to put them as we get further into the tutorials.

Line 6 is a return statement. It returns 0 to close the program. It also ends in a semi-colon. This will be discussed in more detail in a later tutorial!

Line 7 is just a closing brace and the end of our program. The closing brace is required to 'close' the function! That's the 'hello world' program fully explained. You can experiment with outputting text by simply making a couple more cout statements.

If anything is to complicated to understand or if I screwed up. >:) then be sure to let me know. Please let me know if you would like anything more, want something explained in a little more detail or whatever. I'll try my best to fix it! - JARED

Other 2 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.