I am very confused about array of objects coding. I have looked around a bit, and nothing of what I found was helpful in having me actually understand it (cause most of it was just all code and no words).
So I don't know how to work an array of objects for data members. Anything anyone could tell me that can help me understand this would be greatly appreciated.
To really understand either [] or vector<> well, you need to understand the amount of memory allocated and value semantics (when are copy constructors called?).
For example, you should understand it enough to know why:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Parent {
public:
Parent() { }
};
class Child1 : public Parent {
};
class Child2 : public Parent {
};
vector<Parent> this_is_bad; // cannot hold a mixture of Child1 and Child2
vector<Parent*> this_is_ok; // holds anything that points to a derivation of Parent, including Parent*
vector<Parent&> this_is_also_bad; // not allowed - why?
Just remember, a [] or vector<> just points to a chunk of contiguous memory.
You should be able to draw what this chunk of memory looks like.
#include <string>
//All the members of this class are private, and it has no public methods.
// For testing you may want to use a struct instead.
class pets
{
string PetName;
int PetNum; //lowercase 'i' in "int"
string Species;
}; //don't forget the ;
int main() {
constint N = ...;
pets inventory[N]; //creates an array of N pets
... inventory[0] //do something with the first pets object
...
return 0;
}