C++Please explain the code below.
It doesn't have to be long, as long as you explain what the important parts of the code do. You can also explain it line by line for best ratings. Thank you so much!
#include "list.h"#include #include #include using namespace std;class ArrayList : public List { int* array; int index; int capacity; void dyn_all_add(){ int cap = ceil(capacity * 1.5); array = (int*)realloc(array,cap * sizeof(int)); capacity = cap; } void dyn_all_rem(){ int cap = capacity - (capacity/3); array = (int*)realloc(array,cap * sizeof(int)); capacity = cap; } public: // CONSTRUCTOR ArrayList() { capacity = 4; array = (int*)malloc(capacity); index = 0; } int add(int num) { if (index == capacity){ dyn_all_add(); } *(array + index) = num; index++; return index; } int get(int pos){ if (pos-1 <> return *(array + pos-1); } return -1; } int size(){ return index; } void swap(int pos1, int pos2){ int temp = *(array+pos1-1); *(array+pos1-1) = *(array + pos2-1); *(array+pos2-1) = temp; } // WARNING! Do not modify the print method. // Doing so will nullify your score for this activity. void print() { cout <> for (int i = 0; i < capacity;="" i++)=""> if (i < index)=""> cout < *(array="" +=""> } else { cout <> } if (i != capacity - 1) { cout < ",=""> } } cout < "]"=""><> }};
void dyn_all_rem(){ int cap = capacity - (capacity/3); array = (int*)realloc(array,cap * sizeof(int)); capacity = cap; }
public: // CONSTRUCTOR ArrayList() { capacity = 4; array = (int*)malloc(capacity); index = 0; }
int add(int num) { if (index == capacity){ dyn_all_add(); } *(array + index) = num; index++; return index; }
int get(int pos){ if (pos-1 <> return *(array + pos-1); } return -1; } int size(){ return index; }
void swap(int pos1, int pos2){ int temp = *(array+pos1-1); *(array+pos1-1) = *(array + pos2-1); *(array+pos2-1) = temp; }
// WARNING! Do not modify the print method. // Doing so will nullify your score for this activity. void print() { cout <> for (int i = 0; i < capacity;="" i++)=""> if (i < index)=""> cout < *(array="" +=""> } else { cout <> } if (i != capacity - 1) { cout < ",=""> } } cout < "]"=""><> }};
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here