Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.

Similar presentations


Presentation on theme: "CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus."— Presentation transcript:

1

2 CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus

3 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){ ; }

4 next Chains struct AgeNode { int age; AgeNode* next; }; How do I access the third node? head 2468 head->next->next->age

5 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

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

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

8 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. ‘B’ ‘C’ ‘L’‘T’ ‘V’ ‘Y’ listData

9 Circular Linked List Operations: Find(item) Insert(item) Delete(item)

10 ‘A’ ‘C’ ‘F’ ‘T’ ‘Z’ 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. listData

11 Doubly Linked List Operations: Find(item) Insert(item) Delete(item)

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

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

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

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

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

17 What’s the difference? A shallow copy shares the pointed to data with the original class object. A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.

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

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

20 MyStack.topPtr is left dangling ? 30 StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); Private data: topPtr 6000 MyStack SomeStack shallow copy Private data: 7000 6000 topPtr 7000

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

22 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 a copy constructor, which makes a deep copy of the dynamic data in a different memory location.

23 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. Classes that Use Dynamic Memory

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

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

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

27 Copy Constructor

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

29 StackType::StackType(const StackType& source) { 85 source.topPtr NULL50 topPtr

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

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

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

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

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

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

36 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; tempPtr 85 source.topPtr NULL50 topPtr 50 location

37 location = topPtr;// copy remaining nodes tempPtr 85 source.topPtr NULL50 topPtr 50 location

38 tempPtr = tempPtr->next; tempPtr 85 source.topPtr NULL50 topPtr 50 location

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

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

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

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

43 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; } NULL tempPtr 85 source.topPtr NULL50 topPtr 8550 location

44 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; } NULL tempPtr 85 source.topPtr NULL50 topPtr 85NULL50 location

45 Stack After Using Copy Constructor 85 source.topPtr NULL50 85NULL50 topPtr

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

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

48 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

49 // Code identical to the copy constructor follows qFront = NULL; qRear = NULL; source.qFront CNULL source.qRear NULL qFront NULL qRear ts

50 // Code identical to copy constructor follows qFront = NULL; qRear = NULL; // If source is empty, no copying is needed if (source.qFront !=NULL) { source.qFront CNULL source.qRear NULL qFront NULL qRear ts

51 // 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; source.qFront CNULL source.qRear NULL qFront NULL qRear ts insert_ptr temp_ptr

52 // Copy the first node in source insert_ptr = new NodeType; source.qFront CNULL source.qRear NULL qFront NULL qRear ts insert_ptr temp_ptr

53 // Copy the first node in source insert_ptr = new NodeType; insert_ptr->info = source.qFront->info; source.qFront CNULL source.qRear NULL qFront NULL qRear ts insert_ptr temp_ptr C

54 // Copy the first node in source insert_ptr = new NodeType; insert_ptr->info = source.qFront->info; insert_ptr->next = NULL; source.qFront CNULL source.qRear NULL qFront NULL qRear ts insert_ptr temp_ptr CNULL

55 // Copy the first node in source insert_ptr = new NodeType; insert_ptr->info = source.qFront->info; insert_ptr->next = NULL; qFront = insert_ptr; source.qFront CNULL source.qRear qFront NULL qRear ts insert_ptr temp_ptr CNULL

56 // Copy the first node in source insert_ptr = new NodeType; insert_ptr->info = source.qFront->info; insert_ptr->next = NULL; qFront = insert_ptr; qRear = insert_ptr; source.qFront CNULL source.qRear qFront qRear ts insert_ptr temp_ptr CNULL

57 // Copy the other nodes in source temp_ptr = source.qFront->next; source.qFront CNULL source.qRear qFront qRear ts insert_ptr NULL temp_ptr CNULL

58 // Copy the other nodes in source temp_ptr = source.qFront->next; while (temp_ptr != NULL) { source.qFront CNULL source.qRear qFront qRear ts insert_ptr NULL temp_ptr CNULL

59 insert_ptr = new NodeType; insert_ptr->info = temp_ptr->info; insert_ptr->next = NULL; qRear->next = insert_ptr; qRear = insert_ptr; temp_ptr = temp_ptr->next; } } } } // End of everythingsource.qFront CNULL source.qRear qFront qRear ts insert_ptr NULL temp_ptr CNULL

60 C++ Operator Overloading Guides 1 All operators except these ::. sizeof ?: may be overloaded. 2 At least one operand must be a class instance. 3 You cannot change precedence, operator symbols, or number of operands. 4 Overloading ++ and -- requires prefix form use by default, unless special mechanism is used. 5 To overload these operators = ( ) [ ] member functions (not friend functions) must be used. 6 An operator can be given multiple meanings if the data types of operands differ.

61 Composition (containment) Composition (or containment) means that an internal data member of one class is defined to be an object of another class type. A FAMILIAR EXAMPLE...

62 Private data value ComparedTo Print Initialize class ItemType ItemType Class Interface Diagram

63 Inheritance Inheritance is a means by which one class acquires the properties--both data and operations--of another class. When this occurs, the class being inherited from is called the Base Class. The class that inherits is called the Derived Class. AN EXAMPLE...

64 class QueType QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’ ‘Z’ ‘T’

65 // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); // CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR bool IsEmpty( ) const; bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); void MakeEmpty( ); private: NodeType * qFront; NodeType * qRear; };

66 // DERIVED CLASS CountedQue FROM BASE CLASS QueType template class CountedQue : public QueType { public: CountedQue( ); void Enqueue( ItemType newItem ); void Dequeue( ItemType& item ); int LengthIs( ) const; // Returns number of items on the counted queue. private: int length; }; SAYS ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED FOR OBJECTS OF TYPE CountedQue

67 class CountedQue QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’ ‘Z’ ‘T’ CountedQue LengthIs Enqueue Dequeue. Private Data: length 3

68 // Member function definitions for class CountedQue template CountedQue ::CountedQue( ) : QueType ( ) { length = 0 ; } template int CountedQue ::LengthIs( ) const { return length ; }

69 template void CountedQue ::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Increments length. { length++; QueType ::Enqueue( newItem ); } template void CountedQue ::Dequeue(ItemType& item ) // Removes item from the rear of the queue. // Decrements length. { length--; QueType ::Dequeue( item ); }


Download ppt "CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus."

Similar presentations


Ads by Google