Dynamic Arrays Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.

Slides:



Advertisements
Similar presentations
Lists: An internal look
Advertisements

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
DATA STRUCTURE & ALGORITHMS
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Object Oriented Data Structures
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Pointers OVERVIEW.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Review and Prepare for Test 1 Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
An Array-Based Implementation of the ADT List
Data Structures Using C++ 2E
Doubly Linked List Implementation
Review and Prepare for Test 2
The Standard Template Library
Data Structures & Algorithms
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lists CMSC 202, Version 4/02.
Dynamic Array: Implementation of Stack
Data Structures and Algorithms CS 244
Presentation transcript:

Dynamic Arrays Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content from Data Structures Using C++ (D.S. Malik)

Points of Note Assignment 7 is posted Due soon: Wednesday, April 2 Assignment 8 is posted Due Thursday, April 3 Assignment 9 is posted Due Thursday, April 10 Check D2L to confirm all due dates

Previously Previous discussion was on Reading Data from a text file Sorting Data Searching Data Linked Lists Single, Double, Circular

Today Dynamic Arrays Establishes a setting to talk about how to implement stacks transitions to Stack ADT Suggest In the background download example: DS214_Pitcher from D2L

Marker Slide Any General Questions ? Next up Dynamic Arrays Setup Pitcher Pour Pitcher Class Ice Cube Tea

Dynamic Arrays Dynamic Array An array that can grow in size Has a Capacity The maximum number of things it can hold Number of cells Number chairs in the room Has a Size The current number of things it holds Number of cells used Number of chairs being used (students in the room) Standard Template Library example std::vector in STL is a “dynamic array”

Dynamic Array Example in STL C++ Vector in STL Recall the Standard Template Library is a collection of data types (and algorithms) std::vector is one of these data types Allows random access i.e. A[5], A[231], A[59], …  not ordered (‘random’ indices) Similar to Java’s ArrayList (but a different API) Some functions of std::vector include size() capacity() push_back() empty() there are more…

Words “Dynamic array” is too wordy also called growable arrays and expandable arrays and a variety of other [synonym for dynamic] arrays SO Many CS types have taken to using VECTOR to mean Dynamic Array and “array” means “static array” be aware of who you are talking with and how they use the words Vector however is a very well defined math-physics-engineering word ALSO used by CS types

Adding elements to a vector Vectors, like static arrays can become full when the vector’s size == the vector’s capacity However unlike static arrays (which just throw errors) Vectors can allocate more memory to hold more elements hence “dynamic array” Note this allocation of more memory is hidden from the person “using” the vector It just happens automatically and behind the scenes But we are concerned and interested programmers and must understand HOW this automation happens

Marker Slide Any Questions On: Dynamic Arrays Setup Next up Dynamic Arrays Pitcher Pour Pitcher Class Ice Cube Tea

Picture Pour? This is a picture to illustrate: A vector has become full A pitcher has become full More memory has been allocated (double the size) New pitcher twice the size All the old data is copied (poured) into the newly allocated memory space Contents of small pitcher poured into larger And viola there is now plenty of space to add more elements

Picture Pour? This is a picture to illustrate: A vector has become full A pitcher has become full More memory has been allocated (double the size) New pitcher twice the size All the old data is copied (poured) into the newly allocated memory space Contents of small pitcher poured into larger And viola there is now plenty of space to add more elements

Picture Pour? This is a picture to illustrate: A vector has become full A pitcher has become full More memory has been allocated (double the size) New pitcher twice the size All the old data is copied (poured) into the newly allocated memory space Contents of small pitcher poured into larger And viola there is now plenty of space to add more elements

Dynamic Array Towards an Example Let’s go with the idea of Pitcher…

Motivation / Analogy For a get together – we have a pitcher of tea It can only hold enough for 4 drinks We invite 3 friends to come over so all is good.

Motivation / Analogy Say we have a pitcher it can only hold enough water for 4 drinks We invite 3 friends to come over so all is good At the last minute Someone decides to bring their roommate with them Now we have to make more tea AND need a bigger pitcher

Motivation / Analogy We could select a pitcher that can hold enough for 5 drinks But what if we get another last minute addition? Then we would have to do this whole thing again for each new addition

Motivation / Analogy Perhaps a pitcher that holds 6 drinks (2 more than original would be better) Then we only need to do this for each 2 new additions… + =

Motivation / Analogy Perhaps a pitcher that holds 6 drinks (2 more than original would be better) Then we only need to do this for each 2 new additions… Or maybe getting a pitcher that can hold twice as much would be better + = * 2 =

Group In-Class Exercise Assume it takes 10 minutes of preparation time each time we have to get a new pitcher (a nd 1 minute to prepare/place out things for each guest) 4 minutes originally, 5+10 minutes with late addition, 6+10 minutes if another… Which solution will be the better option for saving time as the number of guests (n) increases? Increasing the pitcher size by 2 each as needed or Doubling the pitcher size as needed Assume n increments by 1 for an undetermined amount of time. Assume our party size could get huge (to unknown large maximum) Cannot use a pitcher size of infinity or 5 million or whatever without a need for it Discuss in groups of 3 or 4 Elect someone to write it up (.txt file) for discussion * 2 = + =

Discuss Some Discuss some of the solutions proposed

Marker Slide Any Questions On: Dynamic Arrays Setup Pitcher Pour Next up Dynamic Arrays Pitcher Class Ice Cube Tea

“Official” Solution See next presentation (mostly) Now let’s think about some code We want to make a Pitcher class sort of a dynamic array class but no array (to start with)

class Pitcher Our pitcher needs to have Capacity Max number of drinks it can hold Member variable int m_capacity Default initial capacity is 4 Size Some tea in it The amount used Number of drinks currently held Member variable int m_size class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

class Pitcher Our pitcher needs to have Capacity Max number of drinks it can hold Member variable int m_capacity Default initial capacity is 4 Size Some tea in it The amount used Number of drinks currently held Member variable int m_size class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

class Pitcher – header file stuff Our pitcher needs to have Capacity Max number of drinks it can hold Member variable int m_capacity Default initial capacity is 4 Size Some tea in it The amount used Number of drinks currently held Member variable int m_size class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

class Pitcher – constructor (cpp) Pitcher constructor Initializes the private member variables Pitcher::Pitcher(int capacity) { what goes here ? } Recall header file was like so: class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

class Pitcher – constructor (cpp) Pitcher constructor Initializes the private member variables Pitcher::Pitcher(int capacity) { m_capacity = capacity; m_size = 0; //init size to 0 } Recall header file was like so: class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };

class Pitcher – addTea We need to be able to add tea to the pitcher Because we have a Keurig we make our tea one serving at a time So we add one serving at a time to our pitcher class Pitcher { private: : public: Pitcher(int capacity = 4); void addTea(); : }; void Pitcher::addTea() { ++m_size; } // Pitcher.h file // Pitcher.cpp file

class Pitcher – addTea We need to be able to add tea to the pitcher Because we have a Keurig we make our tea one serving at a time So we add one serving at a time to our pitcher class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); void addTea(); : }; void Pitcher::addTea() { ++m_size; } WAIT! There is something missing! // Pitcher.cpp file // Pitcher.h file

class Pitcher – addTea We need to be able to add tea to the pitcher Because we have a Keurig we make our tea one serving at a time So we add one serving at a time to our pitcher class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); void addTea(); : }; void Pitcher::addTea() { ++m_size; } WAIT! There is something missing! // Pitcher.cpp file // Pitcher.h file

class Pitcher – addTea void Pitcher::ddTea() { Corrections needed here …. } void Pitcher::addTea() { ++m_size; }

class Pitcher – addTea void Pitcher::addTea() { if ( (m_size + 1) > m_capacity ) { m_capacity = m_capacity * 2; // go with doubling idea here } ++m_size; } void Pitcher::addTea() { Corrections needed here …. }

class Pitcher – getters class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); void addTea(); int getCapacity(); int getSize(); : }; // Pitcher.h file

class Pitcher – getters int Pitcher::getCapacity() { what goes here ? } int Pitcher::getSize() { what goes here ? } class Pitcher { : public: Pitcher(int capacity = 4); void addTea(); int getCapacity(); int getSize(); : }; // Pitcher.h file // Pitcher.cpp file

class Pitcher – getters int Pitcher::getCapacity() { return m_capacity; } int Pitcher::getSize() { what goes here ? } class Pitcher { : public: Pitcher(int capacity = 4); void addTea(); int getCapacity(); int getSize(); : }; // Pitcher.h file // Pitcher.cpp file

class Pitcher – getters int Pitcher::getCapacity() { return m_capacity; } int Pitcher::getSize() { return m_size; } class Pitcher { : public: Pitcher(int capacity = 4); void addTea(); int getCapacity(); int getSize(); : }; // Pitcher.h file // Pitcher.cpp file

class Pitcher – copy constructor class Pitcher { : public: Pitcher(int capacity = 4); Pitcher(Pitcher& copyMe); : }; // Pitcher.h file Pitcher::Pitcher(Pitcher& copyMe) { what goes here ? } // Pitcher.cpp file

class Pitcher – copy constructor class Pitcher { : public: Pitcher(int capacity = 4); Pitcher(Pitcher& copyMe); : }; Pitcher::Pitcher(Pitcher& copyMe) { m_capacity = copyMe.m_capacity; m_size = copyMe.m_size; } // Pitcher.h file // Pitcher.cpp file

class pitcher – assignment operator class Pitcher { : public: Pitcher& operator= (const Pitcher& rhs); : }; // Pitcher.h file Pitcher& Pitcher::operator= (const Pitcher& rhs) { what goes here ? } // Pitcher.cpp file

class pitcher – assignment operator class Pitcher { : public: Pitcher& operator= (const Pitcher& rhs); : }; // Pitcher.h file Pitcher& Pitcher::operator= (const Pitcher& rhs) { m_capacity = rhs.m_capacity; m_size = rhs.m_size; } // Pitcher.cpp file

bool operator== (const Pitcher& lhs, const Pitcher& rhs) { what goes here ? } class Pitcher – operator == class Pitcher { : public: friend bool operator== (const Pitcher& lhs, const Pitcher& rhs); : }; // Pitcher.h file // Pitcher.cpp file

class Pitcher – operator == class Pitcher { : public: friend bool operator== (const Pitcher& lhs, const Pitcher& rhs); : }; // Pitcher.h file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { bool retVal = false; if (lhs.m_size == rhs.m_size) retVal = true; return retVal; } Note size (contents) matters for equality Capacity does not // Pitcher.cpp file

Marker Slide Any Questions On: Dynamic Arrays Setup Pitcher Pour Pitcher Class Next up Dynamic Arrays Ice Cube Tea

Adding a Twist So because we want to see an example of a dynamic array… We need to change the story a little…

What about the array? This requires some creative thought… because pitchers hold liquid normally… It is pretty cold here… I think our tea has frozen So each serving is now an ice cube To make it interesting Let’s say each ice cube has a flavor

Enumerated Flavors In the header let’s add a line enum Flavor { noFlavorSet = -1, raspberry, peach, lemon, grape, strawberry }; class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : }; // Pitcher.h file

class Pitcher – with an array Our pitcher already has Capacity Size Let’s make an array to keep track of each flavor of each serving (each ice cube) So add another member variable: mp_serving of type Flavor* class Pitcher { private: int m_capacity; int m_size; Flavor* mp_serving; : public: Pitcher(int capacity = 4); : }; enum Flavor { noFlavorSet = -1, raspberry, peach, lemon, grape, strawberry }; // Pitcher.h file

class Pitcher – memory allocation So we need to modify some functions now First create a function to allocate memory and “save things” when more memory is needed.

Pitcher::growPitcher void Pitcher::growPitcher(int n, bool copy) { Flavor* newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0; i < m_size; i++) { newArr[i] = mp_serving[i]; } delete [] mp_serving; // free memory used // Assign newArr to mp_serving and Update capacity to n mp_serving = newArr; m_capacity = n; } // Pitcher.cpp file

Pitcher::growPitcher void Pitcher::growPitcher(int n, bool copy) { int* newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0; i < m_size; i++) { newArr[i] = mp_serving[i]; } delete [] mp_serving; // free memory used // Assign newArr to mp_serving and Update capacity to n mp_serving = newArr; m_capacity = n; } // Pitcher.cpp file

Pitcher::growPitcher void Pitcher::growPitcher(int n, bool copy) { int* newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0; i < m_size; i++) { newArr[i] = mp_serving[i]; } delete [] mp_serving; // free memory used // Assign newArr to mp_serving and Update capacity to n mp_serving = newArr; m_capacity = n; } // Pitcher.cpp file

Pitcher::growPitcher void Pitcher::growPitcher(int n, bool copy) { int* newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0; i < m_size; i++) { newArr[i] = mp_serving[i]; } delete [] mp_serving; // free memory used // Assign newArr to mp_serving and Update capacity to n mp_serving = newArr; m_capacity = n; } // Pitcher.cpp file // should also initialize all elements // to invalid state – as a safety for(int i=0; i < m_n; i++) { newArr[i] = noFlavorSet; }

Pitcher – constructor, Revised Need to allocate memory now Pitcher::Pitcher(int capacity) { m_capacity = capacity; m_size = 0; //init size to 0 } Pitcher::Pitcher(int capacity) { mp_serving = NULL; // always init ptrs growPitcher(capacity, false); m_size = 0; // init size to 0 // init all servings to notSet for (int j=0;j <m_capacity; j++) { mp_serving[j] = noFlavorSet; } // OLD Pitcher.cpp file // New Pitcher.cpp file

Pitcher – addTea, Revised We need to be able to add tea to the pitcher Because we have a Keurig we make our tea one serving at a time So we add one serving at a time to our pitcher Now allow a flavor to be specified class Pitcher { private: : public: void addTea(); : }; // Old Pitcher.h file

Pitcher – addTea, Revised We need to be able to add tea to the pitcher Because we have a Keurig we make our tea one serving at a time So we add one serving at a time to our pitcher Now allow a flavor to be specified class Pitcher { private: : public: void addTea(Flavor flav); : }; // New Pitcher.h file

Pitcher – addTea, Revised void Pitcher::addTea() { if ( (m_size + 1) > m_capacity ) { m_capacity = m_capacity * 2; // go with doubling idea here } ++m_size; } // Old Pitcher.cpp file void Pitcher::addTea(Flavor flav) { if ( (m_size + 1) > m_capacity ) { growPitcher(m_capacity * 2, true); // go with doubling idea here } mp_serving[m_size] = flav; // set the new serving flavor ++m_size; // add 1 to serving count } // New Pitcher.cpp file

Pitcher – copy constructor, Revised Pitcher::Pitcher(Pitcher& copyMe) { m_capacity = copyMe.m_capacity; m_size = copyMe.m_size; } // Old Pitcher.cpp file Pitcher::Pitcher(Pitcher& copyMe) { mp_serving = NULL; // always init ptrs growPitcher(copyMe.m_capacity, false); for (int j=0; j < copyMe.m_capacity; j++) { mp_serving[j] = copyMe.mp_serving[j]; } m_size = copyMe.m_size; } // New Pitcher.cpp file

Pitcher – assignment operator, Revised Pitcher& Pitcher::operator= (const Pitcher& rhs) { m_capacity = rhs.m_capacity; m_size = rhs.m_size; } // Old Pitcher.cpp file Pitcher& Pitcher::operator= (const Pitcher& rhs) { if (m_capacity < rhs.m_capacity) { growPitcher(rhs.m_capacity, false); } for (int j=0; j < rhs.m_capacity; j++) { mp_serving[j] = rhs.mp_serving[j]; } m_size = rhs.m_size; } // New Pitcher.cpp file An oddity could happen here if this->m_capacity was originally greater than rhs.m_capacity All will work… but… what if this->size was initially greater than rhs.m_size too ?

Revised Pitcher – operator == bool operator== (const Pitcher& lhs, const Pitcher& rhs) { bool retVal = false; if (lhs.m_size == rhs.m_size) retVal = true; return retVal; } // Old Pitcher.cpp file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { bool retVal = false; if (lhs.m_size == rhs.m_size) { retVal = true; int j = 0; while ( (j < lhs.m_size) && (true == retVal)) { if ( lhs.mp_serving[j] != rhs.mp_serving[j]) { retVal = false; } ++j; } return retVal; } // New Pitcher.cpp file

End of Revisions So that should mostly work Probably want one more function to output stuff

class Pitcher operator << ostream& operator<< (ostream& ostr, const Pitcher& pi) { if ( pi.m_size <= 0) { ostr << "pitcher is empty"; } for(int i=0; i < pi.m_size; i++) { switch (pi.mp_serving[i]) { case raspberry: ostr << "raspberry"; break; case peach: ostr << "peach"; break; : // lines skipped here default: ostr << "oops spilled one"; break; } // end switch ostr << " "; } // end for return ostr; }

So Ends Class Pitcher Details of this class may be found on D2L Ask if you cannot locate them Check In-Class Assignments, Unit 2, Examples or similar

Marker Slide Any Questions On: Dynamic Arrays Setup Pitcher Pour Pitcher Class Ice Cube Tea Next up End of Part 1

The End The end of part 1, presentation 15