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
- Know that you will make a lot of mistakes.
- With probability 99%, your code won't work at one shot;
you need to debug your code, figuring out where you were wrong.
- Again unfortunately, with probability 99%, the debugging won't be
finished at one shot. After several trials (sometimes, tens or even
hundreds of trials, depending on the program size), your program will
finally work.
In a way, with each program, you'll experience success only once,
while the other hundred attempts may result in errors. There's nothing
wrong with being wrong when writing code; what's important is getting it
right in the end. What truly matters is whether you can learn from your
mistakes and correct them.
- Make friends with frustration.
Writing programs can be fun, but the inevitable mistakes along the way can
sometimes lead to frustration. Instead of resisting it, embrace frustration as a
part of the learning process. Every time you feel frustrated, remember that it's
a sign you're learning something new.
Tips:
If you're stuck, remind yourself of the basic techniques and look for anything
you might have missed. Reading the relevant course notes and reviewing the code
examples can be particularly helpful in this process.
- If you're still stuck, don't hesitate to seek help.
Schedule EI sessions with your instructor or attend MGSP sessions. It's your
responsibility to ask for help, and it's our responsibility to provide the best
support we can when you do.
- Manage your workload effectively.
As you adjust to the rhythm of this course, you'll find that the difficulty
level is usually manageable. The real challenge will be handling the
continuous stream of assignments.
- Homework will be assigned after every class.
- Weekly lab work is required.
- There are three major projects throughout the course.
Falling behind can make it very hard to catch up. Develop a consistent
system for staying on top of your work. Pulling all-nighters before
deadlines is not an effective strategy.
- Beginner's Creed.
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.
-
Javascript is an interpreted language.
This means that the code you
write is read and processed by the browser, which then executes the steps
demanded by that code.
-
C++ is a compiled language.
As shown in the figure below, this
means that the code you write is read and processed by a special-purpose
program called a compiler, which translates it into the zeros and
ones that the CPU speaks, and stores it in an executable file. At some
later point, the OS is told to start the program stored in the executable
file running, and the CPU initiates its fetch-decode-execute cycle on the
code contained in that file.
 |
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 ...
The section will ask you if you understand important points of the lecture.
- How are Javascript and C++ different?
- What are four steps you need to go through, when you write a program
with the C++ programming language?