Data Structures Test 2 Review Dynamic Arrays Flashcards
In the code below, which is the size member variable? class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };
int m_size;
In the code below, what is the default initial capacity? class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };
the default initial capacity is 4
initialize the private member variables Pitcher::Pitcher(int capacity) { [WHAT GOES HERE?] }
m_capacity = capacity;m_size = 0; //init size to 0
// Pitcher.h file class Pitcher { private: : : public: Pitcher(int capacity = 4); [WHAT GOES HERE?] : };
void addTea();
// Pitcher.cpp file void Pitcher::addTea() { [WHAT GOES HERE?] }
++m_size;
// Pitcher.h file class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); void addTea(); [WHAT GOES HERE?] : };
int getCapacity();int getSize();
// Pitcher.cpp file int Pitcher::getCapacity() { [WHAT GOES HERE?] }
return m_capacity;
// Pitcher.cpp file int Pitcher::getSize() { [WHAT GOES HERE?] }
return m_size;
// Pitcher.h file class Pitcher { : public: Pitcher(int capacity = 4); [WHAT GOES HERE?] : };
Pitcher(Pitcher& copyMe);
// Pitcher.cpp file Pitcher::Pitcher(Pitcher& copyMe) { [WHAT GOES HERE?] }
m_capacity = copyMe.m_capacity;m_size = copyMe.m_size;
// Pitcher.h file class Pitcher { : public: [WHAT GOES HERE?] : };
void operator = (const Pitcher& rhs);
// Pitcher.cpp file void Pitcher::operator= (const Pitcher& rhs) { [WHAT GOES HERE?] }
m_capacity = rhs.m_capacity;m_size = rhs.m_size;
// Pitcher.h file class Pitcher { : public: [WHAT GOES HERE?] };
friend bool operator==(const Pitcher& lhs, const Pitcher& rhs);
// Pitcher.cpp file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { [WHAT GOES HERE?] }
bool retVal = false; if (lhs.m_size == rhs.m_size) retVal = true; return retVal;
// Pitcher.h file [WHAT GOES HERE?] class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };
enum Flavor { noFlavorSet = -1, raspberry, peach, lemon, grape, strawberry };
// Pitcher.h file class Pitcher { private: int m_capacity; int m_size; [WHAT GOES HERE?] : public: Pitcher(int capacity = 4); : };
Flavor* mp_serving;
In the code below, which is the capacity member variable? class Pitcher { private: int m_capacity; int m_size; : public: Pitcher(int capacity = 4); : };
int m_capacity;
// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { [WHAT GOES HERE?] // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } } delete [] mp_serving; // free memory used // Assign newArr to mp_serving and update capacity to n mp_serving = newArr; m_capacity = n; }
Flavor& newArr = new Flavor[n]; // alloc memory
// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { Flavor& newArr = new Flavor[n]; // alloc memory [WHAT GOES HERE?] delete [] mp_serving; // free memory used // Assign newArr to mp_serving and update capacity to n mp_serving = newArr; m_capacity = n; }
// if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } }
// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { Flavor& newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } } [WHAT GOES HERE?] // Assign newArr to mp_serving and update capacity to n mp_serving = newArr; m_capacity = n; }
delete [] mp_serving; // free memory used
// Pitcher.cpp file void Pitcher::growPitcher(int n, bool copy) { Flavor& newArr = new Flavor[n]; // alloc memory // if copy is true, loop to copy elements in mp_array to newArr array if (true == copy) { for(int i=0, i < m_size; i++) { newArr[i] = mp_serving[i]; } } delete [] mp_serving; // free memory used [WHAT GOES HERE?] }
// Assign newArr to mp_serving and update capacity to nmp_serving = newArr;m_capacity = n;
// Old Pitcher.cpp file Pitcher::Pitcher(int capacity) { [WHAT GOES HERE?] }
m_capacity = capacity;m_size = 0; // init size to 0
// New Pitcher.cpp file Pitcher::Pitcher(int capacity) { [WHAT GOES HERE?] }
mp_serving = NULL; // always init ptrs growPitcher(capacity, false); m_size = 0; // init size to 0 // init all servings to notSet for (int j=0; j < m_capacity; j++) { mp_serving[j] = noFlavorSet; }
// Old Pitcher.h file class Pitcher { private: : : public: [WHAT GOES HERE?] : };
void addTea();
// New Pitcher.h file class Pitcher { private: : : public: [WHAT GOES HERE?] : };
void addTea(Flavor flav);
// Old Pitcher.cpp file void Pitcher::addTea() { [WHAT GOES HERE?] }
if ( (m_size + 1) > m_capacity ) { m_capacity = m_capacity * 2; // go with doubling idea here } ++m_size;
// New Pitcher.cpp file void Pitcher::addTea(Flavor flav) { [WHAT GOES HERE?] }
if ( (m_size + 1) > m_capacity ) { growPitcher(m_capacity * 2, true); // go with doubling idea here } mp_serving[m_size] = flav; // set the new serving flavor ++m_size; // add 1 to serving count
// Old Pitcher.cpp file Pitcher::Pitcher(Pitcher& copyMe) { [WHAT GOES HERE?] }
m_capacity = copyMe.m_capacity; m_size = copyMe.m_size;
// New Pitcher.cpp file Pitcher::Pitcher(Pitcher& copyMe) { [WHAT GOES HERE?] }
mp_serving = NULL; // always init ptrs growPitcher(copyMe.m_capacity, false); for (int j=0; j < copyMe.m_capacity; j++) { mp_serving[j] = copyMe.mp_serving[j]; } m_size = copyMe.m_size;
// Old Pitcher.cpp file Pitcher& Pitcher::operator= (const Pitcher& rhs) { [WHAT GOES HERE?] }
m_capacity = rhs.m_capacity;m_size = rhs.m_size;
// New Pitcher.cpp file Pitcher& Pitcher::operator= (const Pitcher& rhs) { [WHAT GOES HERE?] m_size = rhs.m_size; }
if (m_capacity < rhs.m_capacity){ growPitcher(rhs.m_capacity, false);}for (int j=0; j < rhs.m_capacity; j++){ mp_serving[j] = rhs.mp_serving[j];}
// Old Pitcher.cpp file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { [WHAT GOES HERE?] }
bool retVal = false; if (lhs.m_size == rhs.m_size) retVal = true; return retVal;
// New Pitcher.cpp file bool operator== (const Pitcher& lhs, const Pitcher& rhs) { [WHAT GOES HERE?] } return retVal; }
retVal = true;int j = 0;while ( (j < lhs.m_size) && (true == retVal)){ if ( lhs.mp_serving[j] != rhs.mp_serving[j]) { retVal = false; }++j;}
“class Pitcher operator «_space; ostream& operator«_space;(ostream& ostr, const Pitcher& pi) { if ( pi.m_size <= 0) { ostr «_space;"”pitcher is empty””; } for(int i=0; i < pi.m_size; i++) { switch (pi.mp_serving[i]) { case raspberry: ostr «_space;"”raspberry””; break; case peach: ostr «_space;"”peach””; break; : // lines skipped here [WHAT GOES HERE?] } // end switch ostr «_space;”” “”; } // end for return ostr; }”
“default: ostr «_space;"”oops spilled one””; break;”