1 / 15

CS106X – Programming Abstractions in C++

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 .

pepper
Download Presentation

CS106X – Programming Abstractions in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. CS106X – Programming Abstractions in C++ Cynthia Bailey Lee

  2. Today’s Topics: • Pointers and new/delete (finish from Wed.) • Making your own C++ class • Implement Vector • How to manage dynamic memory in a class

  3. Pointers and new/delete Finish up the example from Wednesday

  4. Dynamic memory allocation int * 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 #

  5. Dynamic memory allocation These last four lines… Looks good! Didn’t do enough deleting Did too much deleting Accessed memory after deleting Other/none/more int * 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 #

  6. Making your own class IntVector class

  7. Practice making a class • We will do a simplified Vector implementation • Stanford library Vector uses templatethat can hold any type • Vector<bool>, Vector<Vector<char>>, … • For simplicity, ours will only hold int • Don’t worry, we’ll learn templates later!

  8. 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

  9. Implementation #1: Array • We will just use an array • We need it to change size sometimes • THANK YOU, DYNAMIC MEMORY!

  10. 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

  11. 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;

  12. FIRST RULE OF LINKED LISTS CLUB: DRAW A PICTURE OF LINKED LISTS Seriously. Seriously. Do no attempt to code linked lists without pictures.

  13. Draw a picture! head->next->next = new ListNode; head->next->next->data = 40; Before: • After: • After: • Using “next” that is NULL gives error • Other/none/more than one head head head

  14. head Different cases of add Add to front NULL Add to the end head NULL NULL Add to the middle head NULL

  15. Special cases of remove Remove from the front head NULL Remove in the middle Remove from the end head head NULL NULL NULL

More Related