64,282
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
const int defaultsize=10;
class animal
{
public:
animal(int weight):i(weight){}
animal():i(0){}
~animal(){}
int geti()
{
return i;
}
void display()
{
cout <<i <<endl;
}
private:
int i;
};
template <class t>
class array
{
public:
int gets()const
{
return s;
}
const t &operator[](int offset)const
{
return p[offset];
}
t &operator[](int offset)
{
return p[offset];
}
array(int size=defaultsize):
s(size)
{
p=new t(s);
for (int i=0;i <s;++i)
p[i]=0;
}
array(const array &rhs)
{
s=rhs.gets();
p=new t[s];
for (int i=0;i <s;++i)
p[i]=rhs[i];
}
array &operator=(const array &rhs)
{
if (this=&rhs)
return *this;
delete [] p;
s=rhs.gets();
p=new t[s];
for (int i=0;i <s;++i)
p[i]=rhs[i];
}
template <class U>//mark
friend ostream &operator <<(ostream &thestream,array <U> ta);
private:
t *p;
int s;
};
template <class t>
ostream &operator <<(ostream &thestream,array <t> ta)
{
for (int i=0;i <ta.gets();++i)
thestream <<ta.p[i] <<endl;
return thestream;
}
int main()
{
array <int> ta;
int offset,value;
while (true)
{
cout <<"erter two number:";
cin>>offset>>value;
if (offset <0)
break;
ta[offset]=value;
}
cout <<ta;
return 0;
}