#include <cstdlib>
#include <iostream>
using namespace std;
class Arr10 {
public:
static const size_t TOTAL_ARRAYS = 10;
Arr10(size_t _size);
~Arr10();
int& operator[](int);
void print() const;
private:
int * arr[TOTAL_ARRAYS];
size_t size;
};
int& Arr10::operator[](int index)
{
if (static_cast<size_t>(index) >= size * TOTAL_ARRAYS) {
return arr[0][0];
}
size_t x = index / size;
size_t y = index % size;
return arr[x][y];
}
Arr10::Arr10(size_t _size)
{
size = _size;
if (size != 0) {
for (int i = 0; i < static_cast<int>(TOTAL_ARRAYS); ++i) {
arr[i] = new int[size];
for (int j = 0; j < static_cast<int>(size); ++j) {
arr[i][j] = 0;
}
}
}
}
Arr10::~Arr10()
{
for (int i = 0; i < static_cast<int>(TOTAL_ARRAYS); ++i) {
for (int j = 0; j < static_cast<int>(size); ++j) {
delete arr[i];
}
}
}
void Arr10::print() const
{
for (int i = 0; i < static_cast<int>(TOTAL_ARRAYS); ++i) {
for (int j = 0; j < static_cast<int>(size); ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main()
{
size_t s = 4;
Arr10 arr10(s);
for (int i = 0; i < static_cast<int>(s * Arr10::TOTAL_ARRAYS); ++i)
arr10[i] = i;
arr10.print();
arr10[0] = 66;
arr10[4] = 66;
arr10[8] = 66;
arr10[33333] = 66;
cout << endl;
arr10.print();
return 0;
}
Comments
Leave a comment