DynamicString.cpp Study Guide Exam Questions And Answers2024.
Default Constructor - correct answer DynamicString::DynamicString(){ cstr = new char[1]; *(cstr) = '0'; size = 0; } C-String Constructor Creates a copy of the provided c-string. - correct answer DynamicString::DynamicString(const char* str){ size = 0; //Initializing a counter for(int i = 0; str[i] != '0'; i++) { //Counting the number of elements in char array{ size++; } if(size 0) { throw out_of_range("Out of Range"); //Checking if the str is out of range } cstr = new char [size+1]; //Allocate memory for new cstr char array for(int i = 0; i size; i++){ //Copying str to new cstr cstr[i]=str[i]; } cstr[size] ='0'; //Putting a null terminator at the end of the array } Copy-constructor. Performs a deep-copy. - correct answer DynamicString::DynamicString(const DynamicString& other){ cstr = new char[+1]; //create a new array for copy constructor for(int i = 0; i +1; i++) { *(cstr+i) = *(+i); } } Assignment operator. Performs a deep-copy. Returns itself, now containing a deep copy - correct answer DynamicString& DynamicString::operator=(const DynamicString& other){ if(this != &other) { deleteArray(); //Deletes cstr to deal with possible overlapping memory cstr = new char[+1]; //Creates new cstr char array for assignment operator for(int i = 0; i ity(); i++) { *(cstr+i) = *(+i); } } return *this; } De-constructor. Releases memory resources + Method to call delete operator - correct answer DynamicString::~DynamicString(){ deleteArray(); } void DynamicString::deleteArray() //Tests for cstr incase it is already empty, then deletes contents of array if not empty { if(cstr != nullptr) { delete[] cstr; } cstr = nullptr; size = 0; //Reassigns size back to 0; }
Información del documento
- Subido en
- 29 de abril de 2024
- Número de páginas
- 3
- Escrito en
- 2023/2024
- Tipo
- Examen
- Contiene
- Preguntas y respuestas