First thing we need to do is make a .cpp file.
Type in a shell:
vi HelloWorld.cpp
That means create a file HelloWorld.cpp and then edit it.
Type i to insert text.
Type Esc w q to save the file.
You can also use any other editor you like.
HELLO WORLD EXAMPLE
Here comes the source code:
#include <iostream> Means, include the iostream header so we can use cout to print out our text.
using namespace std; means, use the standard namespace.
You can also delete it and add std:: before cout.
int main() means this our main function, of the type integer.C++ automatically jumps to the main function and executes every line what's between { and }.
cout << "Hello World \n"; Means: print Hello World on the screen and end the line(\n).What i mean with end the line, if use another cout to print text it would appear on the next line, if you didn't do that it would appear next to it.The ; means, and of our cout line.
return 0; Means: return the value 0 to the function.It isn't really important to understand what it does, but you have to know that its necessary.
COMPILING THE SOURCE CODE
Now we're going to compile our source code using the GNU g++ compiler.
Take care you're in the directory with HelloWorld.cpp in it.
Then type:
g++ HelloWorld.cpp -o HelloWorld
It means compile and link HelloWorld.cpp to an executable called HelloWorld.
Now HelloWorld is ready to be executed and Hello World will appear in your shell.
VARIABLES AND INPUT
If we want input from the user we need a variable to store our input in.There are lots of different variables, but to keep it simple we only use the string type.
Here's our source code:
#include <iostream>
#include <string>
int main()
{
string name;
cout << "Type in your name: ";
cin >> name;
cout << "Hello " << name << "! \n";
return 0;
}
Our first new line is #include <string>, that means include an header called string to let us use a string variable.
The first line in our main function is string name;.
That means: make a string variable called name to store our input in.
cin << name; means: Put the input of the user in the variable called name(we use created it).
The next line prints first Hello, then the name you typed and then !.
Now it probably makes sense why we use " and " to print our text in and not without the "s, because that may confuse the compiler.Is it a variable or just text?
[source:LinuxQuestions.org]
Type in a shell:
vi HelloWorld.cpp
That means create a file HelloWorld.cpp and then edit it.
Type i to insert text.
Type Esc w q to save the file.
You can also use any other editor you like.
HELLO WORLD EXAMPLE
Here comes the source code:
Code:
#include <iostream> using namespace std; int main() { cout << "Hello World \n"; return 0; }
using namespace std; means, use the standard namespace.
You can also delete it and add std:: before cout.
int main() means this our main function, of the type integer.C++ automatically jumps to the main function and executes every line what's between { and }.
cout << "Hello World \n"; Means: print Hello World on the screen and end the line(\n).What i mean with end the line, if use another cout to print text it would appear on the next line, if you didn't do that it would appear next to it.The ; means, and of our cout line.
return 0; Means: return the value 0 to the function.It isn't really important to understand what it does, but you have to know that its necessary.
COMPILING THE SOURCE CODE
Now we're going to compile our source code using the GNU g++ compiler.
Take care you're in the directory with HelloWorld.cpp in it.
Then type:
g++ HelloWorld.cpp -o HelloWorld
It means compile and link HelloWorld.cpp to an executable called HelloWorld.
Now HelloWorld is ready to be executed and Hello World will appear in your shell.
VARIABLES AND INPUT
If we want input from the user we need a variable to store our input in.There are lots of different variables, but to keep it simple we only use the string type.
Here's our source code:
#include <iostream>
#include <string>
int main()
{
string name;
cout << "Type in your name: ";
cin >> name;
cout << "Hello " << name << "! \n";
return 0;
}
Our first new line is #include <string>, that means include an header called string to let us use a string variable.
The first line in our main function is string name;.
That means: make a string variable called name to store our input in.
cin << name; means: Put the input of the user in the variable called name(we use created it).
The next line prints first Hello, then the name you typed and then !.
Now it probably makes sense why we use " and " to print our text in and not without the "s, because that may confuse the compiler.Is it a variable or just text?
[source:LinuxQuestions.org]
No comments:
Post a Comment