337 Tutorials Flashcards
T8:
Assume that there is more code above the following. How many times is the constructor called at point three? How many are default constructors?
#include "mystring.h" int main(void){ MyString s; MyString *s2; { MyString s1 ("Blue"); s2 = new MyString("Red"); MyString s3 ("AB"); } // Point three delete s2; // point four return 0; }
4 constructors.
MyString s; //call 1
MyString s1(“Blue); //call 2
s2 = new MyString(“Red”); //call 3
MyString s3(“AB”) //call 4
Note:
MyString *s2 is just a pointer. It doesn’t call anything.
T8:
How many times is the destructor called at point 3?
#include "mystring.h" int main(void){ MyString s; MyString *s2; { MyString s1 ("Blue"); s2 = new MyString("Red"); MyString s3 ("AB"); } // Point three delete s2; // point four return 0; }
2 destructors.
s1 will be destroyed when it hits the brackets
s3 will be destroyed when it hits the brackets
Note that s2 will not be destroyed because it is pointing to a dynamic memory location.
T8:
How many times is the destructor called at point 4?
#include "mystring.h" int main(void){ MyString s; MyString *s2; { MyString s1 ("Blue"); s2 = new MyString("Red"); MyString s3 ("AB"); } // Point three delete s2; // point four return 0; }
3 destructors:
s1 will be destroyed when it hits the brackets
s3 will be destroyed when it hits the brackets
s2 will be destroyed explicitly with the delete.
T8:
How many times is the destructor called at the end of the program?
#include "mystring.h" int main(void){ MyString s; MyString *s2; { MyString s1 ("Blue"); s2 = new MyString("Red"); MyString s3 ("AB"); } // Point three delete s2; // point four return 0; }
4 destructors in total. Everything will be destroyed. This makes logical sense since there are 4 constructors.
T8 B: Consider the following program. Write the default constructor for SimpleArray() #include <iostream> typedef int Type;// Type is an alias name for int. class SimpleArray { public: SimpleArray(); // PROMISES: size will zero int size() const; private: int sizeM; Type *storageM; };
Start with our default constructor SimpleArray::SimpleArray():size(0), storageM(nullptr){} Here for C++ it is a better idea to use a nullptr instead of 0 or NULL.
T8 B: Consider the following program. Write the constructor for SimpleArray() (NOT DEFAULT)
#include <iostream> typedef int Type;// Type is an alias name for int. class SimpleArray { public: SimpleArray(); SimpleArray(const Type *a, int size); // REQUIRES: size > 0, a points to an array of Type // PROMISES: dynamically allocates memory and initializes all elements of // storageM with values in the array that a pointer a points to. int size() const; private: int sizeM; Type *storageM; };
//constructor SimpleArray::SimpleArray(const Type *a, int size):sizeM(size){ storageM = new Type[size]; if (storageM == nullptr){ cerr << "Memory not available" << endl; exit(1); } for (int i = 0; i <size; i++){ storageM[i] = a[i]; } }
T8 B: Consider the following program. Write the destructor ~SimpleArray(); #include <iostream> typedef int Type;// Type is an alias name for int. class SimpleArray { public: SimpleArray(); SimpleArray(const Type *a, int size); ~SimpleArray(); int size() const; private: int sizeM; Type *storageM; };
SimpleArray::~SimpleArray(){ sizeM = 0; delete [] storageM; }
T8 B: Consider the following program. Write the function for size(),which only returns the value of size.
#include <iostream> typedef int Type;// Type is an alias name for int. class SimpleArray { public: SimpleArray(); SimpleArray(const Type *a, int size); ~SimpleArray(); int size() const; private: int sizeM; Type *storageM; };
//function for returning sizeM
int SimpleArray::size()const{
return sizeM;
}
T8 B: Consider the following program. Write the function for the “at” function. It must check that the index is in the range.
#include <iostream> typedef int Type;// Type is an alias name for int. class SimpleArray { public: SimpleArray(); SimpleArray(const Type *a, int size); ~SimpleArray(); int size() const; Type at(int index) const; // REQUIRES: 0 <= index < sizeM // PROMISES: returns the value of storageM at index. void append(const SimpleArray& other); private: int sizeM; Type *storageM; };
Type SimpleArray::at(int index)const{ assert(index >= 0 && index < sizeM); return storageM[index]; }
T8 B: Consider the following program. Write the function for the append function.Hint, Type *newStorageM = ??
#include <iostream> typedef int Type;// Type is an alias name for int. class SimpleArray { public: SimpleArray(); SimpleArray(const Type *a, int size); ~SimpleArray(); int size() const; Type at(int index) const; void append(const SimpleArray& other); //PROMISES: appends values in each element of other.storageM to the end this-storageM private: int sizeM; Type *storageM; };
We want to pass in a new simple array and add it to the end of what we have. In C++ we don't have realloc. Note that since other is a reference and not a pointer we can use the dot notation. void SimpleArray::append(const SimpleArray&other){ Type *newStorageM = new Type[sizeM+other.sizeM]; //checks to see if memory is allocated or not if(newStorageM == nullptr){ cerr<<"Memory not avaliable"<<endl; exit(1); } // we need to copy all the information through for(int i = 0; i <sizeM; i++){ newStorageM[i] = storageM[i]; } delete [] storageM; // we can now delete old //we want to append. for(int i = 0; i <other.sizeM; i++) newStorageM[i+sizeM] = other.storageM[i]; storageM = newStorageM; sizeM = sizeM+other.sizeM }
T9B: Consider the following code. Write the display() function so that it can display all the values of the linked list one at a time. #include <iostream> using namespace std; struct Node { int item; Node *next; }; class List { public: List():headM(0){} void insert(int the_item); void display(); void insert_n(int the_item); private: Node *headM; }; void List::insert(int the_item){ Node *new_node = new Node; new_node->item = the_item; if(headM == NULL) { headM = new_node; new_node -> next = NULL; } else{ new_node -> next = headM; headM = new_node; } } Hint:You can use Node *current = headM;
void List::display(){ //puts the headM into a copy, which is current //this allows us to iterate without changing headM Node *current = headM; //nullptr is the better choice over //0 and null while(current != nullptr){ cout<<current ->item <<endl; current = current -> next; } }
T9B:
Consider the following function insert_n. Let’s say that we want it to go from smallest to largest.
void List::insert_n(int the_item){ //declares a new node pointer. We fill it's content with the item. Node *new_node = new Node; new_node -> item = the_item; //check if headM is NULL if(headM == NULL){ headM = \_\_\_\_\_\_\_\_; \_\_\_\_\_\_\_\_\_\_\_ = NULL; } else{ //checks whether the item is smaller than headM if(headM ->item > the_item){ //the new_node next points to the old location new_node->next = \_\_\_\_\_\_\_; //assign the head to new_node headM = new_node; } else{ Node *current = headM; while(current -> next != NULL){ if(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ > the_item) break; current = \_\_\_\_\_\_\_\_\_\_\_; } new_node ->next = current -> next; current ->next = \_\_\_\_\_\_\_\_\_\_; } } }
void List::insert_n(int the_item){ //declares a new node Node *new_node = new Node; new_node -> item = the_item; if(headM == NULL){ headM = new_node; new_node -> next = NULL; } else{ //checks whether the item is smaller than headM if(headM ->item > the_item){ //the new_node next points to the old location new_node->next = headM; //assign the head to new_node headM = new_node; } else{ Node *current = headM; while(current -> next != NULL){ if(current->next->item > the_item) break; current = current ->next; } new_node ->next = current -> next; current ->next = new_node; } } }
T10 A Consider the following code. What will be the output for point 1? #include<iostream> using namespace std; void func(double **m){ cout << "\n *m[0] = " << *m[0]; cout <<"\n *m[1] = " << *m[1]; cout << "\n (*m)[1] = " << (*m)[1]; cout <<"\n **m = "<< **m; cout <<"\n **(m+1)) = "<< **(m+1); cout <<"\n m[1][1] = " << m[1][1]; // POINT ONE } int main() { int row = 3; int col = 4; double* p[4]; double x[]={100, 340.3, 55.6, 103, 134.5, 155.6, 203, 234.5, 255.6, 303, 334.5, 355.6}; for(int i = 0, j = 0; i<row ; j+= col, i++) p[i] = x + j; func(p); return 0; }
The output will be:
100
134.5
340.3
100
134.5
155.6
For the following code, write the definition for insert struct Node { int item; Node *next; }; class List { public: List(); // PROMISES: Creates empty list. ~List(); void insert(const int& the_item); // PROMISES: Node with item == the_item is inserted in the linked list that goes from smallest to largest void remove(const int& the_item); // PROMISES: Node with item == the_item is removed private: Node *headM; void copy(const List& src); void destroy(); }; void List::insert(const int& d) { Node **p; Node *new_node = new Node; // complete this function assert(new_node != \_\_\_\_\_\_\_); new_node->item = \_\_; if(headM == \_\_\_\_\_\_\_ || headM -> item > \_\_\_){ new_node ->next = \_\_\_\_\_\_\_; \_\_\_\_\_\_\_\_ = new_node; } else{ p = &headM; while((*p) != nullptr && (*p)->item < \_\_){ p = \_\_\_\_\_\_\_\_\_\_; } new_node ->next = *p; *p = new_node; } }
void List::insert(const int& d) { Node **p; Node *new_node = new Node; // complete this function assert(new_node != nullptr); new_node->item = d; if(headM == nullptr || headM -> item >d){ new_node ->next = headM; headM = new_node; } else{ p = &headM; while((*p) != nullptr && (*p)->item <d){ p = &(*p) -> next; } new_node ->next = *p; *p = new_node; } }
For the following code, write the definition for remove(); struct Node { int item; Node *next; }; class List { public: List(); // PROMISES: Creates empty list. ~List(); void insert(const int& the_item); // PROMISES: Node with item == the_item is inserted in the linked list that goes from smallest to largest void remove(const int& the_item); // PROMISES: Node with item == the_item is removed private: Node *headM; void copy(const List& src); void destroy(); }; void List::remove(const int& d) { Node **prev; prev = \_\_\_\_\_\_; while((*prev != \_\_\_\_\_\_\_) && ((*prev)->item != \_\_)){ prev = \_\_\_\_\_\_\_\_ ->next; } if (*prev != \_\_\_\_\_\_\_ && (*prev)-> item == \_\_\_){ Node *current = \_\_\_\_\_\_\_; \_\_\_\_\_\_\_ = (*prev)->next; delete \_\_\_\_\_\_\_\_\_; } }
void List::remove(const int& d) { Node **prev; prev = &headM; while((*prev != nullptr) && ((*prev)->item !=d)){ prev = &(*prev) ->next; } if (*prev != nullptr && (*prev)-> item == d){ Node *current = (*prev); *prev = (*prev)->next; delete current; } }
Let’s assume we have a classed called Matrix. We then want to make a constructor such that given c and r, it will create a matrix and initialize them with the values 0.0
Matrix::Matrix(int r, int c): colsM(c), rowsM(r){ storageM = new int* [\_\_\_\_\_\_]; assert(\_\_\_\_\_\_\_ != \_\_\_\_\_\_\_\_); for(int i = 0; i<r; i++){ storageM[i] = new int[\_\_\_\_\_\_]; assert(\_\_\_\_\_\_\_\_\_ != \_\_\_\_\_\_\_\_); for(int j = 0; j<colsM; j++) \_\_\_\_\_\_\_\_\_\_[i][j] = 0.0; } }
Matrix::Matrix(int r, int c): colsM(c), rowsM(r){ storageM = new int* [rowsM]; assert(storageM != nullptr); for(int i = 0; i<r; i++){ storageM[i] = new int[colsM]; assert(storageM[i] != nullptr); for(int j = 0; j<colsM; j++) storageM[i][j] = 0.0; } }