1
#include <cstdlib> 2 #include <iostream> 3 4 using namespace std; 5 6 int main(int argc, char *argv[]) 7 { 8 //This line prints out a salutation and your name 9 cout << "Hello my name is: " << "George Bush" << endl; 10 //You can change the text between the quotes to describe yourself by day 11 cout << "Goofy President by day" << endl; 12 //You can change the lines between the quotes to describe your super self 13 cout << "Terror of the Terrorists by night" << endl; 14 //This line pauses the program, try putting // in front of it to see what 15 //happens 16 system("PAUSE"); 17 //This line says that the program has terminated normally, ie not crashed 18 return EXIT_SUCCESS; 19 }
Purpose of the program
This short program is designed to be
little than a demonstration of what program code looks like. As an example this
program does very little. It is designed as a description of a Super hero. You
can change the value where it says George Bush to read any name of your choice.
Then change line 11 to describe what the Super hero looks like as a normal human
beings. Then alter line 13 to describe them as their Super selves. Basically
alter or 'Hack' the code to see what difference various changes make, this is a
great way to learn.
Break down of the code
Line 1 - This line starts with
a '#' hash sign which indicates that it is a preprocessor directive. The
#include directive instructs the preprocessor to copy the contents of the
following file into this source code file at this point. The filename is
surrounded with '<>' brackets this instructs the preprocessor to look in the
system 'include' directory first for this file. So this line copies the contents
of the file cstdlib into the source code file at this point before compiling
starts. The file cstdlib contains the declaration of many basic functions that
are used in c/c++ programs.
Line 2 - This
line works in the same manner as line 1. The difference is that the preprocessor
copies the contents of the file 'iostream' in to the source code at this point
before compiling starts. The file 'iostream' contains the declaration of C++
Input/Output functions.
Line 3 - Is
blank.
Line 4 - This
line uses the keyword combination using namespace. The name of the namespace
follows in this case it is std. Namespaces are like packages which contain
functions, classes and the like. They allow you to use functions with the same
name and arguments without clashes. This program uses cout and endl both of
which are contained in the std package. If we didn't use the namespace std we
would have to specify each time we used cout or endl that it is part of the std
package we would do this by writing std::cout or std::endl each time. It is a
bad idea to write using namespace std in header files since they can cause
clashes later on.
Line 5 - Is
blank
Line 6 - This
line contains the start of our definition of the main function. As you can see
main returns an int value. This value indicates to the operating system whether
or not the program has terminated normally. The main function also takes two
parameters these contain any command line options that are sent to you program
when it starts. E.g. if you start notepad from the command line you would write
notepad if you want to start notepad with a file to edit you would write notepad
readme.txt. readme.txt would be a command line option. This is not a subject
that will be considered further in the book so you may wish to find out more
yourself.
Line 7 - This
line contains the opening brace of the main function. There must be a closing
brace to match every opening brace and vice versa. The closing brace is on line
19.
Line 8 - This
line starts with a '//' This indicates that it is a comment. Comments are
ignored by the compiler and are only to help you or others to understand what
the code does. Use them as much as possible you will be glad of it one day.
Comments beginning with '//' are single line comments and end at the end of the
line. There are also comments which start with '/*' this style of comment end
with a matching '*/' and may cover several lines.
Line 9 - This
line uses the the cout class from the iostream library. cout is designed to
output strings to the console. It does this using the '<<' operator. Several
strings can be sent to cout by seperating each one with '<<'. The statement endl;
at the end of line 9 means 'end this line and send any further output to a new
line' this is equivalent to inserting '\n' within the string. Both cout and endl
are part of the std namespace and would require you to write std::cout or
std::endl if not for line 4.
Line 10 - Is a
comment
Line 11 - The
same as line 9
Line 12 - Is a
comment
Line 13 - The
same as line 9
Line 14 - Is a
comment
Line 15 - Is a
comment
Line 16 - The
function system("PAUSE") is used to pause the program until a key is pressed.
The reason for this is that under windows the console would open, the text would
be output to it then it would close. You would see little more than a flash on
the screen as this happened. Try commenting out the line to see this happen.
Line 17 - Is a
comment
Line 18 - The
return statement is used at the end of functions to return an item of data. This
item of data should match the return type declared at the start of the function.
We are in the main function and as you can see in line 6 this should be an
integer. The return statement marks the end of the function any code after this
will not be executed your compiler will usually warn you of this when you
compile the program. The value defined by EXIT_SUCCESS is a value that tells the
operating system that the program has terminated normally.
Line 19 - This
line contains the closing brace of the main function.