Create a two-dimensional array of objects. : Array Two Dimension « Data Structure « C++






Create a two-dimensional array of objects.

Create a two-dimensional array of objects.


#include <iostream>
using namespace std;

class MyClass {
  int a;
public:
  MyClass(int n) { 
     a = n; 
  }
  int getA() { 
     return a; 
  }
};

int main()
{
  MyClass ob[4][2] = {
    1, 2,
    3, 4,
    5, 6,
    7, 8
  };
  int i;

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

  cout << endl;

  return 0;
}


           
       








Related examples in the same category

1.Init two dimension class arrayInit two dimension class array
2.Define and initiate a two-dimensional arrayDefine and initiate a two-dimensional array
3.Multidimensional ArraysMultidimensional Arrays