#include <iostream>
using namespace std;
template<typename T>
class MyArray
{
int min_index;
int max_index;
T *data;
public:
MyArray(int mi,int ma)
{
min_index=mi;
max_index=ma;
data=new T[max_index-min_index+1];
}
MyArray(int ma)
{
min_index=0;
max_index=ma;
data=new T[1+max_index];
}
~MyArray()
{
delete [] data;
}
T & operator [](int index)const
{
return data[index-min_index];
}
int getMinIndex()const{return min_index;}
int getMaxIndex()const{return max_index;}
};
template<typename T>
istream &operator >>(istream &is,MyArray<T> &a)
{
for(int i=a.getMinIndex();i<=a.getMaxIndex();i++)
is>>a[i];
return is;
}
template<typename T>
ostream &operator <<(ostream &os,const MyArray<T> &a)
{
for(int i=a.getMinIndex();i<=a.getMaxIndex();i++)
os<<a[i]<<' ';
return os;
}
int main()
{
MyArray<double>x(3,8);
cout<<"请输入"<<(x.getMaxIndex()-x.getMinIndex()+1)<<"个数据";
cin>>x;
cout<<"刚输入的数据是: ";
cout<<x<<endl;
return 0;
}
模板类MyArry
最新推荐文章于 2022-09-06 18:36:14 发布