Arrays of objects in C++

An array of object is declared just like a normal array.

For Example: A two dimensional array

#include <iostream>

using namespace std;

class test {

int a;


void set_a(int b) { a=b; }

int get_a() { return a; }


int main()


test ob[2];

int a;

for(a=0; a<2; a++) ob[a].set_a(a+1);

for(a=0; a<2; a++)

cout << ob[a].get_a() << “\n”;

return 0;


Leave a Reply