CSCI261C/E Lecture 2: C++ Fundamentals August 29, 2011 Programming - - PowerPoint PPT Presentation

csci261c e
SMART_READER_LITE
LIVE PREVIEW

CSCI261C/E Lecture 2: C++ Fundamentals August 29, 2011 Programming - - PowerPoint PPT Presentation

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?


slide-1
SLIDE 1

CSCI261C/E

Programming is cool!

Lecture 2: C++ Fundamentals August 29, 2011

slide-2
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
SLIDE 3

(whatever)

Atribution: Wired Magazine

slide-4
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
SLIDE 5

Review

We declare constant “facts” using the const keyword.

slide-6
SLIDE 6

Review

  • We compiled and ran a program
  • But... “what happened?”

Attribution: MCA Records

slide-7
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
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
SLIDE 9

Concept #0

“translate to computerese”

Compile, link and execute.

(“run” not “kill”)

“compile to machine code (binary)”

slide-10
SLIDE 10

Big Picture

source code compiler

  • bject file

source code compiler

  • bject file

linker libs executable

main.cpp testApp.cpp OpenFrameworks stdio string main.o testApp.o helloWorld.exe

slide-11
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
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
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
SLIDE 14

What’s an Object File, Really?

Structured encoding of one .cpp file. Six segments.

  • bject file

header text segment data segment

relocation information

symbol table debugging info

main.cpp’s instructions main.cpp’s data

slide-15
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
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

  • Go! Go!
slide-17
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
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 19
slide-20
SLIDE 20

C++ Program Structure

preprocessing directives int main() { variable declarations; statements; return 0; }

slide-21
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
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
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
SLIDE 24

Concept #1

We need to declare facts about the world to the machine. Facts that can change over time? Variables.

slide-25
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

  • Why? To store values
slide-26
SLIDE 26

Data Types of Variables

  • int
  • double
  • char
  • boolean
  • string (technically, not a standard type)
slide-27
SLIDE 27

...to be continued

slide-28
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
SLIDE 29

Input / Output

aka I/O

#include<iostream>

slide-30
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
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
SLIDE 32

Homework

  • Read Chapter 2
  • Complete 02_sphereVolume