CS31 Discussion 1E Spring 17: week 10 TA: Bo-Jhang Ho - - PowerPoint PPT Presentation

cs31 discussion 1e
SMART_READER_LITE
LIVE PREVIEW

CS31 Discussion 1E Spring 17: week 10 TA: Bo-Jhang Ho - - PowerPoint PPT Presentation

CS31 Discussion 1E Spring 17: week 10 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju What is new } Dynamically allocate memory } Step 1: allocate memory } Step 2: return the memory address } Don t forget to


slide-1
SLIDE 1

CS31 Discussion 1E

Spring 17’: week 10

TA: Bo-Jhang Ho

bojhang@cs.ucla.edu

Credit to former TA Chelsea Ju

slide-2
SLIDE 2

What is “new”

} Dynamically allocate memory

} Step 1: allocate memory } Step 2: return the memory address

} Don’t forget to return the memory

} delete operator

} Don’t lose the pointer, otherwise you will never be able to

recycle the allocated memory!

slide-3
SLIDE 3

Stack / heap

slide-4
SLIDE 4

Stack / heap

Local variables go here! Dynamic memory allocations go here!

slide-5
SLIDE 5

Local variable v.s. dynamic allocation

} Local variables are easier to manage

} The last showing up variable is going recycled first

} Dynamic memory allocation

} A more flexible way to get available memory! } There is no way to determine when an allocated memory piece

is going to be freed. Developers need to explicitly say which memory have to be recycled.

} Memory fragmentation problem

slide-6
SLIDE 6

An array of pointers

} Scorpion* objects[10];

slide-7
SLIDE 7

Accessing member operator

} Dot (.) operator

} Scorpion scor;

scor.m_row = 3;

} Arrow (->) operator

} Scorpion *ptr = new Scorpion;

ptr->m_row = 3;

} Just treat dot (.) and arrow (->) operator as ‘s

slide-8
SLIDE 8

Constructor / destructor

} Constructor is for class (structure) initialization } Destructor gives the victim a chance to say good bye to

the main program before it gets recycled

} Usually we don’t need to override the default destructor, but if

you dynamically allocate memory in constructor, this is the only chance you can free the allocated memory.

slide-9
SLIDE 9

Constructor / destructor

class Pit { public: Scorpion *sa; Scorpion *sb; int thirdVal; Pit() { sa = new Scorpion; sb = new Scorpion; thirdVal = 10; } ~Pit() { delete sa; delete sb; } };

slide-10
SLIDE 10

Constructor / destructor

class Pit { public: Scorpion *sa; Scorpion *sb; int thirdVal; Pit(); ~Pit(); }; Pit::Pit() { sa = new Scorpion; sb = new Scorpion; thirdVal = 10; } ~Pit::Pit() { delete sa; delete sb; }

slide-11
SLIDE 11

Toward object-oriented

} Treat things as objects, capture interaction of objects } Consider variables in a class as states

} Usually variables are declared as private } Which means beyond the scope of the class, nobody can

arbitrarily change the state

} Consider methods as ways to change states

} To enforce users to change states in a certain ways } Maintain the states, keep them valid

} A method is a function in a class

slide-12
SLIDE 12

What is this

} this is a built-in keyword } this can only be used in a method } this means the owner of the method } this is a pointer } Why do we need to have the keyword this?

} Distinguish local variable from instance variable } Pass myself to others

slide-13
SLIDE 13

What is this

class Test { public: int score; Test(); saveScore(); }; Test::Test() { score = 60; } Test::saveScore(int score) { score = score; }

slide-14
SLIDE 14

What is this

class Test { public: int score; Test(); saveScore(); }; Test::Test() { score = 60; } Test::saveScore(int score) { this->score = score; }

slide-15
SLIDE 15

What is this

Player::Player(Pit* pp, int r, int c) { ... m_pit = pp; ... } bool Pit::addPlayer(int r, int c) { ... // Dynamically allocate a new Player and add it to the pit m_player = new Player(this, r, c); return true; }