A simple bounded 2-d array example. : Array Object « Data Structure « C++






A simple bounded 2-d array example.

A simple bounded 2-d array example.
 


#include <iostream>
#include <cstdlib>
using namespace std;

class MyArray {
  int isize, jsize;
  int *p;
public:
  MyArray(int i, int j);
  int &put(int i, int j);
  int get(int i, int j);
};

MyArray::MyArray(int i, int j)
{
  p = new int [i*j];
  if(!p) {
    cout << "Allocation error\n";
    exit(1);
  }
  isize = i;
  jsize = j;
}

int &MyArray::put(int i, int j)
{
  if(i <0 || i>=isize || j<0 || j>=jsize) {
    cout << "Bounds error!!!\n";
    exit(1);
  }
  return p[i*jsize + j];
}

int MyArray::get(int i, int j)
{
  if(i <0 || i>=isize || j<0 || j>=jsize) {
    cout << "Bounds error!!!\n";
    exit(1);
  }
  return p[i*jsize +j]; 
}
  
int main()
{
  MyArray a(2, 3);
  int i, j;

  for(i = 0; i <2; i++)
    for(j=0; j<3; j++)
      a.put(i, j) = i+j;

  for(i = 0; i <2; i++)
    for(j=0; j<3; j++)
      cout << a.get(i, j) << ' ';

  a.put(10, 10);

  return 0;
}



           
         
  








Related examples in the same category

1.Init class array: char typeInit class array: char type
2.Define and init class array: two int type parameters in constructorDefine and init class array: two int type parameters in constructor
3.Init class array: int constructor parameterInit class array: int constructor parameter
4.Arrays of ObjectsArrays of Objects
5.Initialize each object in an array by specifying an initialization list
6.Two dimension object array: reference by indexTwo dimension object array: reference by index
7.Two dimension object array: reference by pointerTwo dimension object array: reference by pointer
8.Incrementing an object pointer