C++ Develop a class called myVector which is a simple version of the container.
ID: 3689082 • Letter: C
Question
C++
Develop a class called myVector which is a simple version of the container. The class you created should provide some public functions for client to use like push_back, erase, getsize, etc. The class header file I provide is as below:
template
class myVector{
public:
myVector(); //the default constructor, make size =0, arr = NULL
myVector(myVector &otherVector); // copy constructor
~myVector(); //destructor, delete the arr and reset size
void push_back(myType valueIn); // insert valueIn to the end of arr. Size should increased by 1
void erase(myType target); // erase all copies of valueIn in dynamic array, ex: if arr contains 1, 2, 1, 3. then erase(1) will erase all 1s
int getSize(); // return the current size
myType& operator[] (const int index); //operator overloading
void print(); //print all the data in arr
private:
myType *arr; // dynamic array
int size; // current size for arr
};
myVector has two private members: arr is the dynamic array (pointer), size is the current number of elements in dynamic array.
Please write the code for public functions
Explanation / Answer
myVector()
{ arr = new myType;
arr = NULL; size =0;
}
myVector(myVector &otherVector)
{
arr = new myType;
arr = otherVector.arr;
size = otherVector.size;
}
~myVector()
{
delete arr; size = 0;
}
void erase(myType target)
{
int getSize()
{ return size; }
void print()
{
cout<<" Array is ";
for(int i=0;i<size;i++)
cout<<endl<<arr[i];
}
myType& operator[] (const int index)
{
myType *temp;
temp = arr[index] ;
return temp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.