2. Can we declare an array of objects? Array of classes?

Can we declare an array of objects? Array of classes?
Nusrat NuriyevAsked:
Who is Participating?
 
jkrCommented:
Also, since that seems common in your question: There are only arrays of instances of classes (aka objects), there's no such thing as an array of classes.
0
 
jkrCommented:
Yes, you can. That works just like an array of any other data, e.g.

class MyClass {

  // ...
};

int main() {

  MyClass array_of_myclass[42]; // declares an instantiates array of 42  ojects of 'MyClass'
}

Open in new window


As a side note, if you declare an array of objects, each element's default constructor is called.
0
 
jkrCommented:
BTW, for a more complex example, see the tutorial at https://www.hscripts.com/tutorials/cpp/array-of-objects.php
0
 
Nusrat NuriyevAuthor Commented:
jkr, thank you for your input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.