#ifndef ARRAY_H_INCLUDED
#define ARRAY_H_INCLUDED
#include<iostream>
using namespace std;
class Array
{
public:
int m_length;
int *m_space;
public:
Array(int length)
{
this->m_length = length;
m_space = new int[m_length];
}
void setData(int index, int value)
{
m_space[index] = value;
}
int getData(int i)
{
return m_space[i];
}
int &operator[](int i)
{
return m_space[i];
}
Array& operator=(Array &temp)
{
if (this->m_space != NULL)
{
delete [] m_space;
m_length = 0;
}
this->m_length = temp.m_length;
this->m_space = new int[m_length];
for (int i = 0; i < m_length; i++)
{
this->m_space[i] = temp[i];
}
return *this;
}
int operator==(Array &temp)
{
if (this->m_length != temp.m_length)
return 0;
for (int i = 0; i < this->m_length; i++)
{
if (this->m_space[i] != temp[i])
return 0;
}
return 1;
}
};
#endif
#include"Array.h"
int main()
{
Array a1(10);
for (int i = 0; i < a1.m_length; i++)
{
a1.setData(i, i);
}
cout << "a1数组元素为:" <<endl;
for (int i = 0; i < a1.m_length; i++)
{
cout << a1.getData(i) << " ";
}
cout << endl;
Array a2(5);
a2 = a1;
a2[2] = 10;
cout << "a1[2]: " << a1[2] << endl;
cout << "a2[2]: " << a2[2] << endl;
cout << "a2数组元素为:" <<endl;
for (int i = 0; i < a2.m_length; i++)
{
cout << a2.getData(i) << " ";
}
cout << endl;
if (a1 == a2)
cout << "两数组相等" << endl;
else
cout << "两数组不相等" << endl;
return 0;
}