CSCI261C/E Lecture 2: C++ Fundamentals August 29, 2011 Programming is cool!
Bjarne Stroustrup http://en.wikipedia.org/wiki/Bjarne_Stroustrup This is not what I mean by cool . Note the white jeans.
(whatever) Atribution: Wired Magazine
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
Review We declare constant “facts” using the const keyword.
Review • We compiled and ran a program • But... “ what happened ?” Attribution: MCA Records
We “talked” to the machine by giving it commands. (a program* ) * Cool kids call this code . (Always singular unless you’re speaking lolcat.)
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 .
Concept #0 “translate to computerese” “compile to machine code (binary)” Compile, link and execute. (“run” not “kill”)
Big Picture main.cpp main.o source compiler object file code helloWorld.exe linker executable testApp.cpp testApp.o source compiler object file code libs OpenFrameworks stdio string
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!
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”
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”
What’s an Object File, Really? Structured encoding of one .cpp file. Six segments. object file text data symbol debugging relocation header segment segment information table info main.cpp ’s instructions main.cpp ’s data
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
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 • Go! Go!
Your Program in Memory Stack } Your final program’s data Dynamic Data Static Data Text (Instructions) Your final program’s instructions
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
C++ Program Structure preprocessing directives int main() { variable declarations; statements; return 0; }
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.”
main() Is special -- all programs start with main . What does your program do? Whatever is in the code block following main() .
Code Blocks { the stuff in between braces } The guts of your algorithm int main() { /* computer, what should I do with my life? */ return 0; }
Concept #1 We need to declare facts about the world to the machine. Facts that can change over time? Variables .
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 • Why? To store values
Data Types of Variables • int • double • char • boolean • string (technically, not a standard type)
...to be continued
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.”
Input / Output aka I/O #include<iostream>
cout aka “character output” or “standard output” “the screen” cout << “does he/she like me?”; cout << “does he/she like me?” << “or looove me?”;
cin aka “character input” or “standard input” “the input prompt on a screen” cin >> answer; “Computer, take what the user types when they hit [enter] and assign it to the variable following the >> symbol”
Homework • Read Chapter 2 • Complete 02_sphereVolume
More recommend