Docsity

Programming Abstractions, Exams of Programming Abstractions

Partial preview of the text

Download Programming Abstractions and more Exams Programming Abstractions in PDF only on Docsity!

Programming Abstractions

Cynthia Lee

C S 1 0 6 B

Topics du Jour:  Make your own classes! (cont.) › Last time we did a BankAccount class (pretty basic) › This time we will do something more like the classes you have used from the Stanford libraries  Arrays in C++ › In order to implement our version of a Vector (we’re calling it ArrayList), we will need an array 2

CS106B Weeks 4-8:

Learn to make the car

Relevant: Trailer to “The Love Bug (Herbie)” (1968) https://www.youtube.com/watch?v=ay3GgrYEa1M

Arrays (11.3) type * name = new type [ length ]; › A dynamically allocated array. › The variable that refers to the array is a pointer. › The memory allocated for the array must be manually released, or else the program will have a memory leak. (>_<)  Another array creation syntax that we will not use: type name [ length ]; › A fixed array; initialized at declaration; can never be resized. › Stored in a different place in memory; the first syntax uses the heap and the second uses the stack. (discussed later)

Initialized? type * name = new type [ length ]; // uninitialized type * name = new type [ length ] () ; // initialize to 0 › If () are written after the array [], it will set all array elements to their default zero-equivalent value for the data type. (slower) › If no () are written, the elements are uninitialized, so whatever garbage values were stored in that memory beforehand will be your elements. int* a = new int[3] ; cout << a[0]; // 2395876 cout << a[1]; // - 197630894 int* a2 = new int3 ; cout << a[0]; // 0 cout << a[1]; // 0

Implementing our ArrayList M a k i n g o u r o w n c o n t a i n e r c l a s s!

Exercise Let's write a class that implements a growable array of integers.  We'll call it ArrayList. It will be very similar to the C++ Vector.  its behavior: › add( value ) insert( index , value ) › get( index ), set( index , value ) › size(), isEmpty() › remove( index ) › indexOf( value ), contains( value ) › toString() ...  We'll start with an array of length ( capacity ) 10 by default, and grow it as needed.

Implementing add (bug)

// in ArrayList.cpp

// BUG

// Socrative: what is the bug in this code?

void ArrayList:: add (int value) {

myElements[mySize] = value;

Implementing add

How do you append to the end of a list? list.add(42);

 place the new value in slot number size

 increment size

index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 0 0 0 0 size 6 capacity (^10) index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 0 0 0 0 size capacity

insert solution // in ArrayList.cpp void ArrayList:: insert (int index, int value) { // shift right to make room for (int i = mySize; i > index; i--) { myElements[i] = myElements[i - 1]; } myElements[index] = value; mySize++; }

Implementing clear

How do you clear the list? list.clear();

 change size to 0

 do we need to zero out all the data?

index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 0 0 0 0 size 6 capacity (^10) index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 0 0 0 0 size capacity

Other members code // in ArrayList.cpp int ArrayList:: get (int index) { return myElements[index]; } void ArrayList:: set (int index, int value) { myElements[index] = value; } int ArrayList:: size () { return mySize; } bool ArrayList:: isEmpty () { return mySize == 0; }

Other members code // in ArrayList.cpp ostream& operator << (ostream& out, const ArrayList& list) { out << "{"; if (!list.isEmpty()) { out << list.get(0); for (int i = 1; i < list.size(); i++) { out << ", " << list.get(i); } } out << "}"; return out; } string ArrayList:: toString () const { ostringstream out; out << *this; return out.str(); }