Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
array of object (class).



...
Posted
Updated 24-May-12 21:41pm
v6
Comments
Sergey Alexandrovich Kryukov 24-May-12 14:53pm    
Why? No abstract thinking, only by example, or what? Don't get me wrong: I don't mind helping, look at my other answers, but only to those who show some hope.
--SA
Sergey Alexandrovich Kryukov 24-May-12 14:54pm    
By the way, what is "array of class"? Class is not an object in C++.
--SA
sajad abbasi 24-May-12 15:14pm    
I can not speak English. And my English is bad.
I can not get what I mean.

Please do it by yourself — this is easy enough. Do you know how many non-trivial data structures can be built with C++? If you ask for examples in all cases, you won't have any time to learn anything; and such answers would not help you.

And, by the way, if your criteria are like "is easy", you should better give up learning programming right now instead of wasting you life on it, honestly.

—SA
 
Share this answer
 
Comments
Maciej Los 24-May-12 17:08pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 24-May-12 18:04pm    
Thank you, Maciej.
--SA
VJ Reddy 25-May-12 6:42am    
Nice answer. 5!
Sergey Alexandrovich Kryukov 25-May-12 11:49am    
Thank you, VJ.
--SA
Arrays of classes are no different to arrays of any of the built-in types.
You use the [] operator to access the element you want in addition to the . or -> operators to access member functions/vars.

Take an example of strings.

C++
#include <string>
#include <cstdio>
#include <cstdlib>

using std::string;

int main()
{
    int i;
    const int maxItems = 10;
    string someStrings[maxItems];

    for (i=0; i<maxItems; i++)
    {
        someStrings[i] = "TextItem: ";
        someStrings[i] += ('0' + i);
    }

    for (i=0; i<maxItems; i++)
        printf("String_%d is: %s\n", i, someStrings[i].c_str() );

    return 0;
}
 
Share this answer
 
v2
Comments
CPallini 24-May-12 15:17pm    
Why do you feel the need of including iostream?
:-)
enhzflep 24-May-12 15:20pm    
Simple. - Without it, the code won't compile.
I had intended to use cout, though changed my mind and was too lazy to change <iostream> to <string> :)
CPallini 24-May-12 15:41pm    
Right, I missed that. Anyway, now you have to change it. :-D
Carlo The Nitpick note: #include <stdio.> should be instead #include <cstdio> and so on...
enhzflep 24-May-12 15:45pm    
:Grins:
Thanks Carlo. Duly noted and updated. :-)
Maciej Los 24-May-12 17:08pm    
Good answer, my 5!
An array is a region in memory containing equally sized elements of the same type. These can be objects or literals (integers, characters, floating point, string, boolean, pointer or user defined literals).

The array notation a[n] is convenient language construct that provides access to the element at offset n in the array. The compiler knows the size, and alignment, of each element so you don't need to handle this.
Basically the compier translates a[n] into an address in memory.

A class defines the behaviour of an object, so we can have arrays of objects, but not classes, and an object is just a region of memory.

A class may provide it's own idea about how the array operator should work, but it's usually advisable that the functionality should be conceptually equivalent to a regular array.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sandeep Mewara 25-May-12 0:57am    
Nice. 5!
Espen Harlinn 25-May-12 3:29am    
Thank you, Sandeep :-D
VJ Reddy 31-May-12 20:12pm    
Nice explanation. 5!
Espen Harlinn 1-Jun-12 4:14am    
Thanks VJ :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900