#include<iostream>
#include<stdlib.h>
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;
}
template < typename T >
class BoundArray : public Array < T >
{
public:
BoundArray(int low = 0, int height = 1) :Array(height - low + 1)
{
if (height - low < 0)
{
cout << "Beyond the bounds of Array.\n";
exit(1);
}
min = low;
}
virtual const T & Entry(int index) const
{
if (index < min || index > min + size - 1)
{
cout << "Beyond the bounds of index.\n";
exit(1);
}
return Array<T>::Entry(index - min);
}
virtual void Enter(int index, const T& value)
{
if (index < min || index > min + size - 1)
{
cout << "Beyond the bounds of index.\n";
exit(1);
}
Array<T>::Enter(index - min, value);
}
protected:
int min;
};
int main()
{
int i;
int low = 1, height = 10;
BoundArray<int> b(low, height);
for (i = low; i <= height; i++)
b.Enter(i, i * 2);
cout << "BoundArray: \n";
for (i = low; i <= height; i++)
{
cout << "b[" << i << "] = " << b.Entry(i) << "\t";
if (i % 5 == 0)cout << endl;
}
}
哪位大神能帮忙看看应该怎么改吗