dis1

dis1 - CS32 Discussion Sec.on 3A Week 2 TA: Brian Choi Copy...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS32 Discussion Sec.on 3A Week 2 TA: Brian Choi Copy Constructors Mo.va.on class School { public: School(); School(const string &name); string getName() const; void setName(const string &name); void addStudent(const Student &student); Student *getStudent(const string &name) const; bool removeStudent(const string &name); int getNumStudents() const; private: string m_name; Student *m_students; int m_numStudents; }; // accessor // // // // // modifier modifier accessor modifier accessor // name of the school // dynamic array of students // number of students Copy Constructors Mo.va.on Student st1("Brian"); s1 UCLA Student st2("John"); School s1("UCLA"); s1.addStudent(st1); s1.addStudent(st2); Student *p = s1.getStudent("John"); Copy Constructors Mo.va.on Candidate 1: Just use an assignment School s2 = s1; What happens internally if you do this (assume no operator overloading)? What are the limits to this approach? Copy Constructors Mo.va.on Candidate 1 School s2 = s1; UCLA s1 Every member variable gets copied even the pointers (but not the pointees). It will first call the default constructor of s2, ini.alize members with default values, and then copy the values efficiency issue. UCLA s1 UCLA s2 Copy Constructors Mo.va.on Candidate 2: Just grab values out of s1 and manually copy them into s2 School s2; s2.setName(s1.getName()); ... What are the limits to this approach? Copy Constructors Mo.va.on Candidate 2 School s2; s2.setName(s1.getName()); // how do I get students out of s1? We may not have accessors and modifiers to all variables! It is oZen not desirable to have the user (of a class) to know all the internals. Freakin' long to write, and slow! Copy Constructors public: School(const School &aSchool); This is a constructor that is used to copy values from one instance to another. Why do you think the parameter is a constant reference? School Copy Constructor School::School(const School &aSchool) { School Copy Constructor School::School(const School &aSchool) : m_name(aSchool.m_name), m_numStudents(aSchool.m_numStudents), m_students(new Students[m_numStudents]) { for (int i = 0; i < m_numStudents; i++) m_students[i] = aSchool. m_students[i]; } Why is it that you don't have to use the accessors? If there are dynamically allocated objects, you allocate new memory and manually copy them over. You can now use: School s2(s1); I s.ll think this is neat! School s2 = s1; Use operator overloading (in this case, assignment overloading). public: School &operator=(const School &aSchool) Trick: Operator Overloading School& School::operator=(const School &aSchool) { I assume we have = operator properly overloaded in Student class. Trick: Operator Overloading School& School::operator=(const School &aSchool) { m_name = aSchool.m_name; m_numStudents = aSchool.m_numStudents; m_students = new Students[m_numStudents]; for (int i = 0; i < m_numStudents; i++) m_students[i] = aSchool. m_students[i]; return *this; } I assume we have = operator properly overloaded in Student class. // don't forget this! Before we talk about linked lists... CS32 is all about organizing data. We call an organiza.on scheme a data structure. For every data structure, we must define: rules for organizing data items (e.g. array with integers stored in a nondecreasing order), a method to add a new data item without breaking any of the rules, a method to remove a data item without breaking any of the rules, and most importantly, how to search for an item We will examine various data structures and algorithms, pros and cons of each, as well as their efficiency. Linked Lists A key component of a linked list is a node, which is a single unit of data. value *next The first box carries a value, and the second is a pointer to another node. e.g. struct Node { int value; Node* next; }; Linked Lists Linked list is a series of nodes, each poin.ng to the next one. The last node's next pointer is NULL. What is the informa.on you need to complete the picture? Head pointer! Obviously, you need to know where it begins. We keep a pointer that points to the first item and call it the head pointer. e.g. Node *head; Linked Lists (Min. Requirements) You need a descrip.on of a node, which must contain a next pointer. You need a head pointer that points to the first node. You shouldn't have any loop in the structure (unless it is a circularly linked list, where a loop must exist). Linked Lists (Inser.on) 3step inser.on 1. Create a new node. Call the pointer to it p. p head 2. Make its next pointer point to the first item. p->next = head; head p Linked Lists (Inser.on) 3step inser.on 3. Make the head pointer point to the new node. head = p; head p Linked Lists (Inser.on) Sanity Check Does it work with an empty list? p->next = head; head = p; head NULL Linked Lists (Search) Isn't it too obvious? Linked Lists (Removal) Suppose there is an item that you want to remove, and it is pointed by a pointer, say p. Can I just do "delete p;"? q p We need to set the previous node(q)'s next pointer to point to the next node of p! Linked Lists (Removal) When looking up p, keep the pointer to the previous node (q). q p Then ... q->next = p->next; delete p; Linked Lists (Removal) Sanity Check Does it work if p == head? Does it work if p points to the last one? p? p? Linked Lists (Removal) If p == head, there is no "previous" node to p. Make a separate case for this. We need to reset the head pointer. head = p->next; delete p; Linked Lists (Removal Summary) Remove(valToRemove) p = head, q = NULL while (p != NULL) If p>value == valToRemove, break q = p; p = p>next; if p == NULL return head = p>next // no valToRemove in the list if p == head (or q == NULL), else q>next = p>next delete p What's nice about linked lists Very efficient inser.on Flexible memory alloca.on Think about what you should do if you have to grow/shrink a dynamically allocated array. And yes, there is a liile overhead, but that's the price we pay. Simple to implement What's not so nice about linked lists Slow search (i.e. accessing a certain element, e.g. "get the 4237th item") Usually, search is the opera.on that maiers more than inser.on or removal. Varia.ons Sorted Linked Lists Make changes to the inser.on method. Doubly Linked Lists Each node has prev and next pointers. A tail pointer is kept to point to the last node. Why do you think this is useful? Circularly Linked Lists The last node's next pointer points to the first one. Essen.ally, there is no "first" node. ...
View Full Document

This note was uploaded on 11/07/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Ask a homework question - tutors are online