
Infinitus
Veteran

Jan 24, 2005, 5:12 AM
Post #2 of 13
(4633 views)
Shortcut
|
|
Re: [Java Code] C++ for beginners
[In reply to]
|
Can't Post
|
|
That was somewhat confusing Java. Here is a simpler approach to the Hello World program: 1 #include <iostream> 2 3 using namespace std; 4 int main( void ) 5 { 6 cout << "Hello World!" ; 7 return 0; 8 } To start out you must learn what each part of this program does. In line 1, we see #include. #include is a directive. It allows you to incoporate other files you or others have created into your source code. Next we see <iostream>. <iostream> is a standard library that is found in every C++ compiler. It deals with the input and output of your programs, such as text output to the screen as well as user input into the program. I'll explain user input in a sec. It is also helpful to know that standard library files have no file extension and are placed in < > without the .cpp extension. In line 3 we see using namespace std; Namespaces are used to make standard libraries function correctly. When using the namespace std you are allowed to use the statements, or commands, cout and cin. Namespaces are a bit more complicated and it's best not to worry about them yet. Just besure to include "using namespace std;" when #include'ing <iostream>. In line 4 we come to int main( void ). the first part, int, shows that when this function is complete, it will return an integer to show it has run through successfully, by standards, it should return a 0, which we will get to later in the code. The next part, main( void ), specifies the beginning of the main function. All programs have ONE main function. Most of your coding goes into this function, although you are also able to write your own functions and use them within your programs. All of the code inside the main function go inside of the brackets: { } In line 6 the statement cout is used. cout is used to display output onto your computer screen, such as text, numbers, etc. cout is followed by two less than signs: <<. These show the direction the data is going. cout << "Hello World!" ; This shows that the data "Hello World!" is going to be displayed on your screen as the output. The last important line (excluding the ending bracket for the main function) is: return 0; This pretty much tells the computer nothing else is going to occur inside main and to exit the function. That is how to interpret the Hello World program. (I normally use Hullo World ) ----------------------- Things I forgot to add: You must always end lines in your program with a semicolon (;). There are special exceptions such as int main(void) and others but generally you will need to use one. To add comments, you can use: //Insert comment here for single line comments /* Insert comment here for commenting inside the statement */ /* Insert comment here for commenting over several lines */ It is best to use the first example. It is possible to use the others but more than likely it will make your program very messy when trying to read it.
(This post was edited by Infinitus on Jan 24, 2005, 5:15 AM)
|