Init two dimension class array : Array Two Dimension « Data Structure « C++






Init two dimension class array

Init two dimension class array
#include <iostream>
using namespace std;

class MyClass {
  int a, b;
public:
  MyClass(int n, int m) { 
     a = n; 
     b = m; 
  }
  int getA() { 
     return a; 
  }
  int getB() { 
  
     return b; 
  }
};

int main()
{
  MyClass ob[4][2] = {
    MyClass(1, 2), MyClass(3, 4), 
    MyClass(5, 6), MyClass(7, 8),
    MyClass(9, 10), MyClass(11, 12),
    MyClass(13, 14), MyClass(15, 16)
  };

  int i;

  for(i = 0; i <4; i++) {
    cout << ob[ i ][0].getA() << ' ';
    cout << ob[ i ][0].getB() << endl;
    cout << ob[ i ][1].getA() << ' ';
    cout << ob[ i ][1].getB() << endl;
  }

  cout << endl;

  return 0;
}


           
       








Related examples in the same category

1.Create a two-dimensional array of objects.Create a two-dimensional array of objects.
2.Define and initiate a two-dimensional arrayDefine and initiate a two-dimensional array
3.Multidimensional ArraysMultidimensional Arrays