SLIDE 1 CSCI261C/E
Programming is cool!
Lecture 2: C++ Fundamentals August 29, 2011
SLIDE 2 Bjarne Stroustrup
This is not what I mean by cool. Note the white jeans.
http://en.wikipedia.org/wiki/Bjarne_Stroustrup
SLIDE 3 (whatever)
Atribution: Wired Magazine
SLIDE 4 Huh? Wha? (review)
- computer system = hardware + software
- programming = describing algorithms to a
computer
- abstraction = don’t worry about the details
(as long as it works)
- we speak english, computers speak binary
- programming languages bridge the gap
SLIDE 5
Review
We declare constant “facts” using the const keyword.
SLIDE 6 Review
- We compiled and ran a program
- But... “what happened?”
Attribution: MCA Records
SLIDE 7 We “talked” to the machine by giving it commands. (a program*)
* Cool kids call this code. (Always singular unless you’re speaking lolcat.)
SLIDE 8
Concept #0
We talk to the machine through programs (“code”). The machine understands and follows our commands. But, it has to “translate” our program into “computerese.” Why? We speak in English, computers speak in binary.
SLIDE 9 Concept #0
“translate to computerese”
Compile, link and execute.
(“run” not “kill”)
“compile to machine code (binary)”
SLIDE 10 Big Picture
source code compiler
source code compiler
linker libs executable
main.cpp testApp.cpp OpenFrameworks stdio string main.o testApp.o helloWorld.exe
SLIDE 11 Implement
- You write a program in a “programming language”
- The language is easy for the machine to read
- ... and easy enough for humans to read & write
- (some languages are more “human” than others)
For example, Ruby!
SLIDE 12 Compilation
- Visual Studio contains a compiler
- (shy and likes to hide behind )
- Reads the C++ source file
- Translates it into machine code
- Produces an “object file”
SLIDE 13 What’s an Object File?
- Contains machine instructions, data
- Typically never directly executed
- Destined to be combined with other object
files into a “program” or “executable”
SLIDE 14 What’s an Object File, Really?
Structured encoding of one .cpp file. Six segments.
header text segment data segment
relocation information
symbol table debugging info
main.cpp’s instructions main.cpp’s data
SLIDE 15 Linking
- Your program relies on other source files
and libraries (if not, it’s probably boring)
- The linker combines multiple object files
- Produces an executable program
SLIDE 16 Execution
- Executable programs first reside on disk
- To “execute” a program means to:
- Load the executable file into memory
- Tell the computer where the first
instruction is
SLIDE 17 Your Program in Memory
Stack Dynamic Data Static Data Text (Instructions)
Your final program’s instructions Your final program’s data
}
SLIDE 18 What to Remember
- We write instructions and declare data in .cpp files
- .cpp files get compiled to object files
- object files get linked and become an executable
program
- execution means your program is loaded into
memory and the computer is carrying out your instructions
SLIDE 19
SLIDE 20
C++ Program Structure
preprocessing directives int main() { variable declarations; statements; return 0; }
SLIDE 21 Preprocessing Directives
Things (aka ‘libraries’) your program will need to use. eg, math functions, input/output, graphics #include <iostream> #include <cmath>
“Hey computer, my program uses functions in the iostream library and the math library.”
SLIDE 22
main()
Is special -- all programs start with main. What does your program do? Whatever is in the code block following main().
SLIDE 23 Code Blocks
{ the stuff in between braces } The guts of your algorithm
int main() {
/* computer, what should I do with my life? */ return 0;
}
SLIDE 24
Concept #1
We need to declare facts about the world to the machine. Facts that can change over time? Variables.
SLIDE 25 Variable Declarations
- A computer is dumb
- All variables have a data type
- Why?
You need to tell the computer ahead of time about the memory you will need
- Why? Because variables need
memory
SLIDE 26 Data Types of Variables
- int
- double
- char
- boolean
- string (technically, not a standard type)
SLIDE 27
...to be continued
SLIDE 28
Concept #7
We need to communicate with the machine while our program is running. We want the machine to generate a response. “Put stuff in, get stuff out.”
SLIDE 29 Input / Output
aka I/O
#include<iostream>
SLIDE 30 cout
“the screen” aka “character output” or “standard output”
cout << “does he/she like me?”; cout << “does he/she like me?” << “or looove me?”;
SLIDE 31 cin
“the input prompt on a screen” aka “character input” or “standard input”
cin >> answer;
“Computer, take what the user types when they hit [enter] and assign it to the variable following the >> symbol”
SLIDE 32 Homework
- Read Chapter 2
- Complete 02_sphereVolume