15
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution - NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.or g .

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Embed Size (px)

Citation preview

Page 1: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

CS106X – Programming Abstractions in C++Cynthia Bailey Lee

              

CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee

 is licensed under a Creative Commons Attribution-

NonCommercial-ShareAlike 4.0 International License.

Permissions beyond the scope of this license may be available at 

http://peerinstruction4cs.org.

Page 2: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

2

Today’s Topics:

1. Pointers and new/delete (finish from Wed.)

2. Making your own C++ class

Implement Vector

3. How to manage dynamic memory in a class

Page 3: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Pointers and new/deleteFinish up the example from Wednesday

Page 4: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Dynamic memory allocationint * p1 = new int[3];//0x12*p1 = 5;int * p2 = new int;//0x4 *p2 = 7;int * p3 = new int;//0x20*p3 = 8675309; // important phone #*p1 = *p2;cout << p1 << “ “ << *p1 << endl;p1 = p2;cout << p1 << “ “ << *p1 << endl;delete p1;p2 = p3;delete p1;delete p2;cout << *p3 << endl; //print important phone #

Page 5: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Dynamic memory allocationint * p1 = new int[3];//0x12*p1 = 5;int * p2 = new int;//0x4 *p2 = 7;int * p3 = new int;//0x20*p3 = 8675309; // important phone #*p1 = *p2;cout << p1 << “ “ << *p1 << endl;p1 = p2;cout << p1 << “ “ << *p1 << endl;delete p1;p2 = p3;delete p2;cout << *p3 << endl; //print important phone #

These last four lines…A. Looks good!B. Didn’t do enough

deletingC. Did too much

deletingD. Accessed memory

after deletingE. Other/none/more

Page 6: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Making your own classIntVector class

Page 7: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Practice making a class We will do a simplified Vector

implementation Stanford library Vector uses template

that can hold any type Vector<bool>, Vector<Vector<char>>, …

For simplicity, ours will only hold int Don’t worry, we’ll learn templates later!

Page 8: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

DESIGN! It matters Client expectations: What does an int

Vector need to do? Hold ints Know how many ints it is holding Quickly return an int Quickly change the value of an int Grow/shrink by adding/removing ints at

any index All this becomes the .h file

Page 9: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Implementation #1: Array We will just use an array We need it to change size sometimes

THANK YOU, DYNAMIC MEMORY!

Page 10: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Implementation #2: Linked List Array is fast to access an element, but

slow to insert or remove one in the middle

Linked list is a structure designed to help with this issue

Page 11: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Quick note: accessing a member variable in a C++ object Let’s say you have a pointer to a

ListNode: ListNode * head;

You want to access the next ListNode after that: ListNode* theNextOne = head.next; ListNode* theNextOne = (*head).next;

OR ListNode* theNextOne = head->next;

Page 12: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

FIRST RULE OF LINKED LISTS CLUB:

DRAW A PICTURE OF LINKED LISTS

Seriously. Seriously. Do no attempt to code linked lists without pictures.

Page 13: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Draw a picture!head->next->next = new ListNode;head->next->next->data = 40;

Before:

A. After:

B. After:

C. Using “next” that is NULL gives error D. Other/none/more than one

data next10

data next20 NULLhead

data next

10

data next

40head

data next

20 NULL

data next

10

data next

20head

data next

40 NULL

Page 14: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Add to front

head

head

NULL

NULL

Add to the end

Different cases of add

headNULL

Add to the middle

NULL

Page 15: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons

Remove from the front

NULL

head

Special cases of remove

Remove in the middle

NULL

head

NULL

head Remove from the end

NULL