简单使用模板定义的array类
定义一个可以保存的数组,并且支持多种类型的array。我们定义的array需要支持以下功能:
- 构造的时候,给予一个整数参数
n
,确定对象的容量(即内部要开多大的数组) - 通过
insert
成员函数在数组内的末尾插入一个元素,如果容量已经满了则输出array full
- 通过
showAll
成员函数,输出所有元素,每个占一行
主函数代码已给出
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template <class T>
class Array {
public:
Array(int n) {
size = n;
cnt = 0;
list = new T [size];
};
Array