• Game Rebels: The site that rewards you for posting
  • Join The Awesome ARK Server!

    Island PVE (Official Rates x2) by Vurh.Club
    Join ARK Server! Join ARK Discord!
  • The Game Shop V2!

    A Complete revamp! Hundreds of FREE games, now available!
    Shop Now!
  • Game Rebels Community

    Welcome to this awesome gaming forum
    Register Now
  • Earn Points to Buy Games!

    The more you post, the more you earn
    Learn More

C++|Lesson 1|The basics

Discussion in 'Coding Tutorials' started by Fuzed, Jul 3, 2012.

  1. Fuzed

    Fuzed Active Member

    Right so now you have your compiler, Code::Blocks, setup we can now move on to coding - for now we are just going to get familiar with some basic code.

    Ok so lets start with something simple, a simple Hello World (the classic first app).
    // my first C++ program
    #include <iostream>
    using namespace std;
    int main ()
      cout << "Hello World!";
      return 0;

    Right so lets look at the code in more detail:
    // my first C++ program
    This is just a comment line any line started with two slash signs // are called comments - they do no not influence the program in anyway but they can help coders understand what's going on where. Comments are especially useful in large projects with lots of code - it can be difficult to see what does what or remember where something is.

    #include <iostream>
    Lines beginning with a hash sign # are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

    using namespace std;
    Namespace is a standard C++ library which contains various elements which will help define parts of your program(s). The line is very frequent in C++ programs that use the standard library, there are many other libraries available and they have their own purposes and functions - for now the standard library is sufficient.

    int main ()
    This defines the function - in our case it defines the main function (hence the name of main also). The main function is the point at which the program starts its execution, independently of its location within the source code. It does not matter where this fucntion is located as it will always be executed first as the main function, this is why it is required to have a main function in your C++ program.
    The word main is followed in the code by a pair of parentheses () - brackets. That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.
    Right after these parentheses we can find the body of the main function enclosed in braces {}. What is contained within these braces is what the function does when it is executed.

    cout << "Hello World!";
    This line is a C++ statement. A statement can be a simple or compound expression that effects the program.
    cout is the name of the standard output stream in C++, the command is pre-defined and so all is left to do is to add the text you require to be displayed - as long as you keep the characters within the "" then you can have anything you like.
    The iostream we declared ealier along with the std namespace defines the cout function - making the simple command readbale by the computer. They act similar to dictionaries for the computer - allowing the computer to understand what functions do.
    Also notice the ; that ends the statement. This character is used to mark the end of the statement and must be included at the end of all expression statements in all C++ programs (the source of the most common syntax errors - forgetting the .

    return 0;
    The return statement causes the main function to finish, in our case the main() function. By returning 0 we are just telling the computer that the program has finished without errors, instead we could replace 0 with another function to run mupltiple commands after one another. However for now we shall stick to the basics as this is the most usual way to end a C++ console program.