Course Logistics

Who is your instructor. Who is your section leader. Role of the website. Course Policy and CS Department Honor Policy. Computing resources.

What is this course about?

This course is an introduction to programming. C++ is the language we use, but this course is not only about learning C++.

It is about learning to program!

"How to program" is bigger than "C++" or any other language. Programming is a creative process, in which you construct a model world inside the computer that interacts with the real world via keyboards, mice, monitors, speakers network connections, and so forth. It's very rewarding and exciting; you will never be bored!

This course focuses on solid understanding of foundational concepts, so please be patient with the fact that your programs will usually be reading and writing text and not interacting with all the fancy peripherals mentioned above.

Words of Advice

The structure of a simple C++ program

The example below shows the structure of a very simple C++ program. It's annotated to describe the different parts of the program.

#include <iostream>




using namespace std;





int main()
{
 
  cout << "The answer is ";
  cout << 7*6;
  cout << endl;



  return 0;
}
← This "includes" code needed to use the "standard library" iostream.
  Libraries are repositories of compiled C++ code that we can use in
  our own programs. iostream provides input/output functionality.

← This allows us to use shorter names for standard library  objects. 
  For example, without this line we have to  write "std::cout" rather
  than simply "cout".  The line is saying "just assume the 'std::'
  part".

← The "main" function.  For now, all our C++ code goes in between
  these { }'s.

← "cout << x" is how we write output.  The right-hand side can be 
  almost any expression. "std::cout" is the full name of the output
  stream associated with the terminal.  "cout << endl" sends a
  newline to the output stream.  

← A program actually returns a number to the command shell that
  launched it.  Returning 0 is like saying "this program exited
  normally".

Some more comments follow:

Compiled vs interpreted, and why C++ is different than Javascript

In SY110, all of you got exposed to Javascript programs. You'll notice that what we do here feels a lot more low-level, and requires a lot more discipline.
Step 1: Source code writing.

Use an editor to create the file hello.cpp containing the source code for the program.

$ g++ hello.cpp -o hello
$


Step 2: Compilation.

Use a compiler to translate the human-readable C++ source code into a binary (machine-readable), executable program hello. We will run the compiler by executing the left command on the terminal. In particular,

  • g++ is a binary executable program that serves as a compiler.
  • hello.cpp is the source code we wrote in Step 1.
  • -o hello specifies the filename of the output binary program.
$ g++ hello.cpp -o hello
$ ./hello 
Hello World!
$ 
Step 3: Execution.

Use the OS to initiate execution of (i.e. start the fetch-decode-execute cycle on) the program hello. In particular,

  • Note that hello is the binary program that the compiler created in Step 2.
  • To execute it, we prepend ./ (i.e., dot slash), which tells the terminal that the file is under the current directory.
  • The binary program printed "Hello World!" just as we wanted!

All these steps are kind of a pain in the neck, but remember: we're writing a program that is executing on "bare metal", i.e. directly on the CPU.

Practice Problems

At the end of the notes for almost every lecture will be a couple of example problems with solutions. Everyone likes to see examples of code, so that's what we give you!

Do you know ...

Compiling
http://xkcd.com/303/
The section will ask you if you understand important points of the lecture.