Question: Given class arrayWrapper defined in the header file wrapper.h , show the implementation of method resize as it appears in the implementation file ( i

Given class "arrayWrapper" defined in the header file wrapper.h, show the implementation of method "resize" as it appears in the implementation file (i.e., wrapper.cpp).
The resize method is designed to change the size of the dynamic array while maintaining the existing data. It should contain steps to:
Creating a New Array:
A new dynamic array (newArr) of type T is created with the size specified by newSize
Copying Old Values:A loop iterates through the existing array (arr) and copies values to the new array (newArr). The number of values copied is determined by the smaller of the old size and the new size
Deallocating Old Memory; The old array (arr) is deallocated
Updating Pointers and Size:
The pointer arr is updated to point to the new array
The size variable is updated to reflect the new size
#include
using namespace std;
template
class ArrayWrapper {
private:
T* arr;
int size;
public:
ArrayWrapper(int s) : size(s){
arr = new T[size];
for (int i =0; i < size; i++){
arr[i]= T(); // Default initialize the array elements
}
}
~ArrayWrapper(){
delete[] arr; // Proper memory deallocation
}
void set(int index, T value){
if (index >=0 && index < size){
arr[index]= value;
}
}
T get(int index){
return arr[index];
}
void resize(int newSize);
};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!