Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (1)

Powered by Vanilla. Made with Bootstrap.
[C++] Disecting a simple program [basic]
  • Sh3llc0d3
    Posts: 1,910
    Hey guys, again this is pretty basic stuff in C++, I'll hopefully teach you the different parts of a simple program.

    I could have used a simple 'hello world' program as it's known but thought i'd use this one, basically asks you for your name and prints a greeting.
    // ask for a person's name, and greet the person
    #include <iostream>
    #include <string>

    int main()
    {
    // ask for the person's name
    std::cout << \"Please enter your first name: \";

    // read the name
    std::string name; // define `name'
    std::cin >> name; // read into `name'

    // write a greeting
    std::cout << \"Hello, \" << name << \"!\" << std::endl;

    return 0;
    }


    Now let's disect the program...
    Comments
    // ask for a person's name, and greet the person

    This is what's known as a comment, any comment can run from one line to multiple lines, but the '//' denotes that this comment only lasts for one line and on starting a new line will flip back to being C++ code. For multiple lines this would be shown as...
    /* 
    ask for a person's name, and greet the person
    blah
    blah
    */


    Headers
    #include <iostream>
    #include <string>

    Header's include in your program necessary information that you program needs to work. Declaring which ones you need at the start as above is how the program know's which to use when running, this is done with #include. Several are included with your compiler, for example iostream is the header and is included with your compiler. It supports the C++ I/O system.

    Namespace
    There are 3 ways of confirming in a document which namespace you are using.
    ** using namespace std;
    ** std::cout
    ** using std::cout
    I'll give examples of all three:
    1st.
    // ask for a person's name, and greet the person
    #include <iostream>
    #include <string>
    using namespace std;
    ~snip~

    2nd.
    Is shown in the original program...
    3rd.
    ~snip~
    int main()
    {
    using std::cout;
    using std::cin;
    using std::string;
    using std::endl;
    // ask for the person's name
    cout << \"Please enter your first name: \";

    // read the name
    string name; // define `name'
    cin >> name; // read into `name'

    // write a greeting
    cout << \"Hello, \" << name << \"!\" << endl;

    return 0;
    }

    As you can see they differ not so much, they all do relatively the same thing, they declare you want to use 'std' namespace. Most beginner's books I have come across teach the same way, the 1st way. I took this as valid when learning as it is simple and doesn't need repitition. However since originally learning about namespaces i've been made aware that it is not 'good' coding to use such a declaration, instead of declaring each time you wish to use namespace 'std' it applies it to the whole document. This can cause ambiguity or more importantly errors! The short-cut way is to use the 3rd example which applies in code-blocks to only what you declare. However I prefer the long alternative, as I know when writing even the simplest programs if something is not right it will not compile and warning will show up if I have not used the correct declaration.
    It is considered 'Good' practice to use either the second or the third alternatives when writing in C++. as it eliminates any problems from namespaces.

    Functions & data-types
    All programs have one or more functions and must include the main() function. It's functional code is executed with the opening of the '{' on the following line and then the code indents and when finally the code needs ending a closing '}' is shown. In relation to the example program the coding has been taken out of the centre of the code-block but you can cleary see the { }:
    int main()
    {
    ~snip~
    }

    You may have see the 'int' before 'main()', this denotes the datatype of the function which in this case is an integer. C++ supports more data types but this is the one we will be using.

    Statements
    std::cout << \"Please enter your first name: \";

    This is known as a 'console output statement' it get's it's name because it basically gives a command for the console to print "Please enter your first name: " on-screen when the program is run. 'std::' shows using std namespace, 'cout' is the identifier and '<<' is the operator. Whatever is on the right side of the '<<'
    forces it to use the 'cout' identifier and be printed on-screen.
    std&#58;&#58;string name;

    The above statement in this program defines the variable 'name' as a string. A string is a set of character's enclosed in speech marks (" "). later when learning C++ you will find out that:
    ** 'char' means single character
    ** 'string' means multiple char's/characters.
    std&#58;&#58;cin &gt;&gt; name;

    'cin' basically means 'console input', you will notice that the operator is the other way round [>>] to 'cout'.
    In the below statement we are first 'printing' on-screen the word 'Hello, " then with the << operator asking the name variable to print it's contents followed "!" and a new line denoted by std::endl.
    std&#58;&#58;cout &lt;&lt; \"Hello, \" &lt;&lt; name  &lt;&lt; \"!\" &lt;&lt; std&#58;&#58;endl;


    Terminating
    return 0;

    The above closes/terminates the main() function and sends a message to [normally] the operating system telling it that the process/program is closing normally. '0' is returned signifying it is closing normally, other number's mean the process is closing with errors etc. However the 'true' close is with a closing '}' which literally in the code closes the main() function and program in this case.

    So far...
    Right well so far we have defined what header files are needed for the program, opened the code-block with the main() function, asked the person using the program for his/her name, defined a new variable where the answer will be 'stored' (e.g. "name") and we have made sure it is a string datatype as it will be multiple characters, then with the 'cin' identifier stored the entered name into the variable 'name'.
    Last but not least on the final statement we printed the final part of the program, a greeting to the name stored in the 'name' variable and closed the program.

    Notes to consider
    - C++ is in parts, case-sensitive
    - All statements should finish the line with a ';'
    - I personally use a program in linux called Code::Blocks v8.02
    to install type
    apt-get install code-blocks


    Hope this helped at least someone lol
  • Xin
    Posts: 3,251
    Wow nice paper bro you must have put a lot of time into it :)
    Xin
  • Sh3llc0d3
    Posts: 1,910
    Lol yeah took a bit of time this morning, it was good to see if I still remembered it all though :)
  • AWESOME paper bro Thanks :)
  • undead
    Posts: 822
    Nice paper SP ;) good work