CS计算机代考程序代写 // Workshop 9:

// Workshop 9:
// Collection template
// 2021-06-03
// Version: 1.0
// Author: Fardad Soleimanloo
/////////////////////////////////////////////
#ifndef SDDS_COLLECTION_H_
#define SDDS_COLLECTION_H_
namespace sdds {
template
class Collection {
T* m_data = nullptr;
int m_size = 0;
public:
Collection(int size = 0);
Collection(const Collection& CP);
Collection& operator=(const Collection& RO);
int size()const;
void resize(int newsize);
T& operator[](int index);
Collection& add(const T& element);
~Collection();
};

template
Collection::Collection(int size) :m_size(size) {
if (m_size <= 0) m_size = 0; if (m_size > 0) m_data = new T[m_size];
}
template
Collection::Collection(const Collection& CP) {
operator=(CP);
}
template
Collection& Collection::operator=(const Collection& RO) {
if (this != &RO) {
delete[] m_data;
m_data = new T[m_size = RO.m_size];
for (int i = 0; i < m_size; i++) m_data[i] = RO.m_data[i]; } return *this; } template
int Collection::size()const {
return m_size;
}

template
void Collection::resize(int newsize) {
int i;
T* temp = nullptr;
if (newsize < 0) newsize = 0; if (newsize > 0) {
temp = new T[newsize];
for (i = 0; i < m_size && i < newsize; i++) { temp[i] = m_data[i]; } } delete[] m_data; m_data = temp; m_size = newsize; } template
T& Collection::operator[](int index) {
if (index >= m_size) resize(index + 1);
return m_data[index];
}

template
Collection& Collection::add(const T& element) {
(*this)[size()] = element;
return *this;
}
template
Collection::~Collection() {
delete[] m_data;
}
}

#endif // !SDDS_COLLECTION_H_