Intro To C++
Intro To C++
) 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.
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.
1: #include <iostream.h>
2:
3: int main()
4: {
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++.
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!
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 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:
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.