#include<iostream>
using namespace std;
template <class T>
class templa{
public:
    //初始化
    templa(int n){
        m=n;
        mystr=(T*)malloc(sizeof(T)*n+sizeof(T));
        for (int i =0; i<n; i++) {
            cout<<"请输入第"<<i<<"位要插入的元素:"<<endl;
            cin>>mystr[i];
        }
        cout<<"-----------allen---初始化完成---请指示-----------"<<endl;
    }
    //打印
    void dayin(){
        for (int i = 0; i<m; i++) {
            cout<<mystr[i]<<endl;
        }
         cout<<"-----------allen---dayin完成---请指示-----------"<<endl;
    }
    //增加
    bool add(T t,int n){
        T * temp=(T *)malloc(sizeof(T)*m+sizeof(T)+sizeof(T));
        for (int i =0; i<n; i++) {
            temp[i]=mystr[i];
        }
        temp[n]=t;
        for (int i =n; i<m+1; i++) {
            temp[i+1]=mystr[i];
        }
        delete[] mystr;
        mystr=temp;
        m++;
         cout<<"------------allen----add完成----请指示-----------"<<endl;
        return true;
    }
    //删除
    bool  move(int n){
        T * temp=(T *)malloc(sizeof(T)*m);
        for (int i =0; i<n; i++) {
            temp[i]=mystr[i];
        }
        for (int i =n; i<m+1; i++) {
            temp[i-1]=mystr[i];
        }
        delete[] mystr;
        mystr=temp;
        m--;
        cout<<"------------allen----move完成----请指示-----------"<<endl;
        return true;
    }
    //改
    bool change(T t,int n){
        mystr[n]=t;
//        cout<<mystr[n]<<endl;
        cout<<"-----------allen---move完成---请指示-------------"<<endl;
        return true;
    }
    //查
    T cha(int n){
        cout<<mystr[n]<<endl;
        return mystr[n];
        cout<<"-----------allen---cha完成----请指示--------------"<<endl;
    }
    ~templa(){
        if (mystr!=NULL) {
            delete [] mystr;
        }
        cout<<"-----------allen---~templa完成---请指示-----------"<<endl;
    }
    //push
    bool push(T a){
        T * temp=(T *)malloc(sizeof(T)*m+sizeof(T)+sizeof(T));
        for (int i =0; i<m; i++) {
            temp[i]=mystr[i];
        }
        temp[m]=a;
        delete[] mystr;
        mystr=temp;
        m++;
        cout<<"------------allen----push完成----请指示-----------"<<endl;
        return true;
    }
    //pop
    bool  pop(){
        int n=m;
        T * temp=(T *)malloc(sizeof(T)*(n-1));
        for (int i =0; i<n-1; i++) {
            temp[i]=mystr[i];
        }
        delete[] mystr;
        mystr=temp;
        m--;
        cout<<"------------allen----pop完成----请指示-----------"<<endl;
        return true;
    }
private:
    //个数
    int m;
    T* mystr;
};

int main()
{
    int a;
    cout<<"请输入要存的个数:"<<endl;
    cin>>a;
    templa<int> allen(a);
    allen.dayin();
    allen.move(2);
    allen.dayin();
    allen.cha(2);
    allen.add(1, 2);
    allen.dayin();
    allen.change(1, 1);
    allen.push(22);
    allen.dayin();
    int b;
    cout<<"请输入要存的个数:"<<endl;
    cin>>b;
    templa<double> allen2(b);
    allen2.dayin();
    allen2.move(2);
    allen2.dayin();
    allen2.cha(2);
    allen2.add(3, 2);
    allen2.dayin();
    allen2.change(3, 1);
    allen2.pop();
    allen2.dayin();
    return 0;
}

c++用类模版实现push与 pop