1 / 68

CSI 1340 Introduction to Computer Science II

CSI 1340 Introduction to Computer Science II. Chapter 6 Lists Plus. Definition vs. Declaration. Declaration: Associates an identifier with a data object, an action (e.g., function), or a data type. Ex: typedef int IDType; void PrintArray(int x[ ], int noElmts);

marydmoore
Download Presentation

CSI 1340 Introduction to Computer Science II

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. CSI 1340Introduction to Computer Science II Chapter 6 Lists Plus

  2. Definition vs. Declaration • Declaration: Associates an identifier with a data object, an action (e.g., function), or a data type. Ex: typedef int IDType; void PrintArray(int x[ ], int noElmts); • Definition: A declaration becomes a definition when it binds storage to the identifier Ex: void PrintArray(int x[ ], int noElmts){ ; }

  3. next Chains struct AgeNode { int age; AgeNode* next; }; How do I access the third node? head 2 4 6 8 head->next->next->age

  4. Constructors with new How do you call the constructor function when using new? class Employee { string m_name; // Are these variables private or public? int m_salary; public: Employee( ); Employee(string name, int salary); }; Employee* bob = new Employee(“Bob”, 500); Employee* jane = new Employee; // Calls default constructor

  5. Perilous Pass By Value How can you get in trouble using pass by value?

  6. Referring to Yourself A class instance by any other name is still a class instance - Shakespere What is exactly is the “other” name?

  7. ‘B’ ‘C’ ‘L’ ‘T’ ‘V’ ‘Y’ listData What is a Circular Linked List? • A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

  8. Circular Linked List Operations: • Find(item) • Insert(item) • Delete(item)

  9. What is a Doubly Linked List? • A doubly linked list is a list in which each node is linked to both its successor and its predecessor. ‘A’ ‘C’ ‘F’ ‘T’ ‘Z’ listData

  10. Doubly Linked List Operations: • Find(item) • Insert(item) • Delete(item)

  11. Each node contains two pointers template< class ItemType > struct NodeType { ItemType info;// Data member NodeType<ItemType>* back;// Pointer to predecessor NodeType<ItemType>* next; // Pointer to successor }; 3000 ‘A’ NULL .back .info .next

  12. StackType Private data: topPtr What happens . . . • When a function is called that uses pass by value for a class object like our dynamically linked stack? MakeEmpty IsEmpty 20 30 IsFull Push Pop ~StackType

  13. Passing a class object by value // FUNCTION CODE template<class ItemType> void MyFunction( StackType<ItemType> SomeStack ) // Uses pass by value { . . . . }

  14. Pass by value makes a shallow copy StackType<int> MyStack; // CLIENT CODE . . . MyFunction( MyStack ); // function call MyStack SomeStack Private data: topPtr 7000 Private data: 7000 6000 topPtr 7000 20 30 shallow copy

  15. Shallow Copyvs. Deep Copy • A shallow copycopies only the class data members, and does not copy any pointed-to data. • A deep copycopies not only the class data members, but also makes separately stored copies of any pointed-to data.

  16. What’s the difference? • A shallow copyshares the pointed to data with the original class object. • A deep copystores its own copy of the pointed to data at different locations than the data in the original class object.

  17. Making a deep copy MyStack Private data: 7000 6000 topPtr 7000 20 30 SomeStack Private data: 5000 2000 topPtr 5000 20 30 deep copy

  18. Suppose MyFunction Uses Pop // FUNCTION CODE template<class ItemType> void MyFunction( StackType<ItemType> SomeStack ) // Uses pass by value { ItemType item; SomeStack.Pop(item); . . . } WHAT HAPPENS IN THE SHALLOW COPY SCENARIO?

  19. MyStack.topPtris left dangling StackType<int> MyStack; // CLIENT CODE . . . MyFunction( MyStack ); MyStack SomeStack Private data: topPtr 6000 Private data: 7000 6000 topPtr 7000 ? 30 shallow copy

  20. MyStack.topPtris left dangling NOTICE THAT NOT JUST FOR THE SHALLOW COPY, BUT ALSO FOR ACTUAL PARAMETER MyStack, THE DYNAMIC DATA HAS CHANGED! MyStack SomeStack Private data: topPtr 6000 Private data: 7000 6000 topPtr 7000 ? 30 shallow copy

  21. As a result . . . • This default method used for pass by value is not the best way when a data member pointer points to dynamic data. • Instead, you should write what is called acopy constructor, which makes a deep copy of the dynamic data in a different memory location.

  22. Classes that Use Dynamic Memory • Always require the following member functions: • Destructor • Copy constructor • Overloaded assignment operator NOTE: If you do not implement one or more of the above, you MUST document that they are unsafe to use.

  23. More about copy constructors • When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value. • You do not call the copy constructor. • Like other constructors, it has no return type. • Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition.

  24. Copy Constructor • Copy constructor is a special member function of a class that is implicitly called in these three situations: • passing object parameters by value, • initializing an object variable in a declaration, • returning an object as the return value of a function.

  25. // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template<class ItemType> class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. StackType( const StackType<ItemType>& anotherStack ); // Copy constructor. // Implicitly called for pass by value. . . . ~StackType( ); // Destructor. // POST: Memory for nodes has been deallocated. private: NodeType<ItemType>* topPtr ; };

  26. Copy Constructor

  27. . . . int main( ) { StackType s; s.Push(85); s.Push(50); StackType t(s); // Makin’ a copy

  28. StackType::StackType(const StackType& source) { topPtr source.topPtr 50 85 NULL

  29. tempPtr StackType::StackType(const StackType& source) { NodeType *tempPtr; topPtr source.topPtr 50 85 NULL

  30. tempPtr location StackType::StackType(const StackType& source) { NodeType *tempPtr; NodeType *location; topPtr source.topPtr 50 85 NULL

  31. tempPtr location StackType::StackType(const StackType& source) { NodeType *tempPtr; NodeType *location; topPtr = NULL; topPtr NULL source.topPtr 50 85 NULL

  32. tempPtr location StackType::StackType(const StackType& source) { NodeType *tempPtr; NodeType *location; topPtr = NULL; if (source.topPtr != NULL) { topPtr NULL source.topPtr 50 85 NULL

  33. tempPtr location StackType::StackType(const StackType& source) { NodeType *tempPtr; NodeType *location; topPtr = NULL; if (source.topPtr != NULL) { tempPtr = source.topPtr; topPtr NULL source.topPtr 50 85 NULL

  34. tempPtr location StackType::StackType(const StackType& source) { NodeType *tempPtr; NodeType *location; topPtr = NULL; if (source.topPtr != NULL) { tempPtr = source.topPtr; topPtr = new NodeType; topPtr source.topPtr 50 85 NULL

  35. tempPtr location StackType::StackType(const StackType& source) { NodeType *tempPtr; NodeType *location; topPtr = NULL; if (source.topPtr != NULL) { tempPtr = source.topPtr; // copy first node topPtr = new NodeType; topPtr->info = tempPtr->info; topPtr 50 source.topPtr 50 85 NULL

  36. tempPtr location location = topPtr; // copy remaining nodes topPtr 50 source.topPtr 50 85 NULL

  37. tempPtr location location = topPtr; // copy remaining nodes tempPtr = tempPtr->next; topPtr 50 source.topPtr 50 85 NULL

  38. tempPtr location location = topPtr; // copy remaining nodes tempPtr = tempPtr->next; while (tempPtr != NULL) { topPtr 50 source.topPtr 50 85 NULL

  39. tempPtr location location = topPtr; // copy remaining nodes tempPtr = tempPtr->next; while (tempPtr != NULL) { location->next = new NodeType; topPtr 50 source.topPtr 50 85 NULL

  40. tempPtr location location = topPtr; // copy remaining nodes tempPtr = tempPtr->next; while (tempPtr != NULL) { location->next = new NodeType; location = location->next; topPtr 50 source.topPtr 50 85 NULL

  41. tempPtr location location = topPtr; // copy remaining nodes tempPtr = tempPtr->next; while (tempPtr != NULL) { location->next = new NodeType; location = location->next; location->info = tempPtr->info; topPtr 50 85 source.topPtr 50 85 NULL

  42. tempPtr NULL location location = topPtr; // copy remaining nodes tempPtr = tempPtr->next; while (tempPtr != NULL) { location->next = new NodeType; location = location->next; location->info = tempPtr->info; tempPtr = tempPtr->next; } topPtr 50 85 source.topPtr 50 85 NULL

  43. tempPtr NULL location location = topPtr; // copy remaining nodes tempPtr = tempPtr->next; while (tempPtr != NULL) { location->next = new NodeType; location = location->next; location->info = tempPtr->info; tempPtr = tempPtr->next; } location->next = NULL; } } topPtr 50 85 NULL source.topPtr 50 85 NULL

  44. Stack After Using Copy Constructor topPtr 50 85 NULL source.topPtr 50 85 NULL

  45. What about the assignment operator? • The default method used for assignment of class objects makes a shallow copy. • If your class has a data member pointer to dynamic data, you should write a member function to overload the assignment operator to make a deep copy of the dynamic data.

  46. Main.cpp: QueType s; s.Enqueue(‘C’); QueType t; t = s; overloaded assignment operator function is called: #include “Queue.h” // Declares qFront and qRear void QueType::operator=(const QueType& source) // when the member function is entered, t and s are: qFront source.qFront NULL C NULL t s source.qRear qRear NULL

  47. void QueType::operator=(const QueType& source) { // Check for case where a queue is set equal to itself if (source.qFront != qFront) { // Return the queue to the heap (i.e., MakeEmpty) NodeType *remove_ptr; while (qFront != NULL) { remove_ptr = qFront; qFront = qFront->next; delete remove_ptr; } // NOTE: qRear is set to NULL in next section

  48. // Code identical to the copy constructor follows qFront = NULL; qRear = NULL; qFront source.qFront NULL C NULL t s source.qRear qRear NULL

  49. // Code identical to copy constructor follows qFront = NULL; qRear = NULL; // If source is empty, no copying is needed if (source.qFront !=NULL) { qFront source.qFront NULL C NULL t s source.qRear qRear NULL

  50. // Code identical to copy constructor follows qFront = NULL; qRear = NULL; // If source is empty, no copying is needed if (source.qFront !=NULL) { NodeType *insert_ptr; NodeType *temp_ptr; insert_ptr temp_ptr qFront source.qFront NULL C NULL t s source.qRear qRear NULL

More Related