1 CSC211 Data Structures Lectures 4 & 5 Container Classes Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.
1 CSC212 Data Structure - Section FG Lectures 4 & 5 Container Classes Instructor: Zhigang Zhu Department of Computer Science City College of New York.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 13 Recursive Thinking Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Edgardo Molina, CSC212 Data Structure - Section AB Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Edgardo Molina Department.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 CSC212 Data Structure Lecture 4 Container Classes Instructor: George Wolberg Department of Computer Science City College of New York.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
CSC212 Data Structure - Section AB
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Using a Stack Chapter 6 introduces the stack data type.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
CSC212 Data Structure - Section RS
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
Presentation transcript:

1 CSC211 Data Structures Lectures 4 & 5 Container Classes Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

2 Review of Lecture 2&3 A Review of C++ Classes (Lecture 2) A Review of C++ Classes (Lecture 2) p OOP, ADTs and Classes p Class Definition, Implementation and Use p Constructors and Value Semantics More on Classes (Lecture 3) p Namespace and Documentation p Classes and Parameters p Operator Overloading

3 How to Use Items in a Namespace? p Three ways to use the items in a namespace p using namespace main_savitch_2A; p using main_savitch_2A::point; p main_savitch_2A::point p1; p Can we use a using directive in a header file ? p Can we use it before an #include directive?

4 How to Use Items in a Namespace? (Example?) namespace foo { int bar; double pi; } using namespace foo; int* pi; pi = &bar;

5 More about Namespace p Default namespace p Namespaces are hierarchical.  Within the hypothetical namespace food::fruit, the identifier orange refers to food::fruit::orange if it exists, p or if not food::orange if it exists. p If neither exist, orange refers to an identifier in the default namespace

6 Class as type of parameter

7 p A class can be used as the type of a function’s parameter, just like any other data type p Value parameters p Reference parameters p Const reference parameters

8 Operator Overloading

9 p Overloading arithmetic operations p Overloading binary comparison operations p Overloading input/output functions p Friend functions – when to use

10 p A container class is a data type that is capable of holding a collection of items. p In C++, container classes can be implemented as a class, along with member functions to add, remove, and examine items. Container Classes Data Structures and Other Objects Using C++

11 Bags p For the first example, think about a bag.

12 Bags p For the first example, think about a bag. p Inside the bag are some numbers.

13 Initial State of a Bag p When you first begin to use a bag, the bag will be empty. p We count on this to be the initial state of any bag that we use. THIS BAG IS EMPTY.

14 Inserting Numbers into a Bag p Numbers may be inserted into a bag. I AM PUTTING THE NUMBER 4 INTO THE BAG.

15 Inserting Numbers into a Bag p Numbers may be inserted into a bag. THE 4 IS IN THE BAG.

16 Inserting Numbers into a Bag p Numbers may be inserted into a bag. p The bag can hold many numbers. NOW I'M PUTTING ANOTHER NUMBER IN THE BAG -- AN 8.

17 Inserting Numbers into a Bag p Numbers may be inserted into a bag. p The bag can hold many numbers. THE 8 IS ALSO IN THE BAG.

18 Inserting Numbers into a Bag p Numbers may be inserted into a bag. p The bag can hold many numbers. p We can even insert the same number more than once. NOW I'M PUTTING A SECOND 4 IN THE BAG.

19 Inserting Numbers into a Bag p Numbers may be inserted into a bag. p The bag can hold many numbers. p We can even insert the same number more than once. NOW THE BAG HAS TWO 4'S AND AN 8..

20 Examining a Bag p We may ask about the contents of the bag. HAVE YOU GOT ANY 4's ? YES, I HAVE TWO OF THEM.

21 Removing a Number from a Bag p We may remove a number from a bag. THIS 4 IS OUTTA HERE!

22 Removing a Number from a Bag p We may remove a number from a bag. p But we remove only one number at a time. ONE 4 IS GONE, BUT THE OTHER 4 REMAINS.

23 How Many Numbers p Another operation is to determine how many numbers are in a bag. IN MY OPINION, THERE ARE TOO MANY NUMBERS.

24 Summary of the Bag Operations ÊA bag can be put in its initial state, which is an empty bag. ËNumbers can be inserted into the bag. ÌYou may count how many occurrence of a certain number are in the bag. ¹Numbers can be erased from the bag. ºYou can check the size of the bag (i.e. how many numbers are in the bag).

25 The bag Class  C++ classes (introduced in Chapter 2) can be used to implement a container class such as a bag. p The class definition includes: class bag 4 4 The heading of the definition

26 The bag Class p C++ classes (introduced in Chapter 2) can be used to implement a container class such as a bag. p The class definition includes: class bag { public: bag( ); 4 4 The heading of the definition 4 4 A constructor prototype

27 The bag Class  C++ classes (introduced in Chapter 2) can be used to implement a container class such as a.  C++ classes (introduced in Chapter 2) can be used to implement a container class such as a bag. p The class definition includes: class bag { public: bag( ); void insert(... void erase(......and so on 4 4 The heading of the definition 4 4 A constructor prototype 4 4 Prototypes for public member functions

28 The bag Class  C++ classes (introduced in Chapter 2) can be used to implement a container class such as a bag. p The class definition includes: class bag { public: bag( ); void insert(... void erase(......and so on private: }; 4 4 The heading of the definition 4 4 A constructor prototype 4 4 Prototypes for public member functions 4 4 Private member variables We’ll look at private members later. We’ll look at private members later.

29 The bag ’s Default Constructor p Places a bag in the initial state (an empty bag) bag::bag( ) // Postcondition: {... }

30 The bag ’s Default Constructor p Places a bag in the initial state (an empty bag) bag::bag( ) // Postcondition: The bag has been initialized // and it is now empty. {... }

31 The insert Function p Inserts a new number in the bag void bag::insert(const int& new_entry) // Precondition: // Postcondition: {... }

32 The insert Function p Inserts a new number in the bag void bag::insert(const int& new_entry) // Precondition: The bag is not full. // Postcondition: A new copy of new_entry has // been added to the bag. {... }

33 The size Function p Checks how many integers are in the bag. int bag::size( ) const //Precondition: //Postcondition: {... }

34 The size Function p Checks how many integers are in the bag. int bag::size( ) const // Postcondition: The return value is the number // of integers in the bag. {... }

35 The size Function p Checks how many integers are in the bag. size_t bag::size( ) const // Postcondition: The return value is the number // of integers in the bag. {... }

36 The count Function p Counts how many copies of a number occur size_t bag::count(const int& target) const //Precondition: //Postcondition: {... }

37 The count Function p Counts how many copies of a number occur size_t bag::count(const int& target) const // Postcondition: The return value is the number // of copies of target in the bag. {... }

38 The erase_one Function p Removes (erase) one copy of a number void bag::erase_one(const int& target) //Precondition: //Postcondition: {... }

39 The erase_one Function p Removes (erase) one copy of a number void bag::erase_one(const int& target) // Postcondition: If target was in the bag, then // one copy of target has been removed from the // bag; otherwise the bag is unchanged. {... }

40 The Header File and Implementation File  The programmer who writes the new bag class must write two files: p bag1.h, a header file that contains documentation and the class definition  bag1.cxx, an implementation file that contains the implementations of the bag ’s member functions bag’s documentation bag’s class definition Implementations of the bag’s member functions

41 Using the bag in a Program  Here is typical code from a program that uses the new bag class: bag ages; // Record the ages of three children: ages.insert(4); ages.insert(8); ages.insert(4);

42 Documentation for the bag Class p The documentation gives prototypes and specifications for the bag member functions. p Specifications are written as precondition/postcondition contracts.  Everything needed to use the bag class is included in this comment. bag’s documentation bag’s class definition Implementations of the bag’s member functions

43 The bag ’s Class Definition p After the documentation, the header file has the class definition that we’ve seen before: bag’s documentation bag’s class definition Implementations of the bag’s member functions class bag { public: bag( ); void insert(... void erase(......and so on private: … };

44 The Implementation File p As with any class, the actual definitions of the member functions are placed in a separate implementation file.  The implementations of the bag ’s member functions are in bag1.cxx. bag’s documentation bag’s class definition Implementations of the bag’s member functions

45 A Quiz Suppose that a Mysterious Benefactor provides you with the bag class, but you are only permitted to read the documentation in the header file. You cannot read the class definition or implementation file. Can you write a program that uses the bag data type ? ¬Yes I can.  No. Not unless I see the class definition for the bag.  No. I need to see the class definition for the bag, and also see the implementation file.

46 A Quiz Suppose that a Mysterious Benefactor provides you with the Bag class, but you are only permitted to read the documentation in the header file. You cannot read the class definition or implementation file. Can you write a program that uses the bag data type ? ¬Yes I can. You know the name of the new data type, which is enough for you to declare bag variables. You also know the headings and specifications of each of the operations. You know the name of the new data type, which is enough for you to declare bag variables. You also know the headings and specifications of each of the operations.

47 Using the bag in a Program  Here is typical code from a program that uses the new bag class: bag ages; // Record the ages of three children: ages.insert(4); ages.insert(8); ages.insert(4);

48 Implementation Details p How to store the entries of a bag? An array of integers

49 Implementation Details p The entries of a bag will be stored in the front part of an array, as shown in this example. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... An array of integers 484 We don't care what's in this part of the array.

50 Implementation Details p The entries may appear in any order. This represents the same bag as the previous one... An array of integers 448 We don't care what's in this part of the array. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]...

51 Implementation Details p... and this also represents the same bag. An array of integers We don't care what's in this part of the array. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

52 Implementation Details p We also need to keep track of how many numbers are in the bag. An array of integers 844 We don't care what's in this part of the array. An integer to keep track of the bag's size 3 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]...

53 An Exercise Use these ideas to write a list of private member variables could implement the bag class. You should have two member variables. Make the bag capable of holding up to 20 integers. You have 60 seconds to write the declaration.

54 An Exercise class bag { public:... private: int data[20]; size_t used; }; One solution:

55 An Exercise A more flexible solution: class bag { public: static const size_t CAPACITY = 20;... private: int data[CAPACITY]; size_t used; };

56 The Invariant of a Class p Two rules for our bag implementation  The number of items in the bag is stored in the member variable used ;  For an empty bag, we don’t care what is stored in any of data ; for a non-empty bag, the items are stored in data [0] through data [ used -1], and we don’t care what are stored in the rest of data.  The rules that dictate how the member variables of a (bag) class are used to represent a value (such as a bag of items) are called the invariant of the class

57 The Invariant of a Class  The invariant of the class is essential to the correct implementation of the class’s functions p In some sense, p the invariant of a class is a condition that is an implicit part of every function’s postcondition p And (except for the constructors) it is also an implicit part of every function’s precondition.

58 The Invariant of a Class p Precondition and Postcondition p contract for each function, for use of the function p document pre- and post- in the header file  The invariant of the class p implicit part of pre- and post- so is not usually written as an explicit part of pre- and post- p about the private member variables, thus for implementation, but not for how to use them p documented in the implementation file p Value Semantics p both for implementation and for use p documented in the header file

59 An Example of Calling insert void bag::insert(const int& new_entry) Before calling insert, we might have this bag b: 2 [ 0 ] [ 1 ] [2] b.data b.used

60 An Example of Calling insert void Bag::insert(int new_entry) b.data b.used We make a function call b.insert(17) What values will be in b.data and b.count after the member function finishes ? 2 [ 0 ] [ 1 ] [2] void bag::insert(const int& new_entry)

61 An Example of Calling insert void Bag::insert(int new_entry) After calling b.insert(17), we will have this bag b: 3 [ 0 ] [1] [ 2 ] void bag::insert(const int& new_entry) b.data b.used 2 [ 0 ] [ 1 ] [2]... 84

62 Pseudocode for bag::insert (try it yourself first)

63 Pseudocode for bag::insert  assert(size( ) < CAPACITY);  Place new_entry in the appropriate location of the data array.  Add one to the member variable count. What is the “appropriate location” of the data array ?

64 Pseudocode for bag::insert  assert(size( ) < CAPACITY);  Place new_entry in the appropriate location of the data array.  Add one to the member variable count. data[used] = new_entry; used++;

65 Pseudocode for bag::insert  assert(size( ) < CAPACITY);  Place new_entry in the appropriate location of the data array.  Add one to the member variable count. data[ used++] = new_entry;

66 p A container class is a class that can hold a collection of items. p Container classes can be implemented with a C++ class. p The class is implemented with p a header file (containing documentation and the class definition) bag1.h and bag1.h p an implementation file (containing the implementations of the member functions) bag1.cxx. bag1.cxx p Other details are given in Section 3.1, which you should read, especially the real bag code bag codebag code Summary

67 Outline for Lecture 5 (Next Lecture) p Bag class definition/implementation details p Inline functions p constructor, size p Other basic functions p insert, erase_one, erase, count p More advanced functions p operators +, +=, - p Time Analysis p Big-O p Introduction to sequence

68 Before Lecture 5 p Reading Assignment: Chapter 3 p Reminder: Programming assignment 1 is due on Wednesday

69 Outline for Lecture 5 p Bag class definition/implementation details p Inline functions p constructor, size p Other basic functions p insert, erase_one, erase, count p More advanced functions p operators +, +=, - p Time Analysis p Big-O p Introduction to sequence

70 Review: Container Classes

71 p A container class is a class that can hold a collection of items. p Container classes can be implemented with a C++ class. p The class is implemented with p a header file (containing documentation and the class definition) bag1.h and bag1.h p an implementation file (containing the implementations of the member functions) bag1.cxx. bag1.cxx p Other details are given in Section 3.1 Review: Container Classes

72 Review : Invariants of a class p Precondition and Postcondition  The invariant of the class p Value Semantics

73 Review : Invariants of a class p Precondition and Postcondition p contract for each function, for use of the function p document pre- and post- in the header file  The invariant of the class p implicit part of pre- and post- so is not usually written as an explicit part of pre- and post- p about the private member variables, thus for implementation, but not for how to use them p documented in the implementation file p Value Semantics p both for implementation and for use p documented in the header file

74 Default Constructor and Basic bag Operations p bag() p {used = 0;} p size_t size() const p {return used;} class bag { public: static const size_t CAPACITY = 20; bag(); size_t size() const; bool erase_one(const int& entry);... private: int data[CAPACITY]; size_t used; };

75 Default Constructor and Basic bag Operations p bag() p {used = 0;} p size_t size() const p {return used;} class bag { public: static const size_t CAPACITY = 20; bag() {used =0;} size_t size() const {return used;} bool erase_one(const int& entry);... private: int data[CAPACITY]; size_t used; }; Question: What are inline functions?

76 More bag Operations p void bag::erase(const int& target) p void bag::insert(const int& entry) {assert(size()<CAPACITY); {assert(size()<CAPACITY); data[used]=entry; data[used]=entry; used++; used++;} p bool bag::erase_one(const int& target) p size_t bag::count(const int& target) const

77 Append Operator += void bag::operator+=(const bag& addend) // Precondition: // Postcondition: { }

78 Append Operator += void bag::operator+=(const bag& addend) // Precondition: size( ) + addend.size( ) <= CAPACITY. // Postcondition: Each item in addend has been added to this bag. { }

79 Append Operator += void bag::operator+=(const bag& addend) // Precondition: size( ) + addend.size( ) <= CAPACITY. // Postcondition: Each item in addend has been added to this bag. { size_t i; assert(size( ) + addend.size( ) <= CAPACITY); for (i = 0; i< addend.used; ++i) { data[used] = addend.data[i]; ++used; } } // calling program: a += b; // Question : What will happen if you call: b += b; // Solution : Can you fix the bug in the code?

80 Append Operator += void bag::operator+=(const bag& addend) // Precondition: size( ) + addend.size( ) <= CAPACITY. // Postcondition: Each item in addend has been added to this bag. // Library facilities used: algorithm, cassert { assert(size( ) + addend.size( ) <= CAPACITY); copy(addend.data, addend.data + addend.used, data + used); used += addend.used; } // copy (, ending location>, );

81 Union Operator + // NONMEMBER FUNCTION for the bag class: bag operator+(const bag& b1, const bag& b2) // Precondition: // Postcondition: { }

82 Union Operator + // NONMEMBER FUNCTION for the bag class: bag operator+(const bag& b1, const bag& b2) // Precondition: b1.size( ) + b2.size( ) <= bag::CAPACITY. // Postcondition: The bag returned is the union of b1 and b2. { }

83 Union Operator + // NONMEMBER FUNCTION for the bag class: bag operator+(const bag& b1, const bag& b2) // Precondition: b1.size( ) + b2.size( ) <= bag::CAPACITY. // Postcondition: The bag returned is the union of b1 and b2. // Library facilities used: cassert { bag answer; assert(b1.size( ) + b2.size( ) <= bag::CAPACITY); answer += b1; answer += b2; return answer; } // calling program: c =a+b; // Question : what happens if you call a =a+b ? // Question : why operator+ is a nonmember function ?

84 Subtract Operator - // bag operator-(const bag& b1, const bag& b2); //precondition: //Postcondition: For two bags b1 and b2, the bag x-y contains all the items of x, with any items from y removed // Write your implementation (ask yourself the following questions:) 1. 1.If there is one copy of a target (say 4) in b2 but multiple copies of 4 in b1, how many copies of the target will be in the resulting bag? 2. 2.If there is one copy of a target in b2, but the target is not in b1, how will it affect the resulting bag? 3. 3.Is operator- a member function, nonmember function or friend function?

85 Subtract Operator - // Prototype: NONMEMBER friend FUNCTION for the bag class: // bag operator-(const bag& b1, const bag& b2); // Postcondition: For two bags b1 and b2, the bag x-y contains all the items of x, with any items from y removed // Write your implementation (3 minutes) // HINTS: // 1. A friend function can access private member variables of a bag // 2. You cannot change constant reference parameters // 3. You may use any member functions of the bag class such as //b1.count(target); // how many target is in bag b1? //b1.erase_one(target); // target is an integer item //b2.size(); // size of the bag b2; // bag b3(b2); // automatic copy constructor //

86 Subtract Operator - // NONMEMBER friend FUNCTION for the bag class: bag operator-(const bag& b1, const bag& b2) // Postcondition: For two bags b1 and b2, the bag x-y contains all the items of x, with any items from y removed { size_t index; bag answer(b1); // copy constructor size_t size2 = b2.size(); // use member function size for (index = 0; index < size2; ++index) { int target = b2.data[index]; // use private member variable if (answer.count(target) ) // use function count answer.erase_one(target); // use function erase_one } return answer; }

87 Other Kinds of Bags p In this example, we have implemented a bag containing integers. p But we could have had a bag of float numbers, a bag of characters, a bag of strings... Suppose you wanted one of these other bags. How much would you need to change in the implementation ? Section 3.1 gives a simple solution using the C++ typedef statement.

88 Time Analysis of the Bag Class p Default constructor p p count – the number of occurrence p erase_one – remove one from the bag p erase – remove all

89 Time Analysis of the Bag Class p += - append p b1+b2 - union p insert – add one item p size – number of items in the bag

90 What’s the most important, then? p Concept of Container Classes p the bag class is not particularly important p Other kinds of container classes p sequence – similar to a bag, both contain a bunch of items. But unlike a bag, the items in a sequence is arranged in order. p will be the topic of our second assignment– paying attention to the differences p index – have current, next, last, etc p member functions and their implementation (e.g. insert, attach) p time analysis (insert)

91 A Quiz: Implement a member function of the bag class (you have 5 – 10 minutes) p bool bag::erase_one(const int& target)

92 After Class… p Assignment 2 (online tonight) p due Thursday, Sep 28 p Reading: Chapter 3, Section p especially the sequence code sequence codesequence code p Self-Test Exercises p 1,3, 5,10,11,14,18-24 p Reading for next lecture p Chapter 4, Section

93 T HE E ND Presentation copyright 1997, Addison Wesley Longman For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact. This lecture was modified from the above noticed presentation with new conventions provided in the second edition (2001) of the textbook and other minor changes -- Z. Zhu, , CCNY