Solved

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

Posted on 2015-01-23
4
92 Views
Last Modified: 2015-01-23
Can we declare an array of objects? Array of classes?
0
Comment
Question by:Nusrat Nuriyev
  • 3
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 40567407
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
 
LVL 86

Expert Comment

by:jkr
ID: 40567410
BTW, for a more complex example, see the tutorial at https://www.hscripts.com/tutorials/cpp/array-of-objects.php
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40567413
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
 

Author Comment

by:Nusrat Nuriyev
ID: 40567517
jkr, thank you for your input.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question