|
By hylke at 2004-05-02 03:24
|
|
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:
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World \n";
return 0;
}
#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:
[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?
So that's my short howto. Have fun with it. If you have any questions email to hylke@linux.net
|
|
|
|
% ./HelloWorld
What other C++ applications are available, and which would you suggest?
APIs ... it's not a C++ development tool.
Cheers,
Tink
---
I think this article or how to was a bit short. I clicked on it to find information on how to start programming for Linux.
I would have like to read not a simple sample of hello world as that's basically the same on any platform. But I wanted to know whats better KDE(QT) or GNOME (GTK) and what frameworks and libs are good and from a developers point of view but also from a end users point of view.
Basically what I need to start working on a modern application for Linux and the pro's and cons of certain frameworks.
--noob tryin to rtfm--
cryptowicked-
"I've seen the howto of crabboy so i thought, maybe its fun to make one in c++. This is just some verry basics of c++. Suchs ass printing text, some variables and stuff like that."
There are many English mistakes in this. "its" should be "it's". "This is" should be "These are". "verry" should be "very". "suchs" should be "such". "ass" should definitely be "as"! "c++" should be "C++". Here is a more concise and correct version:
"I've seen the howto of crabboy and thought it would be fun to make one in C++. These are just some basics of C++, such as printing text, some variables and stuff like that."
I am trying to be helpful. People are more likely to spend time on your content when your grammar, spelling, and general usage of English are adequate.
$ make tst
g++ tst.cpp -o tst
make is intelligent about what to do now ( it will invoke the g++ compiler ).
This works on almost all unix platforms.
If you want to pass options use the CXXFLAGS make variable
$ make tst CXXFLAGS=-g
g++ -g tst.cpp -o tst
Now if you are using vim for editing the files, this becomes even simpler:
$ vim tst.cpp
:make tst
this invokes the compiler for you.
To run it just come out of vim with a Ctrl-Z
./tst
$ fg
takes you back to your editing session.
There are three methods to bulid "C" & "C++" programming in RHL 9.0 & FC .by using GNOME desktop & another in KDE .
There are mainly two editor in GNOME , viz. vi- editor & another is emacs editor.I will discuss both here.
using VI-editor:
1. open terminal .
2. type for C programming
vi <filename>.c
for C++
vi <filename>.cpp
e.g:
vi hello.c { for C}
vi hello. cpp { for C++}
3. then type " i" ( i= insert)
4. then write source code . for details www.howstuffworks.com how to works "C" programming?
5. then press
esc : wq
it is save now.
6. now for comile the programme type,
cc <filename>
7. then for execute type
(dot)/a.out ( dot = . )
8. then it will see your screen.
USING EMACS EDITOR:
1.type emacs<file name> on terminal.
3. then type " i" ( i= insert)
4. then write source code . for details www.howstuffworks.com how to works "C" programming?
5. then press
esc : wq
it is save now.
6. now for comile the programme type,
cc <filename>
7. then for execute type
(dot)/a.out ( dot = . )
8. then it will see your screen.
USING KWRITE IN ( GNOME OR KDE DESKTOP)
1. Open terminal & type kwrite
2. then write source code
3. then click file .
4. then clck save as
5. save the file wtth extension .c/.cpp
now for comile the programme type,
cc <filename>
7. then for execute type
(dot)/a.out ( dot = . )
I hope it will helpful .
If you face any diffuculty u can email me
partha_26786794123@yahoo.com , ( for alert reply here)
partha
think it is good to explain how to create a Makefile and why almost any source
one can download will contain this file (sometimes after configure executing).