Implement 2 destructors.
Signed-off-by: Abdulkadir Furkan Şanlı <me@abdulocra.cy>
This commit is contained in:
@@ -68,6 +68,26 @@ Bank::closeAccount (Client &x)
|
||||
return false; // Account doesn't exist.
|
||||
}
|
||||
|
||||
// Only used in ~Client
|
||||
void
|
||||
Bank::closeAccountNoBail (Client &x)
|
||||
{
|
||||
for (auto i = clients.begin (); i != clients.end (); i++)
|
||||
if (i->id == x.id)
|
||||
{
|
||||
clients.erase (i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Client::~Client ()
|
||||
{
|
||||
for (auto i : accounts)
|
||||
{
|
||||
i->closeAccountNoBail (*this);
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
Bank::hire (Employee &x)
|
||||
{
|
||||
@@ -263,6 +283,14 @@ Gov::bailout (Bank &x, const double amount)
|
||||
return false;
|
||||
}
|
||||
|
||||
Employee::~Employee ()
|
||||
{
|
||||
if (employer != nullptr)
|
||||
{
|
||||
employer->fire (*this);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
@@ -382,6 +410,21 @@ main ()
|
||||
if (joe.getMattress () != 550)
|
||||
exit (1);
|
||||
|
||||
{ // Test ~Employee
|
||||
Employee tempemp ("underwood");
|
||||
boa.hire (tempemp);
|
||||
}
|
||||
if (boa.countEmployees () != 2)
|
||||
exit (1);
|
||||
|
||||
{
|
||||
Client tempclient ("guy");
|
||||
boa.openAccount (tempclient);
|
||||
}
|
||||
if ((boa.countClients () != 2 && bailed == 1)
|
||||
|| (boa.countClients () != 3 && bailed == 0))
|
||||
exit (1);
|
||||
|
||||
// Run until both 0 and 1 are seen, in order to test both code paths for
|
||||
// bailout.
|
||||
cout << "Bailed out: " << bailed << ", all others successful." << endl;
|
||||
|
||||
@@ -42,6 +42,7 @@ public:
|
||||
bool fire (Employee &x);
|
||||
bool openAccount (Client &x);
|
||||
bool closeAccount (Client &x);
|
||||
void closeAccountNoBail (Client &x);
|
||||
// TODO: implement loans, for now there is placeholder loan() and unloan(),
|
||||
// in order to simulate loaning and test bailout mechanism.
|
||||
|
||||
@@ -85,6 +86,7 @@ class Employee
|
||||
public:
|
||||
std::string name;
|
||||
Employee (std::string x) : name (x){};
|
||||
~Employee ();
|
||||
|
||||
private:
|
||||
Bank *employer = nullptr;
|
||||
@@ -97,6 +99,7 @@ class Client
|
||||
public:
|
||||
std::string id;
|
||||
Client (std::string x) : id (x), underMattress (0){};
|
||||
~Client ();
|
||||
double earn (const double amount); // Earn money.
|
||||
double getMattress ();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user