template<class T> class myarr { int capacity; int size; T* arr; public: myarr(int a) { arr = new T[a]; size = 0; capacity = a; } myarr(myarr<T>& a) { arr = new T[a.capacity]; size = a.size; capacity = a.capacity; } ~myarr() { if (arr != NULL) { delete[] arr; size = 0; capacity = 0; } } myarr& operator=(myarr<T>& a) { if (arr != NULL) { delete[] arr; size = 0; capacity = 0; } arr = new T[a.capacity]; capacity = a.capacity; size = a.size; return *this; } void tail_insert(T& a) { if (size == capacity) { cout << "已经满了,不能再插了" << endl; return; } arr[size] = a; size++; } void tail_delete() { if (size == 0) { cout << "没有可以删的" << endl; return; } size--; } T& operator[](int n) { if (n > size) { cout << "超范围了" << endl; } return arr[n]; } }; class people { int id; string name; public: people() {} people(int id_, string name_) :id(id_), name(name_) {} void display() { cout << "学号是:" << id << " 姓名是:" << name << endl; } }; void test() { people a(50, "Michelle"); people b(49, "tom"); people c(48, "Adam"); myarr<people> shuzu(3); myarr<people> aa = shuzu; aa.tail_insert(a); aa.tail_insert(b); aa.tail_insert(c); aa[0].display(); aa.tail_delete(); aa[3].display(); } int main() { test(); return 0; }
标签:aa,25,arr,capacity,tail,myarr,cpp,模板,size From: https://www.cnblogs.com/wlxdaydayup/p/17354273.html