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();