标签:
#include <iostream>
using namespace std;
template<typename T>
class Array
{
public:
Array(int s);
virtual ~Array();
virtual const T& Entry(int index)const;
virtual void Enter(int index, const T& value);
protected:
int size;
T* element;
};
template<typename T>
Array<T>::Array(int s)
{
if (s>1)
{
size = s;
}
else
{
size = 1;
}
element = new T[size];
}
template<typename T>
Array<T>::~Array()
{
delete[] element;
}
template<typename T>
const T& Array<T>::Entry(int index) const
{
return element[index];
}
template<typename T>
void Array<T>::Enter(int index, const T& value)
{
element[index] = value;
}
int main()
{
Array<int> IntAry(5);
for (int i=0; i<5; i++)
{
IntAry.Enter(i, i);
}
cout << "Integer Array: \n";
for (int i=0; i<5; i++ )
{
cout << IntAry.Entry(i) << ‘\t‘;
}
cout << endl;
Array<double> DouAry(5);
for (int i=0; i<5; i++)
{
DouAry.Enter(i, (i + 1)*0.35);
}
cout << "Double Array: \n";
for (int i=0; i<5; i++)
{
cout << DouAry.Entry(i) << ‘\t‘;
}
cout << endl;
return 0;
}
标签:
原文地址:http://www.cnblogs.com/nfking/p/5582674.html