For this assignment, you will be redefining your own MyVector class using dynami
ID: 3789952 • Letter: F
Question
For this assignment, you will be redefining your own MyVector class using dynamic memory allocation (dynamically allocating arrays).
The MyVector.h file looks like this:
#ifndef MYVECTOR_
#define MYVECTOR_
/*Defines a MyVector type which mirrors the STL vector class. It uses templates and dynamic memory allocation*/
namespace vec
{
typedef int T;
class MyVector
{
private:
int vsize = 0;
T* vec = nullptr;
public:
MyVector();
T operator[] (int index);
void pop_back();
void push_back(T value);
int size(); //returns the vector size
bool empty();//determine if is empty
int search(T Value);
};
}//namespace
#endif
Can anyone help me with this?
Explanation / Answer
Hi
Please find the MyVector type using templates and dynamic allocation .
#include <iostream>
#ifndef MYVECTOR_H
#define MYVECTOR_H
using namespace std;
template<class T>
class MyVector{
public:
MyVector(); // constructor
MyVector(const MyVector &a); // copy constructor
MyVector& operator = (const MyVector &a); // assignment operator
T& operator [] (unsigned int index); // get array item
void Add(const T &item); // Add item to the end of array
unsigned int GetSize(); // get size of array (elements)
void SetSize(unsigned int newsize); // set size of array (elements)
void Clear(); // clear array
void Delete(unsigned int pos); // delete array item
void* getptr(); // get void* pointer to array data
enum exception { MEMFAIL }; // exception enum
bool empty(); //check if it is empty
int search(T Value); // searches for a value
private:
T *array; // pointer for array's memory
unsigned int size; // size of array (elemets)
T * vec = nullptr;
unsigned int realsize; // actual size of allocated memory
const static int dyn_array_step = 128; // initial size of array memory
};
Hope this might help you .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.