Tuesday, August 30, 2016

Getting good at: C++ | #2 - Hello World Program

Just before we start...

Okay, so I posted this like 5 minutes ago and already has like 25 views. wat

2 - Hello World

Okay, did you survive the first post? Good. I'm never doing that again. Anyway, you're probably wondering "oi ow do u make a program if u dont ell me im the sikest bl0ke ul ever met il call up my m8s an were gonna W1N$T0N u i swear on me mum il rek u so hard r00nscape 1v1 after sk00l". The first thing you'll see after opening up Code::Blocks is this:

Select "Create new project", "Console Application" (for now), "Next >", "Next >", then name your project and make a folder for your .cpp files. After that, click "Next >" and "Finish". Now, after you're done, you should see something like this:


This is a simple "Hello World" program that loads automatically. Like, right after you create the project. So this it how it works:

#include <iostream>

#include is not a Twitter hashtag, rather, it's what allows you to refer to other classes and objects (Often in the form of a ".h" file). For example, let's say that you're writing a program that refers to many different classes. To refer to all of them, you would need to write something like this:

#include <iostream>
#include <string>
#include <"stdio.h">
#include <"dankmemes.h>
#include <"urmum.h">


using namespace std;

No, this doesn't stand for Sexually Transmitted Disease. In C++, std
stands for "standard". In most cases, you would need to include a prefix  to let C++ know that you're using the standard library with your programs.  "using namespace std;" means that your program won't look like this:

#include <iostream>

int main()
{
    std::cout << "end it kid" << std::endl;
    return 0;
}

In other words, you don't have to type as much.

int main()
{
}

This is your main variable. Without this, your compiler's gonna have an aneurysm.

Okay, that's not entirely true, but your program won't compile without it. The empty parentheses are called "parameters". Parameters store values which are passed into functions. The empty curly braces are where your function is stored. A function is the "meat and potatoes" of your program. It's here where we're gonna write the command that will print "Hello World" on a screen. If you still don't understand, here's an example:

int main()
{
    cout << "end it" << endl;
    return 0;
}

"cout" is what allows you to print stuff out on a screen. When you use "cout", you must use << and give it something to print. NOTE THAT IT'S CRUCIAL TO INCLUDE SEMI-COLONS. Return 0 tells the compiler that the program has nothing to return, which means that your program ran successfully.

That's really it for now. Until next time...




5 comments:

  1. Wow, nice work. Personally, I am a big fan of your writing. It is very technical, and more importantly, personal. Your voice really shines through, and that shows your skill as a writer. However, be conscious of your language choice. Some things may offend some readers, which isn't good for developing an audience, keep this in mind in the future. Overall, awesome job. Try to keep working on developing your voice!

    Mr. Hall

    ReplyDelete
    Replies
    1. Also, I know this is a personal blog and it is on the Internet, but we weary of using choices in your language such as "git gud". That is not quite what we are looking for here! Keep in mind that you can keep your voice in your writing and still spell correctly!

      Delete
    2. This comment has been removed by the author.

      Delete
    3. I'm glad you enjoyed it. However, I'm unable to change the title because it would throw readers off. Plus, this is a satirical blog, so of course people won't take it seriously. The language choices are not meant to hurt or offend anyone, and in the event that they do, I will submit a response post explaining why I posted it, and how it shouldn't offend anyone. Nevertheless, thank you for the suggestion.

      Regards,


      Control Alt Defeat

      Delete