Course Logistics
Who is your instructor. Who is your section leader. Role of the website.
Course Policy and CS Department Honor Policy. Computing resources and
course VM.
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. You'll apply your programming skills
to fun graphic stuff next semester: this semester we focus on programming
itself.
Words of Advice
- Make friends with frustration.
Although it's exiting to write programs, it is frustrating at the same time.
- With probability 99.9%, 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.9%, 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.
- Well, actually, you don't know for sure whether the program works
correctly. What you know is that the program works correctly on
several tested cases. There may be several tricky cases in which
your program will crash, or your program may be have some vulnerability
that may be exploited by hackers!
In summary, frustration will always be with you, so welcome it with your
open arms. Remember that you are learning something new, whenever you get
frustrated.
However,if you think you're too frustrated, you must seek
help. It's your responsibility to seek help. It's our responsibility
to help you with all our might when you seek help.
- Know that you will make a lot of mistakes.
As mentioned above, for each program, you will experience success only once;
the rest of more than 100 times, you will be wrong. There is nothing wrong to
be wrong when you write code; you just need to get it correct in the end.
What matters is whether can you learn from failures and fix them.
Compiled vs interpreted, and why C++ is different than Javascript
You really should review the computer architecture section of SY110.
Here's a link to
the SY110
site.
In SY110 last year 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. The main reason is that Javascript programs are
executed by the browser, whereas C++ programs are actually executed by the
physical machine. That fundamental fact leads to a lot of the differences
you'll observe.
-
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.
|
|
Step 2: Compilation.
Use a compiler (g++ in this case) to translate the human-readable
C++ source code into machine-readable object code
hello.o .
|
|
Step 3: Linking.
Use a linker (g++ does this as well) to combine our object file
hello.o with other object files (like those that take care of
input/output) to create the executable program hello .
|
|
Step 4: Execution.
Use the OS to initiate execution of (i.e. start the fetch-decode-execute
cycle on) the program hello .
|
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.
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?