troll batul How might you model a battle between two trolls? How - - PowerPoint PPT Presentation

troll batul how might you model a battle between two
SMART_READER_LITE
LIVE PREVIEW

troll batul How might you model a battle between two trolls? How - - PowerPoint PPT Presentation

CSCI261 Lecture 23: Passing by Reference, Objects and Memory troll batul How might you model a battle between two trolls? How might you model a battle between two trolls? Define and describe trolls (attributes, behavior) Describe the battle


slide-1
SLIDE 1

CSCI261

Lecture 23: Passing by Reference, Objects and Memory

troll batul

slide-2
SLIDE 2

How might you model a battle between two trolls?

slide-3
SLIDE 3
slide-4
SLIDE 4

How might you model a battle between two trolls? Define and describe trolls (attributes, behavior) Describe the battle process (to the death!) Define who wins (who is left standing)

slide-5
SLIDE 5

Declaring functions in external files

slide-6
SLIDE 6

Header & Implementation Files

#include <cstdlib> #include <iostream> #include <string> using namespace std; int main() { // ... cout << "The volume is " << volume(20, 30, 40); return 0; } #include “volume.h” #pragma once int volume(int h, int w, int d); #include “volume.h” int volume(int h, int w, int d) { return h * w * d; }

volume.h volume.cpp

slide-7
SLIDE 7

Header & Implementation Files

Header file (.h) contains the function prototypes. Implementation file (.cpp) contains the definitions.

slide-8
SLIDE 8

Header & Implementation FIles

#include “ships.h” int main() { // cool stuff }

int speed(int thrust);

main.cpp ship.h

int speed(int thrust) { return thrust * 10; }

ship.cpp (compile)

1010 0010 0010 1010

coolgame.exe

main

(compile)

  • bject files

(link)

slide-9
SLIDE 9

Passing arguments by reference

slide-10
SLIDE 10

What? Why?

int free_beer(int beers); int main() { int beers(10); cout << beers; cout << free_beer(beers); cout << beers; // ... } int free_beer(int beers) { cout << beers; beers = 0; cout << beers; return beers; }

Arguments are passed by value...

main()’s beers

f()’s beers

10 10 10 10 10

a copy is made f()’s beers is 10 passed to f()

slide-11
SLIDE 11

...or by reference

int free_beer(int& beers); int main() { int beers(10); cout << beers; cout << free_beer(beers); // ... } int free_beer(int& beers) { cout << beers; beers = 0; cout << beers; return beers; }

main()’s beers

f()’s beers

10 10 10

Your new friend: &

slide-12
SLIDE 12

makeZero

void makeZero(int n) { n = 0; } void makeZero(int &n) { n = 0; }

3

x

makeZero(x); the value of x is “copied” and passed to makeZero as n

3

x

3

n n makeZero

3

x

makeZero(x); a reference to x itself is passed to makeZero as n

x

3

n n

slide-13
SLIDE 13

Troll fred(“Fur-red”); fred

name => “Fur-red” height => 20 weight =>376

fred

MEMORY

slide-14
SLIDE 14

What? Huh?

In general, always pass objects by reference.

slide-15
SLIDE 15

Homework

  • Read Etter 5.3
  • Complete 29_trollBattle

troll batul