



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
You will implement a DynamicArray ADT (Abstract Data Type), a C++ class that will allow us to use a dynamic integer array without worrying about all the ...
Typology: Summaries
1 / 5
This page cannot be seen from the preview
Don't miss anything!




class DynamicArray { private : int *a; int s; int grow( int s2); // Optional public : // Constructors DynamicArray(); DynamicArray( int s); DynamicArray( int *a2, int s2); DynamicArray( const DynamicArray &da); ~DynamicArray(); // Member functions int getSize(); // Already implemented int setValue( int pos, int v); int getValue( int pos, int &v) const ; // Operator overload DynamicArray& operator =( const DynamicArray &da2); bool operator ==( const DynamicArray &da2) const ; friend ostream& operator <<(ostream &os, DynamicArray &da); int & operator []( int pos); int operator []( int pos) const ;
Had to grow the array. SECOND ASSIGNMENT Had to grow the array. THIRD ASSIGNMENT Did not have to grow the array. ARRAY A1: 37 -1 7 -1 23 ARRAY A2: 42 42 42 42 42 ARRAY A3: 42 42 42 42 42 ARRAY A4: 42 42 42 42 42 ACCESSING VALUES Position 2: 7 Position out of range. COMPARISONS
/* Grows the array to size “s2”. This means copying the current array
static const int SET_OK_NOGROW = 0; static const int SET_OK_GROW = 1; static const int SET_ERROR_NEGPOS = 2; static const int SET_ERROR_NEGVALUE = 3; Exercise 3 <<5 points>>
/* Returns the value in position "pos" using parameter "v".
/* Assignment operator overload / DynamicArray& operator =( const DynamicArray &da2); / Equality operator overload.
Exercise 5 <<5 points>>
friend ostream& operator <<(ostream &os, DynamicArray &da); Exercise 6 <<10 points>>
int & operator []( int pos); int operator []( int pos) const ;
int a[5] = {42,42,42,42,42}; DynamicArray a1(a,5); cout << a1[3]; // Prints out 42
int a[5] = {42,42,42,42,42}; DynamicArray a1(a,5); cout << a1[3]; // R-Value version, prints out 42 a[1] = 5; // L-Value version, doesn't grow the array cout << a1[3]; // R-Value version, prints out 5 a[10] = 37; // L-Value version, has to grow the array