using namespace std;
template<class T>
class myarray
{
public:
myarray(int capacity)
{
this->m_capacity = capacity;
this->m_size = 0;
this->paddress = new T(m_capacity);
}
myarray(const myarray& arr)
{
this->m_capacity = arr.m_capacity;
this->m_size = arr.m_size;
this->paddress = new T[arr.m_capacity];
for (int i = 0; i < this->m_size; i++)
{
this->paddress[i] = arr.paddress[i];
}
}
myarray& operator=(const myarray& arr)
{
if (paddress != NULL)
{
delete[] paddress;
paddress = NULL;
m_capacity = 0;
m_size = 0;
}
m_capacity = arr.m_capacity;
m_size = arr.m_size;
paddress = new T(arr.m_capacity);
for (int i = 0; i < this->m_size; i++)
{
paddress[i] = arr.paddress[i];
}
return *this;
}
~myarray()
{
if (this->paddress != NULL)
{
delete[] this->paddress;
this->paddress = NULL;
}
}
void push_back(const T& val)
{
if (m_capacity == m_size)
{
return;
}
paddress[m_size] = val;
m_size++;
}
void delete_back()
{
if (m_size == 0)
{
return;
}
m_size--;
}
T& operator[](int index)
{
return paddress[index];
}
int getcapacity()
{
return m_capacity;
}
int getsize()
{
return m_size;
}
void printmyarray()
{
for (int i = 0; i < m_size; i++)
{
cout << paddress[i] << " ";
}
cout << endl;
}
private:
T* paddress;
int m_capacity;
int m_size;
};
class person
{
private:
string m_name;
int height;
public:
person()
{
m_name = "name"; height = 0;
}
person(string name, int a)
{
m_name = name;
height = a;
}
};
int main()
{
myarray<int> arr(10);
for (int i = 0; i < 10; i++)
{
arr[i] = i;
}
arr.printmyarray();
}