Unformatted text preview:

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 15Log into LinuxCopy 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 2OutlineClasses and dynamic arraysExample class: bag (dynamic array version)Attributes, private member functionsExplicit-value constructor ReserveInsertDestructorCopy constructorAssignment, i.e., operator=Monday, February 14 CS 215 Fundamentals of Programming II - Lecture 15 3Classes and Dynamic ArraysDynamic 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 AttributesThis 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 ObjectHere 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 DefinitionExamine file bag2.hHas 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 UsageExamine file bag2demo.cppDemo 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 ImplementationExamine file bag2.cppNote 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 ConstructorUses static member constant DEFAULT_CAPACITY as its default argumentAllocates 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 10ReserveThere 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 11InsertIf 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 13DestructorSo 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 14DestructorNeed 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 ConstructorSometimes 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 ConstructorDefault behavior is to copy only


View Full Document

UE CS 215 - LECTURE NOTES

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?