You are on page 1of 1

////////////////////////////////////////////////////////////// class Target

class Toy const int MAXEMPLOYEES = 100; {


{ class Company; // Company implementations public:
class Employee ////////////////////////////////////////////////////////////// Target();
...
{ bool move(char dir);
}; Company::Company() int position() const;
public:
{ void replayHistory() const;
// The last argument to Employee's constructor is a pointer to a private:
class Pet // Company object. Employee's constructor will just stuff that m_bonusRate = 0; int pos;
{ // into the m_company data member, which is of type pointer-to- m_nEmployees = 0; string history;
public: Company } };

Pet(string nm, Toy* ft); Employee(string nm, int a, double sal, Company* cp);
Company::~Company() Target::Target()
string name() const;
~Pet(); {
{
void receiveBonus() const; pos = 0;
void changeToy(Toy* ft); ... for (int k = 0; k < m_nEmployees; k++) history = "";
... private: delete m_employees[k]; }
private: string m_name; }
bool Target::move(char dir)
string m_name; double m_salary; {
int m_age; void Company::hire(string nm, double sal, int a)
Toy* m_favoriteToy; {
switch (dir)
Company* m_company; {
... // In a member function of Company, the this pointer is a case 'R':
};
}; // pointer to the Company object the function was called on. case 'r':
pos++;
class Company break;
Pet::Pet(string nm, Toy* ft) { m_employees[m_nEmployees] = new Employee(nm, a, sal, this); case 'L':
{ public: m_nEmployees++; case 'l':
Company(); } pos--;
m_name = nm; break;
~Company();
m_favoriteToy = ft; void Company::setBonusRate(double pct) default:
void hire(string nm, double sal, int a); return false;
} void setBonusRate(double pct); { }
double bonusRate() const; m_bonusRate = pct; history += toupper(dir);
Pet::~Pet() void giveBonuses() const; } return true;
}
{ private:
double m_bonusRate; double Company::bonusRate() const
delete m_favoriteToy; {
int Target::position() const
Employee* m_employees[MAXEMPLOYEES]; {
} return m_bonusRate;
int m_nEmployees; return pos;
}; } }
void Pet::changeToy(Toy* ft)
void Company::giveBonuses() const void Target::replayHistory() const
{ ////////////////////////////////////////////////////////////// {
delete m_favoriteToy; // Employee implementations { for (int k = 0; k != history.size(); k++)
////////////////////////////////////////////////////////////// for (int k = 0; k < m_nEmployees; k++) cout << history[k] << endl;
m_favoriteToy = ft; m_employees[k]->receiveBonus(); }
} }
Employee::Employee(string nm, int a, double sal, Company* cp)
//Auxiliary Functions
{ void repeatMove(Target& x, int nTimes, char dir)
void f(string s) m_name = nm; ////////////////////////////////////////////////////////////// {
{ m_salary = sal; // main routine for (int k = 1; k <= nTimes; k++)
Pet p(s, nullptr); m_age = a; ////////////////////////////////////////////////////////////// x.move(dir);
m_company = cp; int main() }
Pet q("Frisky", new Toy); { void report(const Target& x)
}
p.changeToy(new Toy); Company myCompany;
{
cout << "There's a target at position " << x.position() << endl;
q.changeToy(nullptr); string Employee::name() const myCompany.hire("Ricky", 80000, 34); }
p.changeToy(new Toy); { myCompany.hire("Lucy", 40000, 32); int main()
{
} return m_name;
Company yourCompany; Target t;
} t.move('R');
yourCompany.hire("Fred", 50000, 51);
int main() t.move('R');
void Employee::receiveBonus() const t.move('L');
{ myCompany.setBonusRate(.02); cout << t.position() << endl; // writes 1
{
f("Fluffy"); // This uses the m_company member to access the Company myCompany.giveBonuses(); t.replayHistory();
} object ...
Target t2;
// associated with this Employee object in order to ask it } t2.move('L');
// what the bonus rate is. ========================================
cout << "Pay to the order of " << m_name << " the amount $" Syntax notes: Target ta[3];
<< ( m_salary * m_company->bonusRate() ) << endl; Use the dot operator . when what you have on the left is an ta[0].move('L');
OBJECT or a REFERENCE TO AN OBJECT. ta[1].move('R');
} repeatMove(ta[2], 5, 'R');
Use the arrow operator -> when what you have on the left is a report(ta[2]); // writes There's a target at position 5
POINTER TO AN OBJECT. }

You might also like