#includeusing namespace std;templateclass MyArray{private: BT *array; int size; int curr_size;public: MyArray(int x) : array(nullptr), size(0){ if(x <=>=> cout<"can't construct="" array="">"can't><><> exit(EXIT_FAILURE); } size = x; curr_size=0; array = new BT[x]; } MyArray(const MyArray &object) : array(new BT[object.size]), size(object.size){ copy(object.array, object.array + object.size, array); } MyArray& operator=(const MyArray &object){ BT *new_arr = new BT[object.size]; copy(object.array, object.array + object.size, new_arr); delete[] array; array = new_arr; size = object.size; return *this; } void gr(){ BT *temp_arr = new BT[size*2]; for(int i=0;i<> temp_arr[i] = array[i]; } array = temp_arr; size*=2; } void add(BT data){ if(curr_size<> array[curr_size] = data; curr_size++; }else{ gr(); array[curr_size] = data; curr_size++; } } int getSize(){ return curr_size; } void output(){ for(int i=0;i<> cout<><">"> } cout<> } int & operator[](int arr_ind) { if (arr_ind >= curr_size){ cout <> exit(0); } return array[arr_ind]; } ~MyArray() { delete[] array; }};Output from main function above:1 2 3 4 5 6 7 8 9 10Extracted text: Copy the previous program to a new file and implement the following: 1) A class SomeObj with a single integer id as a data member. 2) This class must have a default and user defined constructor. 3) Create an output function to display the id of the object. 4) Modify the MyArray class to create an array of Someobj objects. Test using following main function. int main() { cout <« endl;="">«> a(1); Someobj o1(1); SomeObj o2(2); Someobj o3(3); Someobj o4(4); Someobj o5(5); SomeObj o6(6); Someobj o7(7); SomeObj o8(8); Someobj 09(9); Someobj o10(10); a.add(01); a.add(02); a.add(03); a.add(04); a.add(05); a.add(06); a.add(07); a.add(08); a.add(09); a.add(010); a.output(); cout <« endl; return 0; } endl;="" return="" 0;="">« endl; return 0; }>
void add(BT data){ if(curr_size<> array[curr_size] = data; curr_size++; }else{ gr(); array[curr_size] = data; curr_size++; } } int getSize(){ return curr_size; } void output(){ for(int i=0;i<> cout<><">"> } cout<> } int & operator[](int arr_ind) { if (arr_ind >= curr_size){ cout <> exit(0); } return array[arr_ind]; } ~MyArray() { delete[] array; }};
Output from main function above:1 2 3 4 5 6 7 8 9 10
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here