It appears that your program is demonstrating the ability of assigning default values to function arguments.
It starts out with includes and "using namespace std". If these need explanation, I would suggest a quick Google search.
Next you have two classes, Critter and Farm. Lets start with the Critter class.
1 2 3 4 5 6 7 8
|
class Critter
{
public:
Critter(const string& name = ""): m_Name(name) {}
string GetName() const { return m_Name; }
private:
string m_Name;
};
|
This is the class definition. It starts out with a constructor function. It accepts a string, in the form of a constant, then assigning it to the private variable m_Name. Taking a look into 'cont string& name = ""', this defines once again a constant string, a reference I believe. This is followed by '= ""'. This allows you to call this function omitting this argument. If this is done, it will effectively assign "" to the constant string name.
The next line is somewhat self explanatory. It returns the value of the private string m_Name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Farm
{
public:
Farm(int space = 1) { m_Critters.reserve(space); }
void Add(const Critter& aCritter) { m_Critters.push_back(aCritter); }
void RollCall() const
{
for (vector<Critter>::const_iterator iter = m_Critters.begin();
iter!= m_Critters.end(); ++iter)
cout << iter->GetName() << " here.\n";
}
private:
vector<Critter> m_Critters;
};
|
The next class is of a similar concept. The constructor has an int named space whose default value is 1. This allows the calling function to omit this argument. For example, calling "Farm();" would create a farm whose space is equal to 1, while "Farm(2);" would create a farm whose space is equal to 2.
This class has a private vector. This is similar to an array, but more intuitive. If you need an explanation on this, again please refer to Google. The Add function adds a Critter to the vector, and RollCall prints out all the Critters in the vector.
The main function simply creates instances of both classes for demonstration purposes.