< Previous | Contents | Next >

Introducing the Critter Farm

Program

The Critter Farm program defines a new kind of critter with a name. After the program announces a new critters name, it creates a critter farma collection of critters. Finally, the program calls roll on the farm and each critter announces its name. Figure 9.1 shows the results of the program.


image

Figure 9.1

The critter farm is a collection of critters, each with a name.


You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 9 folder; the filename is critter_farm.cpp.

//Critter Farm

//Demonstrates object containment


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

Using Aggregation 289


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);

}

290 Chapter 9 n Advanced Classes and Dynamic Memory: Game Lobby


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;

}