#include<iostream>
using namespace std;
class IntArray
{
public:
IntArray(int s = 0);
~IntArray();
int & operator [](int i);
IntArray & operator =(const IntArray & a);
friend ostream & operator << (ostream & out, IntArray & a);
private:
int *pArr;
int size;
};
IntArray::IntArray(int s)
{
size = s;
if (size == 0)
pArr = NULL;
else
pArr = new int[size + 1];
for (int i = 1;i <= size;i++)
pArr[i] = 0;
}
IntArray::~IntArray()
{
if (pArr != NULL)
delete[]pArr;
}
int & IntArray::operator[](int i)
{
if (i<1 || i>size)
{
cout << "Out of range!" << endl;
return pArr[0];
}
return pArr[i];
}
IntArray & IntArray:: operator =(const IntArray & a)
{
if (pArr != NULL)
delete[] pArr;
size = a.size;
if (size == 0)
pArr = NULL;
else
pArr = new int[size + 1];
for (int i = 1;i <= size;i++)
pArr[i] = a.pArr[i];
return *this;
}
ostream & operator << (ostream & out, IntArray & a)
{
for (int i = 1;i <= a.size;i++)
out << a.pArr[i] << ' ';
return out;
}
int main()
{
IntArray a(5), b(3);
for (int i = 1;i <= 5;i++)
a[i] = i;
b = a;
a[5] = b[5] = 10;
cout << "a: " << a << endl << "b: " << b << endl;
getchar();
return 0;
}