Don't like this style? Click here to change it! blue.css

Hello class. Welcome.

We have exactly 3 pomodoros together per class. Let's spend the first one going over the structure of the course, my expectations for you, my gifts for you, and outline our hopes and dreams for the course.

Take a moment to look at my philosophy for teaching.

tl;dr Be empowered. Learn to make fast things.

Class 1: Hello C++

So I want to immediately start with compiling your own code in various places. I want to get you comfortable with running code in many ways so that you aren't defined by your tools.

Fastest way to test a snippet

Often students are filled with a particular type of worry. I don't know how to describe it other than wanting to know that they are doing "the right thing" before actually doing it. Perhaps it's a fear of getting an F morphed into a lifestyle.

I strongly believe that in this field the best way to test if you are doing the right thing is to just do it. You won't kill a kitten by creating a syntax error. I promise. Fail early and often to succeed sooner (and on time).

Just Do It.

MINI-TASK 1

MINI-TASK 2: Create a Repl.it account, change the message, save your file. Observe the URL.

g++, github, the command-line, and cloud9

GitHub/Cloud9 setup I am assuming that you have created a GitHub username in your past. If not then please create a GitHub username and use it to say hi in our course chat window.

Next up, visit http://c9.io and create an account (use your github name) or login if you already have an account.

MINI-TASK A: Fork this GitHub repo. That is, create your own copy of this repo in GitHub by clicking the Fork button.

MINI-TASK B: Create a new Cloud9 Workspace from your fresh repo (there is an input field for the git repository, but it will get cleared if you click any of the other buttons like "custom" or "C++").

MINI-TASK C: Find the terminal and compile hello.cpp with the command g++ hello.cpp

MINI-TASK D: Execute the program with the command ./a.out.

Let's talk about what just happened, you linux stud you.

A GitHub is a way of collecting your code and preserving it from your own mistakes. It is the web version of git an awesome version control system. If you are going to look for a coding job one day then having a github portfolio full of nice stuff is always helpful. You can contribute to open-source projects and show the world "what you got". For more info, just ask me.

B Cloud9 is a very pleasant collaborative work environment. I promote it because when you inevitably have to deal with actually making code for a real machine out there (probably a web server btw) this is what it will feel like. A cloud9 workspace is basically a very pleasant Ubuntu machine created just for you. You interact with the machine through the command line and you can save files like any other machine. In this way we all have a nice uniform play space which you can also co-edit with friends. At its core, coding is the act of creating textfiles and turning them into machine code. IDEs can often hide this from you, so I will emphasize command-line work for your benefit.

C To convert your "high-level" instructions into executable machine code we need to compile it. g++ is the most popular C++ compiler (perhaps you'll write one before you graduate). Its C companion is gcc the GNU C Compiler. (By the way if you are into Meta jokes GNU stands for GNU's Not Unix.)

Hofstadter XKCD Meta Joke

By running g++ hello.cpp we are telling the compiler to turn hello.cpp into an executable.

D When you don't specify the name of an executable on a Linux system the default is called a.out. If you want to run a program you execute ./name_of_program_here. So this step executes the executable.

MINI-TASK E recompile the program with the "flag" -o myProg. That is g++ hello.cpp -o myProg, then execute this new program with ./myProg.

Sharing your work

In this course you will have to share your work with me, the TAs, and your group partners (more about that later). We've seen how to save a snippet in repl.it, and how to create a cloud9 workspace. So how do we exchange code?

I'll give you two new ways (you can always just send files around however you want to do that (dropbox, email, etc.)).

MINI-TASK Z:

MINI-TASK Y:

So, now you can save your code to GitHub as a repo, you can share a cloud9 space, you can email source code, you can compile your code with g++ and you can fork other people's repos.

So smart

Pause for an interview question:

How might you design a program that lets people play Monopoly with each other over the internet?

Pick a group of 3-4 people and discuss it for 5-10 minutes.

Something to think about Task: if someone were to read your source code, would they know the rules of Monopoly?

Diving in to C++

A new sample program

Here is an example of a program which uses a function:

MINI-TASK 100: By looking at the code can you guess what it does? Run it and check. What is wrong with this program's functionality? Fix the issue.

Now that we've run this thing and even fixed it. Let's break down the anatomy. But first:


An aside on Compiling, HeaderFiles, ObjectFiles, Linking

The compiler breakdown

Commands which start with # are preprocessor directives. That sounds fancy, but really it just gives us a way to do some simple stuff at a copy/paste/replace kind of level BEFORE the code is looked at. https://en.wikipedia.org/wiki/C_preprocessor has the basics which include, "including" code, "excluding code" using if/else, and "defining" some macros (replace all instances of some word with some value).

So the line #include<iostream> will include a file named "iostream" with some extension (like iostream.h, iostream.hpp, iostream.hxx, whatever), a header file.

Header files are what allow C++ programs to exist in multiple files. They define the interface for functions and classes that we want to use. iostream.h provides some utility functions that help us deal with Input/Output (hence io) and streams.

When the program compiles it creates objectFiles, that is files ending in *.o, which it will link together into the final executable.

For now we might only use single file programs, but as our projects grow we will want to make nice, clean, modular code. For that we'll need to create header files and use some nice tricks.

If you can't wait to make it that far then check out this useful guide.


Breaking down some code

#include<iostream> will be the only line pre-processed. It will insert the entire iostream header file interfaces into our code.

using namespace std declares that I want to use all of the standard stuff without needing to prefix with std::. That saves me some typing and helps the code look cleaner to you. However pro coders will snub you for using the std namespace. That's because you will inevitably end up writing a module that defines its own functions with the same names. Be warned. Here is the same code without that line:

int main(){ /* stuff here */ }; this is the "main" contents of this program. Whenever a main function exists it will be what gets executed when the program runs. The int word dictates that the function should return an integer. We'll talk about those types more on Thursday.

string prompt_user(string input_prompt){} this defines a function. In this case the function is called prompt_user, takes a string as input and returns a string as output. Inside of the function body it will refer to the input as input_prompt.The function will get called in the future with some statement like prompt_user("input stuff here").

cout << "stuff" << endl; These lines are what display text to the screen. endl adds a newline character to the screen (for me this usually looks like \n but on windows machines this is sometimes \r\n. tl;dr typewriters need two commands one "over" one "up".)

string reply; this is a variable declaration. Much like a function declaration it reserves the word reply and tells the compiler that reply will have the type string (again, we'll look at types and such in more detail next time).

cin >> reply; this line takes input from the keyboard and stores it in the variable reply. (Up to our 'bug'.)

return 0; a return statement immediately ends the program and provides the returned value to whoever called the function. In the case of main this is the operating system. A 0 means everything is OK, other things are problems, but this is all convention.


Alter it

OK, so if we had enough time to make it here then great. If you rushed through all of the explanation that's cool too. Now let's play.

MINI-TASK t2000 alter convo.cpp to ask three questions and then make an elaborate but conversational response from them.