#include<iostream>
#include<cstdlib>
using namespace std;
template<class T>
class data1
{
private:
T* date;
int size;
public:
data1(int n=0);
~data1();
data1(const data1<T>& cp);
data1<T>& operator=(const data1<T>& cp);
T get(int _size);
friend istream& operator>>(istream& in, data1<T>& cp);
friend ostream& operator<<(ostream& out, data1<T>& cp);
};
template<class T>
data1<T>::data1(int n):size(n)
{
date = new T[size];
for (int i = 0; i < size; i++)
{
date[i] = rand() % 100;
}
}
template<class T>
data1<T>::~data1()
{
if (date != 0)
delete[]date;
}
template<class T>
data1<T>::data1(const data1<T>& cp)
{
size = cp.size;
date = new T[size];
for (int i = 0; i < size; i++)
{
date[i] = cp.date[i];
}
}
template<class T>
data1<T>& data1<T>::operator=(const data1<T>& cp)
{
if (date != 0)
delete[]date;
size = cp.size;
date = new T[size];
for (int i = 0; i < size; i++)
{
date[i] = cp.date[i];
}
}
template<class T>
T data1<T>::get(int _size)
{
return date[_size];
}
template<class T>
istream& operator>>(istream& in, const data1<T>& cp)
{
in >> cp.size;
for (int i = 0; i <cp.size; i++)
{
in >> cp.date[i];
}
return in;
}
template<class T>
ostream& operator<<(ostream& out,const data1<T>& cp)
{
out << "输出元素个数为:" << cp.size << endl;
for (int i = 0; i < cp.size; i++)
{
out >> cp.date[i]<<" ";
}
return out;
}
int main()
{
data1<int> bu9(5);
cin >> bu9//调用重载输入模板,出现无法解析外部符号,这是为什么呢?
cout << bu9;
return 0;
}