Transcript 105

Adapted from Bjarne Stroustrup
www.stroustrup.com/Programming
Prof. Zhang, Spring 2012
Fordham Univ.
Today, we’ll outline the aims for this course
and present a rough course plan. We’ll
introduce the basic notion of programming
and give examples of areas in which
software is critical to our civilization.
Finally, we’ll present the simplest possible
C++ program and outline how it can be made
into running code.
CISC1600, Zhang, Spring 2012
2
Learning
 Syllabus
goals and outline
Programming
“Hello,
world!”
Compilation
CISC1600, Zhang, Spring 2012
3
 In
Programming
 For beginners

who want to become professionals


who are assumed to be bright


i.e., people who can produce systems that others will
use
Though not (necessarily) geniuses
who are willing to work hard

Though do need sleep occasionally, and take a normal
course load
 Using
the C++ programming language
CISC1600, Zhang, Spring 2012
4
A

A
Washout course
“if you can get into the science/engineering parts
of a university, you can handle this course”
course in

The C++ programming language

who want to become language lawyers
 For

students

We try not to get bogged down in technical obscurities

We try not to spoon feed
who are assumed to be a bit dim and fairly lazy
 Using

Some untested software development
methodologies and a lot of unnecessarily long
words
CISC1600, Zhang, Spring 2012
5
 Learn



Fundamental programming concepts
Key useful techniques
Basic Standard C++ facilities




Write small colloquial C++ programs
Read much larger programs
Learn the basics of many other languages by yourself
Proceed with an “advanced” C++ programming course



An expert programmer
A C++ language expert
An expert user of advanced libraries
 After
 After
the course, you’ll be able to
the course, you will not (yet) be
CISC1600, Zhang, Spring 2012
6
 Lectures

Attend every one
 Outside

Read a chapter ahead, and read the chapter again
after each lecture



Read actively: with questions in mind, try to
reorganize/rephrase the key points in your mind
Review questions/Terms in chapters
Drills


of lectures
Always do the drills, before the exercises
Exercises
CISC1600, Zhang, Spring 2012
7
 Assignments

Written homework



Small questions for reviewing key concepts.
Please type and print, and submit hard-copy
Lab projects


That’s where the most fun and the best learning takes place
Don’t wait until lab section to start the project
 Start to think about the project early
 Finish up & get help during labs
 Quizzes

Randomly assigned
 Exams


Midterm
Final
CISC1600, Zhang, Spring 2012
8
Don’t study alone when you don’t have to




Form study groups
Do help each other (without plagiarizing)
If in doubt if a collaboration is legitimate:
ask!



Don’t claim to have written code that you copied from
others
Don’t give anyone else your code (to hand in for a
grade)
When you rely on the work of others, explicitly list all
of your sources – i.e. give credit to those who did the
work
Seek help: tutor’s room, my office hours



Go prepared with questions
The only stupid questions are the ones you wanted to
ask but didn’t
CISC1600, Zhang, Spring 2012
9
 Grading


criteria
No curving
No grade appeal: it’s not fair to others

Notify instructors about your difficult situations if
special accomodation is needed
 Everything
on course website
Assignments, slides, guidelines, …
 Book support website
(www.stroustrup.com/Programming)

CISC1600, Zhang, Spring 2012
10
Next:
CISC1600, Zhang, Spring 2012
11

Faster than humans; not smarter




Executes very quickly a huge number of
primitive operations
Stores data
Interacts with devices
Runs programs to handle different tasks:



Balance checkbooks
Process words, display documents
Play games
CISC1600, Zhang, Spring 2012
12
 CPU:



Central Processing Unit
Consists of a single, or small number of, chip(s)
made of plastic, metal and mostly silicon
Composed of several million transistors
Enormously complicated wiring
 Performs
program control, arithmetic, and
data movement

One operation at a time

Locates and executes program instructions
CISC1600, Zhang, Spring 2012
13
 RAM
(Random Access Memory): read-write
memory
 ROM (Read Only Memory): contains certain
programs that must always be present
 Secondary storage (e.g., a hard drive)
provides persistent storage
CISC1600, Zhang, Spring 2012
14
 Allow
the computer to interact with user
and other computers:






Display
Printer
Mouse
Keyboard
Speakers
Network card
CISC1600, Zhang, Spring 2012
15
Bus: a set of electrical lines that connect the CPU, RAM, slots and other connector
CISC1600, Zhang, Spring 2012
16

electronic numerical integrator and computer
First usable electronic computer, Supported by
Navy, for ballistic calculations
 Before ENIAC, people who did these calculations
were called "computers"
 Designed by J. Presper Eckert & John Mauchly, at
UPenn


Completed in 1946 – 2 years before transistors




Many cabinets, in a large room
18,000 vacuum tubes
Programmed by connecting wires on panels
Later used for tabulating US Census data, etc.
CISC1600, Zhang, Spring 2012
17

Conventional definitions


Telling a very fast moron exactly what to do
The definition we’ll use

Specifying the structure and behavior of a program,
and testing that the program performs its task
correctly and with acceptable performance


Never forget to check that “it” works
Programming is communicating between
programmer and computer

What language ?
CISC1600, Zhang, Spring 2012
18
CISC1600, Zhang, Spring 2012
19
 Machine



instructions: extremely primitive, e.g.
Move memory contents to a register
Subtract 100 from a register
If result is positive, go (jump) to another instruction
 Each
type/family of processor has its own set of
machine instructions
 Machine instructions are encoded as numbers:
161 40000 45 100 127 11280
 writing numeric codes manually is tedious and
error prone

thousands of instructions for a simple program
CISC1600, Zhang, Spring 2012
20
 Assembly



language
assigns short names to commands
Can give names to memory locations
e.g.

mov 40000, %eax sub 100, %eax jg 11280
 Makes
reading easier for humans
 Translated into machine instructions by the
assembler , a system program
 Still processor dependent
 Still a great many instructions
CISC1600, Zhang, Spring 2012
21
 Higher-level
programming language:
 Easiest for humans to read and write:

if( int_rate > 100 ) cout << "Interest rate error";
 Independent


of the underlying hardware
You can write a program in C++, and it can work in
different Unix machine …
There are still some architecture dependence in code
by compilers into machine instructions
 Much stricter than spoken languages
 Translated

Compilers don't like to guess
CISC1600, Zhang, Spring 2012
22
 Initially
Ritchie)

designed in 1972 (Kernighan &
C: developed to be translated efficiently into fast
machine code, with minimal housekeeping overhead
 Features
were added in response to
perceived shortcomings

Resulted in different dialects, bad for
portability
 1989
— ANSI standard of C completed
CISC1600, Zhang, Spring 2012
23

1979 — Bjarne Stroustrup of AT&T adds
object oriented features to C, called C with
Classes
1985 — rename to C++
 1998 — ISO C++ standard published
 2003 — minor revision to the ISO standard
 2009 — major revision expected
 Most commonly used language for systems such
as:

Database
 OS


Growing popularity in embedded systems
CISC1600, Zhang, Spring 2012
24
purpose of a programming language is to
allow you to express your ideas in code

C++ is the language that most directly allows you
to express ideas from largest number of
application areas

available on almost all kinds of computers,
most widely used language in engineering
precisely and comprehensively defined
Programming concepts that you learn using
C++ can be used fairly directly in other
languages




Including C, Java, C#, and (less directly) Fortran
CISC1600, Zhang, Spring 2012
25
 Just
about everywhere
Mars rovers, animation, graphics, Photoshop, GUI, OS, compilers,
slides, chip design, chip manufacturing, semiconductor tools, etc.
See www.research.att/~bs/applications.html
CISC1600, Zhang, Spring 2012
26
Finally,
CISC1600, Zhang, Spring 2012
27
For most of this semester, you will work on
storm.cis.fordham.edu, a Linux based server
through a command-line interface
 Command-line interface is a mechanism for
interacting with a computer by typing commands
to perform specific tasks.

text-only interface
 Different from using of a mouse pointer with
a graphical user interface (GUI) to click on options


Above interaction occurs in a text terminal or in
a terminal emulator window as a remote shell
client such as PuTTY.
CISC1600, Zhang, Spring 2012
28

Terminal runs a command-line interpreter



Called shell in Unix, bash, csh, …
A special system program/command
A dialogue:
Shell waits for user to enter a command
User instruct computer to perform a given task by
"entering" a command
1.
2.
1.
3.
4.
5.
User concludes text command by pressing "Enter" key (a
descendant of "carriage return" key of a typewriter
keyboard)
Shell reads (from keyboard), parses and executes the
command
Output of commands are displayed in terminal
Go back to 1, or finish dialog typing “exit”
CISC1600, Zhang, Spring 2012
29
 General

format:
command_name [options] [arguments]
 command


name is an executable file
System commands: cal, cat, cd, who …
The executable file you generated …
 To
learn more about how to use system
commands:


man who
man ls
 You
will learn basic unix commands in lab1
 First one to encounter: passwd
30
 Next one: emacs
CISC1600, Zhang, Spring 2012
C++ source code
C++ compiler
Object code
Executable program
linker
Library Object code

You write C++ source code using a text editor


text editor to use:



Name the file with suffix .cpp, e.g., lab1.cpp, tictactoe.cpp, …
NotePad on Windows
emacs on Unix system (storm)
Do not use Word, which perform annoying automatic
formatting
CISC1600, Zhang, Spring 2012
31
// …
int main()
{
cout << "Hello, world!\n";
return 0;
// main() is where a C++ program starts
// output the 13 characters Hello, world!
// followed by a new line
// return a value indicating success
}




quotes delimit a string literal
NOTE: “smart” quotes “ ” will cause compiler problems.
so make sure your quotes are of the style " “
\n is a notation for a new line
CISC1600, Zhang, Spring 2012
32
// a first program:
#include "../../std_lib_facilities.h" // get the library facilities needed for now
int main( )
{
cout << "Hello, world!\n";
return 0;
// main() is where a C++ program starts
// output the 13 characters Hello, world!
// followed by a new line
// return a value indicating success
}



note the semicolons; they terminate statements
curly brackets { … } group statements into a block
main( ) is a function that takes no arguments ( ), and returns an
int (integer value) to indicate success or failure
CISC1600, Zhang, Spring 2012
33

It’s almost all “boiler plate”


Boilerplate: any text that is or can be reused in new
contexts or applications without being changed much from
the original, e.g., boilerplate code, legal boilerplate
Only cout << "Hello, world!\n" directly does anything
“Boiler plate,” makes our code simple,
comprehensible, trustworthy, and efficient.
#include "../../std_lib_facilities.h“

int main()
{
return 0;
}
CISC1600, Zhang, Spring 2012
34
C++ source code
C++ compiler
Object code
Executable program
linker
Library Object code


Compiler translates source code into object code (i.e., machine
code, machine understandable)
Linker links your code to system code
System code (or library): input/output libraries, operating system
code, and windowing code
 So that you don’t need to worry about how to read from keyboard …


For now, compile and link using a single command
g++ helloworld.cpp

Result is an executable program, by default named a.out
CISC1600, Zhang, Spring 2012
35
 Execute
the program
Type command in terminal:
./a.out
The ./ is needed to tell system where
to look for file a.out

 An





iterative process:
Edit
Compile
Compilation Errors? Edit file. Compile
Test/Execute the program
Run-time Errors? Edit file. Compile
CISC1600, Zhang, Spring 2012
36
 Get
familiar with programming environment
Login, logout, account setup
 Basic Unix commands
 Emacs editor
 Compiler
 Type in hello world program carefully
 Try make a few mistakes to see how compiler
responds; for example







Forget the header
Forget to terminate the string
Misspell return (e.g. retrun)
Forget a semicolon
Forget { or }
…
CISC1600, Zhang, Spring 2012
37
 Integrated
desktop environments (IDEs) shield
user from compilation details:
 Enter code in a window
 Click a button to "compile"
 Click another to run
CISC1600, Zhang, Spring 2012
38
Express your
idea using C++
compiler translates C++
source into object code
linker takes your object
files and code from various
libraries, outputs an
executable file
(already translated)
code, such as iostream
CISC1600, Zhang, Spring 2012
39
CISC1600, Zhang, Spring 2012
40
 Programming

Just state what the machine is to do
 So

So we don’t always know the implications of what we want
“Programming is understanding”



And computers are nitpicking, unforgiving, dumb beasts
The world is more complex than we’d like to believe


why is programming hard?
We want “the machine” to do complex things


is fundamentally simple
When you can program a task, you understand it
When you program, you spend significant time trying to
understand the task you want to automate
Programming is part practical, part theory


If you are just practical, you produce non-scalable
unmaintainable hacks
If you are just theoretical,
you produce toys 41
CISC1600, Zhang, Spring 2012
 For
me, programming is like a puzzle
because you know what you want the end
product to look like, it’s just about finding
the right pieces.
 To me, programming is like reinterpreting
how people solve problems and using the
tools available to use. It is like translating
from one lagunage to another for the above
reason.
CISC1600, Zhang, Spring 2012
42

For me, programming is like an apple. At times,
it can be difficult to complete, just as an apple
that hangs high on the tree can be hard to
attain. However, if you think step by step, or
climb branch by branch, you can always reach
the apple. A program must be very precise, just
like the perfect apple – there are no bumps or
bruises. If there are errors, they must be
fixed (cut out) before you can run the program
(eat the apple). In the end, a good apply is
always satisfying, and the harder it was to pick,
the better it tastes.
CISC1600, Zhang, Spring 2012
43
 Assignment:

Read chapter 3
 Next
class: types, values, variables,
declarations, simple input and output, very
simple computations, and type safety.
CISC1600, Zhang, Spring 2012
44