Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS31 Discussion 1H Winter19: week 9

Similar presentations


Presentation on theme: "CS31 Discussion 1H Winter19: week 9"— Presentation transcript:

1 CS31 Discussion 1H Winter19: week 9
TA: Behnam Shahbazi Credit to former TAs: Chelsea Ju Bo Jhang

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!

3 Stack / heap

4 Stack / heap Local variables go here!
Dynamic memory allocations go here!

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

6 An array of pointers Scorpion* objects[10]; nullptr nullptr nullptr

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

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.

9 Class Destructor The destructor is called when an object is deleted
It can’t have a return type (not even void) It can’t have any argument Ex: class Pet{ public: Pet(string nm, int initialHealth); void eat(int amt); void play(); string name() const; int health() const; bool alive() const; ~Pet(); private: string m_name; int m_health; }; Pet::~Pet(){ cout << name() << " is destroyed" << endl; }

10 Class Destructor Example 8: What is the output? class Pet{ public:
Pet(string nm, int initialHealth); void eat(int amt); void play(); string name() const; int health() const; bool alive() const; ~Pet(); private: string m_name; int m_health; }; Pet::~Pet(){ cout << name() << " is destroyed" << endl; } int main(){ Pet* myPets[2]; myPets[0] = new Pet("Fluffy", 2); myPets[1] = new Pet("Frisky", 4); for (int k = 0; k < 2; k++){ cout << myPets[k] -> name() << " " << myPets[k] -> health() << endl; delete myPets[k]; }

11 Class Destructor Solution 8: What is the output? Fluffy 2
class Pet{ public: Pet(string nm, int initialHealth); void eat(int amt); void play(); string name() const; int health() const; bool alive() const; ~Pet(); private: string m_name; int m_health; }; Pet::~Pet(){ cout << name() << " is destroyed" << endl; } int main(){ Pet* myPets[2]; myPets[0] = new Pet("Fluffy", 2); myPets[1] = new Pet("Frisky", 4); for (int k = 0; k < 2; k++){ cout << myPets[k] -> name() << " " << myPets[k] -> health() << endl; delete myPets[k]; } Fluffy 2 Fluffy is destroyed Frisky 4 Frisky is destroyed

12 Class Destructor Example 9: What is the output? class Pet{ public:
Pet(string nm, int initialHealth); void eat(int amt); void play(); string name() const; int health() const; bool alive() const; ~Pet(); private: string m_name; int m_health; }; Pet::~Pet(){ cout << name() << " is destroyed" << endl; } int main(){ Pet petA = Pet("Fluffly", 2); Pet *petB = new Pet("Frisky", 4); delete petB; }

13 Class Destructor Solution 9: What is the output? Frisky is destroyed
class Pet{ public: Pet(string nm, int initialHealth); void eat(int amt); void play(); string name() const; int health() const; bool alive() const; ~Pet(); private: string m_name; int m_health; }; Pet::~Pet(){ cout << name() << " is destroyed" << endl; } int main(){ Pet petA = Pet("Fluffly", 2); Pet *petB = new Pet("Frisky", 4); delete petB; } Frisky is destroyed Fluffly is destroyed

14 this Pointer A special keyword that is usable within member functions
It refers to the pointer that points to the calling object Ex: we can rewrite the member functions with this class Pet{ public: Pet(string nm, int initialHealth); void eat(int amt); void play(); string name() const; int health() const; bool alive() const; private: string m_name; int m_health; }; int main(){ Pet *myPet = new Pet("Fluffy",10); cout << myPet -> name() << endl; } void Pet::eat(int amt){ this -> m_health += amt; } void Pet::play(){ this -> m_health --; string Pet::name() const{ return this -> m_name; int Pet::health() const{ return this -> m_health; bool Pet::alive() const{ return this -> m_health > 0;

15 Dynamic Memory Allocation
Memory Leak Not giving back the allocated memory (forgetting delete) It is not always obvious Example 3 int *p, *q; q = new int; p = new int; *p = 45; *q = 55; p = q;

16 Dynamic Memory Allocation
Dangling Pointer Using a pointer to memory no longer allocated Example 4 int *p = new int; int *q; *p = 45; q = p; delete p; *q = 55;

17 Example 6 Q: Which member functions/variables calls are valid?
class Zurt{ public: Zurt() { m_health = m_row = m_col = 0; } Zurt(int health, int r,int c) { m_health = health; m_row = r; m_col = c; int health() const { return m_health; int row() const { return m_row; int col() const { return m_col; void setHealth(int health) { void setRow(int r) { void setCol(int c) { private: int m_health, m_row, m_col; }; int main(){ Zurt z(100,1,1); const Zurt* zp = &z; zp->Zurt(); // (a) zp->Zurt(100,1,2); // (b) int h = zp->health(); // (c) int r = zp->row(); // (d) int c = zp->col(); // (e) zp->setHealth(50); // (f) zp-> setRow(2); // (g) zp->setCol(2); // (h) zp->m_col = 5; // (i) return 0; } Q: Which member functions/variables calls are valid?

18 Solution 6 Q: Which member functions/variables calls are valid?
class Zurt{ public: Zurt() { m_health = m_row = m_col = 0; } Zurt(int health, int r,int c) { m_health = health; m_row = r; m_col = c; int health() const { return m_health; int row() const { return m_row; int col() const { return m_col; void setHealth(int health) { void setRow(int r) { void setCol(int c) { private: int m_health, m_row, m_col; }; int main(){ Zurt z(100,1,1); const Zurt* zp = &z; zp->Zurt(); // (a) zp->Zurt(100,1,2); // (b) int h = zp->health(); // (c) int r = zp->row(); // (d) int c = zp->col(); // (e) zp->setHealth(50); // (f) zp-> setRow(2); // (g) zp->setCol(2); // (h) zp->m_col = 5; // (i) return 0; } To construct an object using a pointer: new Ex: zp -> new Zurt(); Since zp is a pointer to constant data, we can’t modify the data content through zp Const qualifier is not allowed on a constructor Q: Which member functions/variables calls are valid? (c) (d) (e)

19 Example 7 Q: Which member functions/variables calls are valid?
class Zurt{ public: Zurt() { m_health = m_row = m_col = 0; } Zurt(int health, int r,int c) { m_health = health; m_row = r; m_col = c; int health() const { return m_health; int row() const { return m_row; int col() const { return m_col; void setHealth(int health) { void setRow(int r) { void setCol(int c) { private: int m_health, m_row, m_col; }; int main(){ Zurt z(100,1,1); Zurt* zp = &z; zp->Zurt(); // (a) zp->Zurt(100,1,2); // (b) int h = zp->health(); // (c) int r = zp->row(); // (d) int c = zp->col(); // (e) zp->setHealth(50); // (f) zp-> setRow(2); // (g) zp->setCol(2); // (h) zp->m_col = 5; // (i) return 0; } Q: Which member functions/variables calls are valid?

20 Solution 7 Q: Which member functions/variables calls are valid?
class Zurt{ public: Zurt() { m_health = m_row = m_col = 0; } Zurt(int health, int r,int c) { m_health = health; m_row = r; m_col = c; int health() const { return m_health; int row() const { return m_row; int col() const { return m_col; void setHealth(int health) { void setRow(int r) { void setCol(int c) { private: int m_health, m_row, m_col; }; int main(){ Zurt z(100,1,1); Zurt* zp = &z; zp->Zurt(); // (a) zp->Zurt(100,1,2); // (b) int h = zp->health(); // (c) int r = zp->row(); // (d) int c = zp->col(); // (e) zp->setHealth(50); // (f) zp-> setRow(2); // (g) zp->setCol(2); // (h) zp->m_col = 5; // (i) return 0; } Q: Which member functions/variables calls are valid? (c) (d) (e) (f) (g) (h)

21 Operator Overloading Operator overloading is the act of providing user-defined behavior for different C++ operators. Object 3 = Obj1 + Obj2 Obj1 = Obj2

22 Operator Overloading - Exmaple

23 Operator Overloading - print
Friend filestream


Download ppt "CS31 Discussion 1H Winter19: week 9"

Similar presentations


Ads by Google