C++ for beginners, part 1

Welcome to the first day of class! A class with absolutely no prerequisites.  Learning a programing language can be daunting, but just remember: 3 days ago, I’d never written a single line of C++ code, so you are on almost exactly the same page as me. Even if I wanted to overwhelm you with high-level concepts and jargon, I couldn’t, because I don’t know enough to do so. I’ll be gentle.

The general plan is: for each post about solving a Project Euler problem in R, there’ll be a post for C++. R is a simple-enough language that there’ll be enough space in those posts to cover the math and fun problem-solving. The C++ posts will need that space for explaining subtleties of the language, and caveats, especially as I’m also learning this as I go. Like I said, we’re on the same page here.

The difference between interpreted and compiled languages

R is an interpreted language, which means that the code you’ve written (source code) is interpreted line-by-line as you enter it, and it performs those instructions in that order, right then and there. A compiled language like C++, takes the source code, then translates it into executable machine code, which you can run like any other .exe file. So I can write some code in C++, compile it, and give you the .exe file. You will be able to run it on your computer without you needing any fancy programs. Like this! (That will take any two integers, and sum up all the numbers in that range, like we did in section 3 of  this post).

Obviously this means that with R, you can change variables and add/remove data points on-the-fly, which is great for working with large datasets where you need trial and error before deciding your next move. And due to the line-by-line nature of it, you can work out the bugs as you go along, as well as build and test components in any order you want. With C++, you don’t know where the bugs are until you’ve compiled the whole thing. But it’s a lot better for bigger projects (R gets used for stats, C++ gets used to make video games), even though it’s more effort to learn and use. I’m not gonna explain why (partly because I promised not to overload you with jargon, and partly because I’ve understood only a tiny fraction of what I’ve read on the subject), so paraphrasing another article:

While you may spend more time writing and fixing code than you do optimizing the code, you have the power to optimize it a hell of a lot better. Build x number of shitty toys in a month and you’re left with a pile of shit. Build 1 amazing toy in a month and everyone will want your toy… ’cause come on, who wants to play with a pile of shit?

So, to write C++, you’ll need a compiler. I’ve been using Code::Blocks, and it hasn’t given me any trouble at all. And here’s a friendly guide for downloading and setting it up. With that done, go to File, New, Project… Console Application. By default, new projects in Code::Blocks automatically contain the ‘Hello World!’ program, which is pretty much customary for learning any new language. You can usually tell a lot about a language based on what it takes to write ‘Hello World!’. For comparison, let’s check out R first.

The Code

Pic1

Wow, way too easy. No thrill of the chase there. How’s it work in C++?

Pic2

Yeah, only one of those lines looks like it’s supposed to be there. No prizes for guessing which. Anyway:

  1. This line is an instruction telling the compiler to include ‘iostream’, which contains the code necessary for input-output functions  (because we will be using ‘cout’, which is like ‘print’). Before the code compiles, the preprocessor loads these, so that the compiler will know what to do when it reaches ‘cout’.
  2. Telling the compiler what namespace you are using. In this case ‘std’ is the standard one, and it’s basically a library of the most common functions. This is to avoid confusion if functions in different namespaces share a name. It’s kinda like agreeing which rulebook edition you’re using before you sit down to play Dungeons & Dragons.
  3. ‘main’ is the main function, and the first thing the compiler actually executes. The empty parentheses () mean that you don’t have to supply the function with any arguments/parameters (compare to one of the R functions). Basically the function doesn’t need to act on any particular number or input. Having ‘int’ at the start defines what type of value the function should return (an integer), on completion of the function. By default, it returns ‘0’ if it worked, or some other number if it failed.
  4. {  Curly braces arrange blocks of code in functions and conditional statements { and you can nest them { as many times as you want! } inside each other } and generally make things easier to read. If you click on that minus sign next to it, it will collapse the code block. } Nice.
  5. Finally! cout is the C++ version of print. << tells it what to output, in this case Hello world! Strings of words/letters go in quote marks. Then it outputs endl, which is obviously end-line. This is followed by a semicolon which ends a command (and basically goes after every line except brackets and parentheses. you’ll pick up an intuition of when to and when not to use it).
  6. return 0; is actually unnecessary. I think this may be from days gone by (because we stated that main() should return an integer). Take the line out, and the code will return 0 when it works anyway (see below).

Give it a go:

Pic3

Pic4

That’s it. I realize this was a boring entry, but it’s a necessary starting point. Next entry I’ll show what the R functions for problem 1 look like in C++, that’ll be more interesting and useful. Think of this whole learning process as watching a season of The Wire. It’s gonna take a couple of episodes before you’re invested, but it’ll be worth it. And you’ll end up with code you can be proud of.

Advertisements
This entry was posted in C++, Problem 1 and tagged , . Bookmark the permalink.

2 Responses to C++ for beginners, part 1

  1. Elacatus says:

    References to D&D AND The Wire in one post? You really know how to push my buttons.

  2. hercles says:

    No love for Archer, then? And I was so close to the pop-cultural hat-trick…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s