zoukankan      html  css  js  c++  java
  • ArrayTemplate

    #include <iostream>
    using namespace std;
    template<class ElementType,int n>
    class ArrayTemplate
    {
    public:
     ArrayTemplate();
     ArrayTemplate(const ElementType &t);
     ElementType& operator[](int i);
     void show();
    private:
     ElementType a[n];
    };
    template<class ElementType,int n>
    ArrayTemplate<ElementType,n>::ArrayTemplate()
    {
     cout<<"执行不带参数的构造函数\n";
     for (int i=0;i<n;i++)
     {
      a[i]=(i+1);
     }
    }
    template<class ElementType,int n>
    ArrayTemplate<ElementType,n>::ArrayTemplate(const ElementType &t)
    {
     cout<<"执行带一个参数的构造函数\n";
     for (int i=0;i<n;i++)
     {
      a[i]=t;
     }
    }
    template<class ElementType,int n>
    ElementType& ArrayTemplate<ElementType,n>::operator[](int i)
    {
     cout<<"执行下标运算符函数operator[]\n";
     if (i<0||i>=n)
     {
      cout<<"超出数组的限制,程序终止!\n";
      exit(EXIT_FAILURE);
     }
     return a[i];
    }
    template<class ElementType,int n>
    void ArrayTemplate<ElementType,n>::show()
    {
     for(int i=0;i<n;i++)
     {
      cout<<"a["<<i<<"]="<<a[i]<<"\t";
     }
     cout<<endl;
    }
    int main()
    {
     ArrayTemplate<int,4>array_1;
     array_1.show();
     ArrayTemplate<int,4>*array_2=new ArrayTemplate<int,4>[4];
     for(int i=0;i<9;i++)
     {
      array_2[i]=array_1[i];
      array_2[i].show();  
     }
     return 0;
    }

  • 相关阅读:
    面向对象(6day)
    pycharm使用问题总结
    docker学习(一)ubuntu上安装docker
    docker指令
    docker简单使用
    使用Docker搭建多人使用GPU服务器
    ubuntu常用指令
    高斯滤波
    ubuntu创建个人账户
    第一次使用SSE指令集
  • 原文地址:https://www.cnblogs.com/greencolor/p/2890451.html
Copyright © 2011-2022 走看看