Object Containment

A couple months ago, I decided I'd like to learn some basic coding skills, so I bought a book called "Beginning C++ Through Game Programming." Most of the concepts seemed very intuitive and well-explained, but now that I'm nearing the end (I'm on ch.9 of 10), I'm starting to feel overwhelmed. I guess I shouldn't have expected to be able to do this all on my own.

I was hoping someone could help me understand this program that's supposed to demonstrate object containment. The book usually gives pretty thorough explanations of the example programs, but in this case, I felt that the explanation was lacking. I think what's really throwing me is the m_Critters vector. Up until this point in the book, all of the vectors have been <int> or <string> type vectors, and I don't really understand how a vector can be declared as a <Critter> type (which is the name of a class in the program). It seems like it should just be a string vector, since it seems to be holding strings.

I tried changing this
 
cout << iter->GetName() << " here.\n";

to this
 
cout << *iter << " here.\n";

so I could see what exactly was in the vector, but I got a compile error. I think it would be helpful if I could see some other examples of how a vector can contain class members. If someone could direct me to a tutorial that explains these concepts, I'd really appreciate it.

Thanks everyone. Here's the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Critter Farm
//Demonstrates object containment

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Critter
{
public:
    Critter(const string& name = "");
    string GetName() const;  
    
private:
    string m_Name;
};

Critter::Critter(const string& name):
    m_Name(name)
{}

inline string Critter::GetName() const 
{
    return m_Name;
}

class Farm
{
public:
    Farm(int spaces = 1); 
    void Add(const Critter& aCritter);
    void RollCall() const;
    
private:
    vector<Critter> m_Critters;
};

Farm::Farm(int spaces)
{
    m_Critters.reserve(spaces);
}

void Farm::Add(const Critter& aCritter)
{
    m_Critters.push_back(aCritter);
}

void Farm::RollCall() const
{
    for (vector<Critter>::const_iterator iter = m_Critters.begin(); 
         iter != m_Critters.end(); 
         ++iter)
	{
        cout << iter->GetName() << " here.\n";
	}
}

int main()
{
    Critter crit("Poochie");
    cout << "My critter's name is " << crit.GetName() << endl; 

    cout << "\nCreating critter farm.\n";
    Farm myFarm(3);
    
    cout << "\nAdding three critters to the farm.\n";
    myFarm.Add(Critter("Moe"));
    myFarm.Add(Critter("Larry"));
    myFarm.Add(Critter("Curly"));

    cout << "\nCalling Roll...\n";
    myFarm.RollCall();
       
    return 0;
}
It seems like it should just be a string vector, since it seems to be holding strings.
What makes you think that?

I think it would be helpful if I could see some other examples of how a vector can contain class members.
A vector can contain objects of any type. I suspect the iterator is what's confusing you.
Let's change that code a bit:
1
2
3
4
5
for (int i = 0; i < m_Critters.size(); i++)
{
    const Critter &critter = m_Critters[i];
    cout <<critter.GetName()<<" here.\n";
}
An item in a list of Critters should be a Critter, right? The fact that I output only the name of the Critter doesn't change the fact that the list doesn't contain just the names of Critters.

Does that make anything clearer?
Yes, that does help, thank you. So could I change the code so that each item in the vector can return multiple values? Like, if I changed the for loop to something like this
1
2
3
4
5
6
for (int i = 0; i < m_Critters.size(); i++)
{
    const Critter &critter = m_Critters[i];
    cout <<critter.GetName1()<<" here.\n";
    cout <<critter.GetName2()<<" here.\n";
}

and then I added a second function that returned a different member? I think I understand the concept now, but I'm not sure if I could code it on my own. I'm going to give it a shot though...
So could I change the code so that each item in the vector can return multiple values? Like, if I changed the for loop to something like this [...] and then I added a second function that returned a different member?
Yes, that's correct.
Cool, thanks again!
Topic archived. No new replies allowed.