Lecture 15OutlineClasses and Dynamic ArraysBag Class AttributesBag ObjectBag Class DefinitionBag Class UsageBag Class ImplementationExplicit-Value ConstructorReserveInsertoperator+=Destructor 1Destructor 2Copy Constructor 1Copy Constructor 2Copy Constructor 3Copy Constructor 4Assignment: operator= 1Assignment: operator= 2Assignment: operator= 3Slide 22Self-AssignmentRules for Dynamic ClassesMonday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 1Lecture 15Log into LinuxCopy files on csserver in /home/hwang/cs215/lecture15/*.*Reminder: Homework 7 due on Wednesday.Questions?Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 2OutlineClasses and dynamic arraysExample class: bag (dynamic array version)Attributes, private member functionsExplicit-value constructor ReserveInsertDestructorCopy constructorAssignment, i.e., operator=Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 3Classes and Dynamic ArraysDynamic allocation is used to implement dynamic data structures like vector<T>.Today we will look at making the bag class dynamic.Idea is that the bag class encapsulates a dynamically-allocated array. It must keep track of its capacity (# of available slots) and as well as its size (actual # of elements).Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 4Bag Class AttributesThis bag class will be implemented using a dynamic array. There are three attributes:data – a pointer to an array of the element type of capacity size to hold the collection.used – an integer that keeps track of the number of used positions as elements are added and removed to the collection.capacity – an integer that keeps track of the amount of memory allocated to dataMonday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 5Bag ObjectHere is a way to think about how this works.bag b(5);b.insert(15);b.insert(34);dataused2capacity515 34[0] [1] [2] [3] [4]Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 6Bag Class DefinitionExamine file bag2.hHas the same typedefs as the original bag class. Has a static constant, but is only used for the default argument to the constructor.A helper function reserve( ) is declared in the private section. This means that the function has access to the private attributes, but is not available to the users of the class. (Textbook has it in the public section, but it is not something a user should have access to.)Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 7Bag Class UsageExamine file bag2demo.cppDemo program is the same as before (Lecture 10). It just asks user for integers, adds them to a bag, and then asks the user for the integers again and removes them from the bag.The difference is that the program does not have to worry about running out of room in the bag. I.e., there is no check against a capacity.Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 8Bag Class ImplementationExamine file bag2.cppNote there are no checks and asserts. Errors in usage are due to running out of memory to allocate. This is indicated when the new operator throws a bad_alloc exception. This is a fairly rare error, so we ignore it and the program will crash if the error happens.Implementations of erase( ), erase_one( ), size( ), and count( ) are the same as before.Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 9Explicit-Value ConstructorUses static member constant DEFAULT_CAPACITY as its default argumentAllocates an array of initial_capacity elements and makes data point to it.Saves initial_capacity in capacity.Initializes used to 0.By providing an explicit-value constructor, the user of the class can increase the efficiency of operations like insert( ) or operator+=.Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 10ReserveThere are several places where we would like to change the size of the array after it has been allocated. The reserve( ) function does this.It uses a local pointer variable to create a new (larger) array, copies the existing elements to the new array, deletes the old array, then makes data point to the new array.It uses the copy function from the <algorithm> library. This function has 3 parameters: the address of the first element to copy, the address one past the last element to copy, the start address to copy to.Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 11InsertIf the data array is not full, the insert( ) function works the same as before.When the data array is full, the function must increase the size of the array. This is done by calling reserve( ). To increase the efficiency of later calls to insert( ) or operator+=, we double the size of the array. (Note: the textbook only adds 1 to the size.)Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 12operator+=Makes sure that the data array is large enough to hold all of the elements by using reserve( ).Uses the copy algorithm to copy the addend's elements. Note that the start address is data+used.Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 13DestructorSo far, so good. But what happens when a Bag object goes out of scope? E.g.,void Function (...) { bag b (5); :}At the end of the function, b is destroyed. Default behavior is to only reclaim the named parts (i.e., parts on the stack). This leaves the data array behind as garbage.Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 14DestructorNeed to write a custom function called a destructor (opposite of constructor). The destructor is called whenever an object needs to be destroyed: when variable goes out of scope or when a dynamically-allocated object is deleted.The name of a destructor is ~<classname>, so the bag class destructor is ~bag()Generally, a destructor reverses the steps of a constructor, so here it deletes the data array.Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 15Copy ConstructorSometimes we need to create a copy of an object. E.g., value parameter or returned object.bag Function (bag b1) { bag b2; : return b2;}int main () { bag b; bag c = Function (b); :}Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 16Copy ConstructorDefault behavior is to copy only
View Full Document