Before starting computer programming its a very good idea to get a general overview of what computer programming is useful for ?
The best way to learn C++ is to look at existing code and understand its fundamental elements. If you are familiar with C, Java, Javascript, PHP, Flash scripting or other C inspired languages, most of this syntax will be familiar to you. Unlike web languages such as PHP and Javascript, C++ is not an interpreted language. In other words, once you write the code in any basic text editor, you will need to compile the code into objects, and then link all the objects together into an executable program. The "compiler" turns what you write into low level codes the computer can understand, and the "linker" makes the low level codes able to be run. Almost any IDE (Integrated development environment - an advanced text editor, which takes much work off of your hands as formating, coloring syntax, compiling and linking as well as debugging) will do this task for you with a single click. Such IDEs include Code::Blocks or KDevelop (even though there are many others)
Alright, on to the code. Two consecutive forward slashes is a single-line comment, and everything to the right of the slashes will be ignored by the compiler.
#include
using namespace std;
int main()
{
cout << "Hello World!" <<>
cin.get(); //Without this line the program ends very quickly in Windows
return 0; //Returns Success of the program
}
Includes
Now to explain this introduction to C++ code. The commands that are used in this example are defined in two files which are included with the #include directive.
#include
Most of the time you will need to include commands like this so you don't have to reinvent them from scratch. When the C/C++ compiler reaches an #include directive, it will attempt to load the file named within the symbols
The
main()
When you compile your code, the compiler looks for a main function and starts the program there. Functions will be explained in greater depth in a later lesson, but what you need to know now is that the compiler looks for main() and runs the commands contained within main() first. You start writing your commands immediately after the { symbol, and the program ends when it reaches the } symbol. A note: with normal functions, you call them from another function, for instance, if you had a function that took two numbers and multiplied them, you might use code like this: multnumbers(6, 2);. So what calls the main function? The answer is the operating system, and so that is where your program starts off. That is why it is so important to include the main() function in your code. By the way, if you don't understand this, don't worry. It will all be explained later on, like I said.
Namespaces
Within all modern programming languages there is the concept of namespaces. Just as I said before, using other peoples code is a great way to get your code working. Again, there is no point in reinventing the wheel. Namespaces contain all sorts of "identifiers" that are useful. In our program there is a namespace called std, which contains cout and endl. The method to use an identifier within a namespace is:
namespace::identifier
So in the case of our program, we've used the namespace std and the identifiers cout and endl. Namespaces can be nested, you can create namespaces, and you can be using identifiers from multiple namespaces at the same time. Coding with the namespace::identifier format can get tedious though, especially if you are only using one namespace in your code. If you had not defined the namespace you were working in (in this case "std") you would have to write std::cout or std::cin everytime. So it is easier to tell the compiler a namespace from which you are using the functions from - for this reason, there is an easy way to let the compiler know that you will be using the std (or any other namespace) for the rest of your code. It's a way of telling the compiler where to look when it can't find an identifier you used. The code looks like this:
using namespace std;
I've used the namespace "std", which is the namespace we used in our code. This will allow us to remove std:: from all of our code. This is extremely useful, because you can imagine how much extra space would be used up by std:: if we had more that one line of cout and endl. "std" is also used for other identifiers that you'll be using in your future code, so it is helpful to include the std namespace in most of your beginner programs. After you've removed std::, your code should look like this:
#include
using namespace std;
int main()
{
cout << "Hello World!" <<>
cin.get(); //Without this line the program ends very quickly in Windows
return 0; //Returns Success of the program
}
Cout, endl, and cin
The cout identifier is an easy and powerful way to output information to the screen. The opposite of the cout identifier is cin, which will be used in our next program. Each operand for cout is seperated by <<, whereas each operand for cin is seperated by >>. This lets the compiler know when it should be looking for a new operand to control cout or cin. For example, you can add extra text by using the code:
cout << "I'm going to try and display Hello World! on a new line" << color="#009900">I'm going to try and display Hello World! on a new line
Hello World!
The cin command is used to recieve user input. Without the cin.get() command the program would run and exit in less than a second on Windows Platforms. By adding this line, the user must press enter to end the program. This gives you a chance to see your code work.
Another way you can do this is using "\n". \n basically means "new line after this" here is an example of it in use:
#include
using namespace std; // very bad practice with namespace
int main()
{
cout << "I'm going to try and display Hello World! on a new line\nHello World!\n"; system("pause"); // this won't work on many platforms - you should stick to standard C++ return 0; }
