0% found this document useful (0 votes)
19 views

Intro To C++

This document provides instructions for compiling and running a simple "Hello World" C++ program. It explains each line of code in the program. It recommends downloading a free compiler like Dev-C++ for Windows and provides the full code for a basic "Hello World" program using cout to print to the screen. The document then explains each line of code, covering includes, main functions, using cout to output, and return statements. It encourages the reader to experiment and ask questions.

Uploaded by

Dwayne Thorney
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Intro To C++

This document provides instructions for compiling and running a simple "Hello World" C++ program. It explains each line of code in the program. It recommends downloading a free compiler like Dev-C++ for Windows and provides the full code for a basic "Hello World" program using cout to print to the screen. The document then explains each line of code, covering includes, main functions, using cout to output, and return statements. It encourages the reader to experiment and ask questions.

Uploaded by

Dwayne Thorney
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

I will continue to write these (If you people actually enjoy them.

) 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.

Here is a list of a few free compilers for Windows:

 Bloodshed's Dev-C++(IDE: uses MinGW or Cygwin)

 Cygwin: Windows GCC Port(Command Line)

 Borland's(I think it's Command


 Line)

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.

Let's get on with the code Shall we?

Windows users: Open up Notepad.

Dev-C++ : Just open it up and click on the new file icon.


*nix: Open up your favorite Text Editor. :)

1: #include <iostream.h>

2:

3: int main()

4: {

5: cout << "Hello World!" << endl;

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++.

Congratulations! You are now a C++ Programmer!

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!

Iostream.h is called a header file and is included our program. Why?


Iostream stands for Input-Output-stream and is needed for cout, which
prints things to the screen. iostream.h is surrounded by a < and a >. This
tells the
compiler to search in the 'include' directory for the file. iostream.h can
also be
surrounded by Quotations like so: "iostream.h"

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 2 is just a blank line. It's just there as a part of whitespace.

Line 3 is the beginning of the Actual Program. On it is the main() function.


Every C++ program has a main() program. When your
program starts it automatically calls main(). According to ANSI Standard we
must state
main() to be int. This will be discussed in another tutorial.

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:

cout << "Hello World\n";


If you want a string of characters to be written to the screen you put your
text between two
Quotations: "Hello world!" like so! You then put a semi-colon at the end
signifying
the end of the statement. Semi-Colons are somewhat like an English period. It
tells the compiler
that a statement is over. You'll learn where to put them and where not to put
them as we get
further into the tutorials.

Line 6 is a return statement. It returns 0 to close the program. It also ends


in a
semi-colon. This will be discussed in more detail in a later tutorial!

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.

If anything is to complicated to understand or if I screwed up. >:) then be


sure to let me know.
Please let me know if you would like anything more, want something explained
in a little more
detail or whatever. I'll try my best to fix it!
- JARED

You might also like