C++ Tutorial

Beginning C++




    Every program in C++ has at least one thing in common.  If it is to be run as an executable it always has a main function.

    The function main has a very strange appearance, and does not always appear the same.  It has several forms, all of which have benefits and penalties.  They are:  (Descriptions will follow...)

    int main() { return 0; }

    int main(int argc, char ** argv) { return 0; }
       -- this is technically the same as the above description, though it is more widely accepted as being clearer --
    int main(int argc, char * argv[]) { return 0; }

    int main(int argc, char *argv[], char * env[]) { return 0; }
        -- this is technically the saame as the above description, though it is more widely accepted as being clearer --
    int main(int argc, char ** argv, char **env) { return 0; }

    Any of the forms of main() can also return void.

    I will cover all of these in turn, but for right now, we are only going to use: int main().  This is simply because it is easiest to deal with.

    So, what do I mean by 'int main() { return 0; }'?
       'int' is the return type of the function.  This is what the function (main in this case) returns to whoever called it.  For main, this almost always an int.  Why?  This is mainly for the reason that this value is what the program will return upon completion.  If it is an int, then scripting tools can be used to determine if an error occured, and other general diagnostic type data.
        So, why 0 as the value with it?  This is called a parameter to an operator.  Also known as an argument.  Basically it is what is given to return, so that return knows what to do.  For right now though, you really don't need to know anything else about it.
 

    Given the above, so now we have to write a program.  So how do we do that?

Well, if we use the above as a template, then we can see that all we need is to copy that and paste it here:

int main() { return 0; }

Now we are going to add just a little bit to it.  Here we want to output a message.  We want to say "Welcome!!", so we can easily add this in here.

int main() {
  "Welcome!!"
  return 0;
}

Unfortunately, this won't quite work, so we have to add some stuff to it.  As it is, C++ would not know what to do with "Welcome!!", after all, it will only do what we tell it to, and we have not told it anything, except whatever "Welcome!!" means. So, we have to tell it that we want to do something with "Welcome!!", but what?
    We want to put "Welcome!!" into (<<) the output, which C++ has named for us, as cout.  Our revised program therefore looks like:

int main() {
    cout << "Welcome!!"
    return 0;
}

    Unfortunately, it still does not know enough to do something.  Just as we end sentences with periods, or some other mark, so must we tell C++ where something ends.  C++ does not think that a newline (the reason that return and cout are on different lines (the carriage return)) is a valid separator, and according to C++, the only separator that it allows is the ';' character.  So we can quickly fix our program to include this,

int main() {
    cout << "Welcome!!";
    return 0;
}

    This will actually compile, well almost...  Unfortunately, I told a small fib previously, C++ does not actually know about the screen, and requires something to tell it about the screen.  This is the line: #include <iostream>
This line will tell C++ all about the screen, which is called cout and about how to get input too, which is called cin.  So adding this to our program, and now it compiles...
Though there is one more little thing.  I told you that C++ just like anything else that tells a computer what to do, only does what it is told.  We have only told the program to put the string "Welcome!!" in the output (which will then be output to the screen).  Though this did not tell the program to put that only on the line, and so we end up with:
    Welcome!!morrison %
    ^^^^^^^^^|^^^^^^^^^
                |                  my prompt!!
                |
     the output from the program
    So to fix this, we have to tell the program to not only output Welcome!!, but to also output a "newline", which the iostream header (what #include <iostream> refers to) calls endl. Unfortunately:
    cout << "Welcome!!" endl;
is not legal, because "Welcome!!" and endl are not the same type of thing.  We have to tell it to put Welcome!! in the output, and then to also put endl in the output.  This is done by:
    cout << "Welcome!!" << endl;
So, now if we look at the entire program, we have:

#include <iostream>
int main() {
  cout << "Welcome!!" << endl;
  return 0;
}

Which will compile and put:
Welcome!!
as the output.
    The normal example, and something you will forever hear about is the "Hello World" program, and for this program, all that is done is to replace "Welcome!!" with "Hello World".  This is largely regarded as the simplest program.