fbpx
I’m a big fan of starting out simple and building up from there. And it’s harder to get any simpler than a “Hello, World!” application. Even here, if you really think about it, there’s a lot going on. So I decided to annotate this for you. And while doing so, tried to add just a little bit more code than your typical “Hello, World!” application so that you can see a little better how things relate.

For example, the main method actually has multiple forms it can use. Seeing them both together helps to understand the differences. And the same thing with displaying text using cout. I think it helps to understand both displaying text with cout and reading text with cin when you can see both of them used together.

Take the next step and build a simple game

Are you looking for more than just some welcoming text? If you like this sample code, then you’ll really like giving it some action by turning it into a racing game! Learn how you can write a game with just 60 lines of code that lets you steer a race car around a track full of obstacles.

//
// main.cpp
// HelloWorldExplained
//
// This code walks you through a simple application line-by-line
// and explains exactly what everything does. This file is a source
// code file. It contains code written in the C++ programming language.
// The file will need to be compiled first by a C++ compiler which will
// use the instructions in this file to create a running application.
// C++ source code files usually have the extension .cpp to remind you
// that they contain C++ source code.
//
// These are C++ style line comments.
// They don't have to start at the beginning of a line. But everything
// after the double slashes is a comment intended just for you. Comments
// are not part of your running application. They just help you to better
// understand the code. This code uses comments on almost every line.
// A typical source code file will have far fewer comments.

// The #include preprocessor directive lets you bring other code into
// your program. These are called header files.
// Use <> angle brackets around the name for system header files.
// Most modern C++ header file names have no file extension. Header files
// are also source code. But they're different from .cpp files because
// header files must be included like this in .cpp files to be used.
// You can also include header files inside other header files. But they
// still need to eventually be included by a .cpp file to be used.
// If you write your own header files, then use the .h file extension.
// Then, to include your own header files, use "" double quotation marks
// instead of angle brackets around the file name.
#include <iostream>  // Include iostream to use cout and cin.
#include <string>    // Include string to work with strings.

// Methods allow you to wrap up many programming instructions into a single
// bundle so they all get executed together. Each method can have a return
// value and optionally, a set of parameters. The return value has no name
// but does have a type. An int type is an integer and represents a whole
// number. Parameters allow a method to accept information that can be used
// to do different things. Each parameter has a name and a type.
//
// The main method can have different forms. This one lets you refer to
// command line arguments. It's commented out with another type of
// preprocessor directive because an application can only have a single
// main method and we don't need this one now. This version returns an int
// and has two parameters, argc and argv:
// * The argc parameter is an int that tells you how many command line
//   arguments were used to run the application.
// * The argv parameter is an array of pointers to constant char. In other
//   words, it's a collection of C style strings. The [] square brackets
//   mean the type is an array. An array just means that there can be more
//   than one. The * asterisk is used to define a pointer. In the older C
//   language, strings were just pointers to the first character in the
//   string. The C++ language inherits some things from the C language.
//   And const is used to specify that the strings pointed to by argv
//   cannot be changed. They're constant.
#if 0
int main (int argc, const char * argv[])
{
    // The value returned from main is the exit code of the app. This
    // is normally only used by scripts to tell if an app succeeded or not.
    return 0; // The value 0 traditionally means that everything went okay.
}
#endif // This matches the #if directive and ends the section. The entire
       // section will not be compiled because 0 is false.

// This is the simpler version of main. It returns an int and has no
// parameters inside the () parenthesis. The {} curly braces define the
// beginning and ending of the method body. The main method is where your
// application begins. And your app is done when main returns. The main
// method is a method like any other. The only things special about main
// are that there can be only one and the name must be exactly main. If you
// instead try to call this method Main, then you'll get an error when
// building your application because the main method could not be found.
// The C++ language is case sensitive, so main and Main are different names.
int main ()
{
    // Namespaces let you define code that's isolated from code in
    // different namespaces. The main method was not declared to be in any
    // namespace. So to use code in other namespaces, then the other
    // namespaces must be specified. Or you can do this to declare that
    // you intend to use a namespace. This means that you can use code
    // defined in that namespace without always needing to specify the
    // namespace name every time. For example, this lets you use just
    // cout instead of std::cout. Putting the using statement here inside
    // the main method means that it applies only inside the main method.
    // The std namespace is where you'll find all the classes and code that
    // make up the C++ Standard Library.
    using namespace std;
    
    // Send the string literal "Hello, world!" to the console window.
    // Use cout whenever you want to print text on the computer screen.
    // The \n at the end is an escape sequence that represents a new line.
    // A new line means that any text output or typed next will appear
    // on a new line in the console window. You can use escape sequences
    // for things that don't have visible letters.
    cout << "Hello, world!\n";
    
    // Declare a local string variable called prompt of type std::string.
    // You don't have to specify std:: because of the using statement.
    // Initialize the string with the contents of the string literal in
    // double quotes. Literals like this one and the previous are not
    // variables. They're values typed directly into your program that
    // cannot change while the app is running. A variable like prompt
    // can change its value during runtime. Notice there is no new line
    // this time. That's because you're being prompted to enter something
    // and it looks better for the text to appear next to the prompt.
    string prompt = "Type something: ";
    cout << prompt; // You can send variables to cout too.
    
    // Declare another variable of type string called input. This string
    // has no text yet and is empty. The code will fill it with the first
    // word of text you type on the keyboard and will wait for you to press
    // the enter key before returning.
    string input;
    cin >> input; // This is the line that reads from the keyboard. Notice
                  // how the arrows face the other direction than cout.
                  // You can think of them as pointing in the direction
                  // that information is flowing.
    
    // Now that the app has read what you typed and stored the first word in
    // the variable called input, it's time to send it back to the console.
    // Notice how you can send multiple things at once to the console cout.
    // You can also put escape sequences anywhere in your strings. This
    // example starts off with a new line, then prints a message. Then the
    // first word that was read just now will be sent to the console. And
    // finally, the endl expression is another way to send a new line to the
    // output. The endl expression has a side effect of causing the text sent
    // so far to immediately appear on the console. Send two new lines at the
    // end to give some space between the application output and any text
    // displayed by your system once the app exits.
    cout << "\nThe first word you typed was: " << input << endl << endl;
    
    return 0;
}